如何在 UI、BLL、DAL 之间使用 DTO

发布于 2024-10-12 14:40:06 字数 917 浏览 9 评论 0原文

我正在尝试编写一个在 BLL 和 DAL 之间具有非常严格边界的小型应用程序,现在我想知道在层之间传递数据(域传输对象)的最佳方法是什么。

我在域级别(类库)中实现了一些由 BLL 和 DAL 访问的类。这些类基本上只包含属性/数据成员,并且当前反映 DAL 数据。例如:

class CustomerData
{
  // some data fields
}

然后我在 BLL 中实现了一些类:

class Customer : CustomerData
{
  // Some methods
}

在我的 DAL 中,我通过 Linq-to-Sql 从数据库获取客户记录。然后,我通过以下方式将 linq 对象映射到我的 Domain 对象:

CustomerData.field = LinqObject.field
// Etc

我的想法是,我现在在请求时从 DAL 到 BLL 的 CustomerData 实例(并且我应该将 Customer 实例传递到我的 UI)。

在我的 BLL 中,我将收到一个 CustomerData 实例,但现在我想用它创建一个 Customer。

问题:

  1. 我现在是否必须在 BLL 中创建一个 Customer 实例并再次复制所有字段成员?
    客户c=新客户; c.field = CustomerData.field;
  2. 如何从 CustomerData 创建客户而无需字段复制步骤?
  3. 我应该使用组合吗?
    类客户 { 客户数据数据; ?
  4. 在我当前的布局中是否有更有效的方法(更少的编码等)来做到这一点
  5. 有更好的方法吗?
  6. 总体上有什么意见吗?

谢谢 !

I'm trying to write a small app with very strict boundaries between BLL and DAL and am now wondering what the best way would be to pass the data (Domain Transfer Objects) between the layers.

I implemented some classes in a Domain Level (class library) that is accessed by both BLL and DAL. These classes basically just contain properties/data members and currently reflect the DAL data. Ex:

class CustomerData
{
  // some data fields
}

Then I implemented some classes in BLL as :

class Customer : CustomerData
{
  // Some methods
}

In my DAL I get the customer records from the database through Linq-to-Sql. I then map the linq object to my Domain object by:

CustomerData.field = LinqObject.field
// Etc

My thinking is thus that I now a CustomerData instance from my DAL to BLL when requested (and that I should pass a Customer instance to my UI).

In my BLL I will thus receive a CustomerData instance, but now I want to make a Customer out of it.

Questions:

  1. Do I have to in my BLL now create a Customer instance and AGAIN copy all field members ?
    Customer c = new Customer;
    c.field = CustomerData.field;
  2. How can I create a Customer from the CustomerData without the field copy steps ?
  3. Should I rather then use composition ?
    class Customer
    {
    CustomerData Data;
    }
  4. Is there a more effective way (less coding etc) to do this in my current layout ?
  5. Is there better ways to do this ?
  6. Any comments in general ?

Thanks !

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

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

发布评论

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

评论(4

柳若烟 2024-10-19 14:40:06

一般来说,我认为 DTO 是非特定于层的,由 DAL 创建/使用,由 BLL 处理并由 UI 使用/创建。

通常,每个层都是 VS 解决方案文件夹中的一个单独项目,因此 DTO 是每个层引用的另一个项目。

这样如果有一个字段需要存在于UI中但不存在于其他层中,则可以继承DTO。

generally I consider DTO's to be non layer specific, created/consumed by DAL, processed by BLL and consumed/created by UI.

usually each layer is a separate project in the VS solution folder, the DTO's therefore are another project which is referenced by each layer.

this way if there is a field that needs to exist in the UI but not in the other layers, the DTO can be inherited from.

夜巴黎 2024-10-19 14:40:06

这里有一些我观点的注释,我不是神谕,但希望它能提供一些帮助:)

对我来说,感觉你这里有太多“模型”。它可能会引起混乱并导致大量代码只是为了在不同表示之间复制数据。大量的代码意味着更多的错误。然后,我认为数据类和业务类之间的继承在定义业务类时限制了您。如果您想创建一个由多个数据类组成的业务类怎么样?我认为你应该使用接口或组合。

通常,我只使用一种反映业务领域的概念模型。正如 Dead Rabit 指出的那样,该模型被数据层和业务层使用,在某些情况下甚至被表示层(在较小的应用程序中)使用。为了持久性,我使用 O/RM,例如 EF 4。

对于较大的项目,尤其是在分布式场景中,我对 UI 层使用自定义 DTO。这些类反映了 UI 的需求,并且可能与概念模型中的实体有很大不同。

就我个人而言,我认为实体框架 4 在根据这种结构构建应用程序时对您有很大帮助,如果您处于项目的早期阶段并使用 .NET 4,您可能想检查一下?

  1. 是的,您可能需要这样做。
  2. 使用组合
  3. 是的,我会使用组合
  4. 使用例如 Entity Framework 4
  5. (-"-)
  6. 见上文

Some notes from my point of view comes here, I'm not an oracle but hopefully it will give some help :)

To me it feels that you have too many "models" here. It might cause confusion and lead to a lot of code just to copy data between different representations. And lot of code means more bugs. Then, I think that the inheritance between your Data-classes and business-classes kinds of limit you when defining your business classes. How about if you want to create a business class that is composed by several data-classes? You should rather use interfaces or composition I think.

Usually, I work with only one conceptual model reflecting the business domain. This model is used both by the data and the business layer and in some cases even the presentation layer (in smaller apps), as Dead Rabit points out. For persistance I use an O/RM such as EF 4.

For larger projects, especially in distributed scenarios I use custom DTO's for the UI-layer(s). These classes reflects the needs of the UI, and could differ a lot from the entities in the conceptual model.

Personally, I think Entity Framework 4 helps you a lot when building apps according to this structure, if you are at an early stage of your project and uses .NET 4 you might want to check it out?

  1. Yes, you probably need to.
  2. Use composition
  3. Yes, I would use composition
  4. Use for example Entity Framework 4
  5. (-"-)
  6. see above
倒数 2024-10-19 14:40:06

如果您坚持在 DTO 上使用多个图层,则可以使用 AutoMapper 来帮助您一次从一个图层转换为另一个图层代码行(通过在 DTO 中使用相同的约定)。

CustomerData customerData = Mapper.Map<LinqObject, CustomerData>(linqObjectInstance);

您还应该查看PresentationModel模式:http://martinfowler.com/eaaDev/PresentationModel.html

如果你想走这条路,你也可以谷歌搜索MVVM(模型-视图-视图模型)。

If you insist on having several layers on DTOs, you can use AutoMapper to help you convert from one to another in one line of code (by using the same convention in your DTOs).

CustomerData customerData = Mapper.Map<LinqObject, CustomerData>(linqObjectInstance);

You should also look at the PresentationModel pattern: http://martinfowler.com/eaaDev/PresentationModel.html

You can also google for MVVM (Model-View-ViewModel) if you want to go this way.

懷念過去 2024-10-19 14:40:06

您没有完全使用 DTO 的 .在您的 Customer 类中,将 CustomerData 直接返回到您的 UI。

并且不需要从 CustomerData 继承 Customer

编辑:
我在这里完全使用了这个词,因为 CustomerData 是 DTO,因此不要返回 Customer,而是返回 CustomerData,因为它是您的 DTO,如下图所示。

一个建议是,您应该使用 存储库模式 来隔离您的 BLL 和 DAL 。

DTO1]

you are not fully using DTO's . In your Customer class, return CustomerData directly to your UI.

and there is no need to inherit Customer from CustomerData

EDIT:
I used the word fully here because CustomerData is DTO, so instead of returning Customer, return CustomerData as it is your DTO as seen in the following diagram.

One suggestion, you should use Repository Pattern to isolate your BLL and DAL.

DTO1]

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