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:
我想知道,我在asp.net mvc和nhibernate中有一个应用程序。我在asp.net mvc上的视图中读过这个,不应该知道Domain,它需要使用DTO对象。所以,我正在尝试这样做,我找到了AutoMapper组件,我不知道为某些域对象做正确的DTOS方法。我有这样的域类:
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 ?
我想举一些例子说明如何查看我的DTO?我只需要在DTO中使用字符串吗?而我的控制器,它应该获得域对象或DTO并在域上转换它以保存在存储库中?
Thanks a lot!
非常感谢!
Cheers
干杯
1 个解决方案
#1
1
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:
这个问题没有指导方针,取决于你的个人情况。我几乎没有在实践中证明有用的建议:1。使用平面DTO - 这意味着DTO的属性必须尽可能原始。这节省了对空引用检查的需要。例如,如果您有这样的域对象:
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
并且您需要在网格中输出员工列表并显示他们更喜欢创建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.
或类似的东西( - :2。不要将所有内容转换为sting - 这会将DTO绑定到具体视图,因为您将应用特殊格式。在视图中直接应用简单格式不是问题.3。使用DTO在你的post动作中,并将它们转换为域对象。通常控制器的动作是对不正确数据的第一线,你不能期望能够从用户的输入中构建一个有效的域对象。在大多数情况下,你必须做一些后处理,如验证,设置默认值等。之后你可以创建你的DTO。
#1
1
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:
这个问题没有指导方针,取决于你的个人情况。我几乎没有在实践中证明有用的建议:1。使用平面DTO - 这意味着DTO的属性必须尽可能原始。这节省了对空引用检查的需要。例如,如果您有这样的域对象:
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
并且您需要在网格中输出员工列表并显示他们更喜欢创建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.
或类似的东西( - :2。不要将所有内容转换为sting - 这会将DTO绑定到具体视图,因为您将应用特殊格式。在视图中直接应用简单格式不是问题.3。使用DTO在你的post动作中,并将它们转换为域对象。通常控制器的动作是对不正确数据的第一线,你不能期望能够从用户的输入中构建一个有效的域对象。在大多数情况下,你必须做一些后处理,如验证,设置默认值等。之后你可以创建你的DTO。