Felipe Oriani
Felipe Oriani

Reputation: 38598

How should be my DTO object for ASP.Net MVC View?

i'd like to know, I have a application in asp.net mvc and nhibernate. I've read about that in the Views on asp.net mvc, shouldn't know about the Domain, and it need use a DTO object. So, I'm trying to do this, I found the AutoMapper component and I don't know the correct way to do my DTOS, for some domain objects. I have a domain class like this:

public class Entity 
{
   public virtual int Id { get; set; }
   public virtual bool Active { get; set; }
}

public class Category : Entity 
{ 
   public virtual string Name { get; set; }
   public virtual IList<Product> Products { get; set; }

   public Category() { }
}

public class Product : Entity 
{ 
   public virtual string Name { get; set; }
   public virtual string Details { get; set; }
   public virtual decimal Prince { get; set; }
   public virtual int Stock { get; set; }
   public virtual Category Category { get; set; }
   public virtual Supplier Supplier { get; set; }

   public Product() { }
}

public class Supplier : Entity 
{
   public virtual string Name { get; set; }
   public virtual IList<Product> Products { get; set; } 

   public Supplier() { }  
}

I'd like to get some example of how can I do my DTOs to View ? Need I use only strings in DTO ? And my controllers, it should get a domain object or a DTO and transform it on a domain to save in repository ?

Thanks a lot!

Cheers

Upvotes: 2

Views: 1063

Answers (1)

Branislav Abadjimarinov
Branislav Abadjimarinov

Reputation: 5131

There is no guidelines on this matter and it depends on your personal chice. I have few advices that have proven useful in practice:
1. Use flat DTOs - this means that the properties of the DTO must be as primitive as possible. This saves you the need for null reference checking. For example if you have a domain object like this:

public class Employee
{
  prop string FirstName{get; set;}
  prop string LastName{get; set;}
  prop Employee Boss{get; set;}
  ...
}

And you need to output in a grid a list of employees and display information for their 1st level boss I prefer to create a DTO

public class EmployeeDTO
{
  prop string FirstName{get; set;}
  prop string LastName{get; set;}
  prop bool HaveABoss{get;set}
  prop string BossFirstName{get; set;}
  prop string BossLastName{get; set;}
  ...
}

or something like this (-:
2. Do not convert everything to sting - this will bind the DTO to a concrete view because you'll apply special formatting. It's not a problem to apply simple formatting directly in the view.
3. Use DTOs in your post actions and than convert them to domain objects. Usually controller's actions are the first line of deffence against incorrect data and you cannot expect to be able to allways construct a valid domain object out of the user's input. In most cases you have to do some post-processing like validation, setting default values and so on. After that you can create your DTOs.

Upvotes: 1

Related Questions