ASP.Net MVC 视图的 DTO 对象应该如何?

发布于 2024-09-02 06:41:39 字数 1094 浏览 3 评论 0原文

我想知道,我在 asp.net mvc 和 nhibernate 中有一个应用程序。我在 asp.net mvc 的视图中读到过这一点,不应该知道域,并且它需要使用 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() { }  
}

我想获取一些示例,说明如何执行 DTO 来查看?我需要在 DTO 中仅使用字符串吗?我的控制器,它应该获取域对象或 DTO 并将其转换到域上以保存在存储库中?

多谢!

干杯

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

依 靠 2024-09-09 06:41:39

关于这个问题没有任何指导方针,这取决于您的个人选择。我有一些在实践中被证明有用的建议:
1. 使用扁平DTO——这意味着DTO的属性必须尽可能原始。这使您无需进行空引用检查。
例如,如果您有一个像这样的域对象:

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

并且您需要在网格中输出员工列表并显示其第一级老板的信息,我更喜欢创建一个 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;}
  ...
}

或类似的东西(-:
2. 不要将所有内容都转换为 sting - 这会将 DTO 绑定到具体视图,因为您将应用特殊格式。直接在视图中应用简单的格式设置不是问题。
3. 在后期操作中使用 DTO,然后将其转换为域对象。通常控制器的操作是防止错误数据的第一道防线,并且您不能指望能够始终根据用户的输入构造有效的域对象。在大多数情况下,您必须进行一些后处理,例如验证、设置默认值等。之后您可以创建 DTO。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文