views:

101

answers:

1

Hi all, 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

+1  A: 

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.

Branislav Abadjimarinov
Hi Branislav, thanks for aswer! :)I'll do this way (with primitive types as possible)...You saw about validation, and in .net framework there's a dataAnnocations to validate properties of a object.... if I'll use DTO in an post action (of my controller) it must to be valid and the code must check it, so I need to use dataAnnotations in DTO ?and what do you use to create your DTOs? AutoMapper is a good choice ? Or do it with hard code ?thanks again man!CheersPS: i'm sorry for my english!
Felipe
I use AutoMapper. Is is pretty stable and easy to use. For valudation you can use data anotations or some of the third party libraries like xVal - http://blog.stevensanderson.com/2009/01/10/xval-a-validation-framework-for-aspnet-mvc/
Branislav Abadjimarinov