.NET N 层架构:我该如何处理模型对象?

发布于 2024-12-25 01:08:36 字数 389 浏览 0 评论 0原文

我正在使用 ASP.NET Web 表单 C# 从头开始​​创建一个解决方案。

我担心模型对象,因为我不想在每一层中创建重复的模型对象集。在 Web 表单 中使用 3 层架构中的模型对象的最佳实践是什么?

我想到的结构如下:

  • UI
  • BLL
  • DAL
  • 模型

该模型将包含可在层的每个部分中使用的所有模型。我认为这会很有用,因为每一层都需要访问模型对象。例如:

  1. UI调用BLL中的方法,传入填充数据的模型对象。
  2. BLL 通过保存的对象调用 DAL 中的方法 在数据库等中。

谢谢

I am creating a solution from scratch, using ASP.NET Web forms C#.

I am concerned about the model objects as I don't want to create duplicate sets of model objects in each layer. What is the best practice for using Model objects in 3 layer architecture in Web Forms?

The structure I have in mind is as follows:

  • UI
  • BLL
  • DAL
  • Model

The Model will contain all the model classes that can be used in each section of the layers. I thought this would be useful as each layer needs access to the model objects. For example:

  1. UI calls a method in BLL passing in a model object filled with data.
  2. BLL calls a method in DAL passing through the object which is saved
    in the database etc.

Thanks

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

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

发布评论

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

评论(4

还如梦归 2025-01-01 01:08:36

模型可以是层的横切关注,这是一种快速的方法。或者,您可以为模型创建接口,这样您就可以简单地用 BLL 之类的东西充实接口 - 这至少可以阻止它交叉。

它还取决于您的模型是否是简单的数据容器(贫血域模型),或者包含行为,例如能够验证自身或跟踪自身更改(富域模型)。

您可以发现您的 DAL 实际上由两部分组成:样板文件(从不特定于应用程序代码来与数据库通信)和特定于应用程序的填充模型代码。我们有这种情况。我们共享模型的接口,特定于应用程序的 DAL 代码可以使用此接口从模型中推送和提取数据,但“真正的”DAL 代码可以处理原始内容。

Models can be a cross-cutting concern with your layers, which is a quick way to do it. Or you can create interfaces for your models such that you could simply flesh out the interface in something like the BLL - this at least stops it being cross-cutting.

It further depends on if your models are simple data containers (anemic domain model), or contain behaviour, such as the ability to validate themselves or track changes themselves (rich domain model).

You can find that your DAL actually consists of two parts: the boilerplate-never-specific to an app code to talk to the database, and the app-specific populate-the-model code. We have this situation. We share interfaces of our models around, the app-specific DAL code can use this interface in order to push and pull data from the model, but the "true" DAL code works with raw stuff.

情深已缘浅 2025-01-01 01:08:36

在相对较小的应用程序中,您可以共享您的域实体一直到您的表示层,但请注意这引入的耦合。

如果在您的数据绑定中,您除了具有 Address 属性的 Customer 类型实体以及 StreetLine1StreetLine2 属性,则所有层都紧密耦合在一起,一层的更改可能会导致其他层的更改。

因此,您的决定应该基于项目的规模和可以拥有的耦合量。

如果您采用低耦合设计,那么您的 BLL 将使用您的 DAL 来检索实体并使用这些实体来执行行为。然后,BLL 将使用数据传输对象 传递到您的表示层,因此您的表示层之间没有耦合> 和您的领域模型

In a relatively small application, you can share your Domain Entities all the way up to your Presentation layer but be aware of the coupling this introduces.

If in your Databinding you except an Entity of type Customer with a property Address with a StreetLine1 and StreetLine2 property then all your layers are tightly coupled together and a change in one layer will probably cause changes in other layers.

So your decision should be based on the scale of your project and the amount of coupling you can have.

If you go for a low coupled design then your BLL will use your DAL to retrieve entities and use those entities to execute behavior. The BLL will then use Data Transfer Objects to pass to your Presentation layer so there is no coupling between your presentation layer and your Domain Model.

会发光的星星闪亮亮i 2025-01-01 01:08:36

看看我在这里的答案: https://stackoverflow.com/a/7474357/559144 这是通常的方法我做的事情并且工作得很好,不仅对于 MVC 和实体框架...事实上,在 MVC 中,模型可以是一个实体类型,它只具有下层定义的真实业务实体包含的一些字段层,这取决于您是否真的绝对需要 UI 级别中的所有字段,还是只需要某些字段来进行一些数据渲染和输入...

look at my answer here: https://stackoverflow.com/a/7474357/559144  this is the usual way I do things and works well, not only for MVC and Entity Framework... in fact in MVC the model could be an entity type which only has some of the fields contained by the real business entities defined in lower layers, it depends if you really absolutely need all fields in the UI level as well or only some to do some data rendering and input... 

翻身的咸鱼 2025-01-01 01:08:36

作为相关主题,请参阅此相关答案 我最近发布了关于在跨平台客户端/服务器系统中避免代码重复和正确架构的文章。

我已经为该帖子中的其他发帖者 +1,因为这并不是完整的答案,只是与问题相关的有用信息。

谨致问候,

As a related topic, please see this related answer which I posted recently on avoiding duplication of code and correct architecture in a cross-platform client/server system.

I have +1'd the other posters in this thread as this is not intended to be a full answer, just useful information related to the question.

Best regards,

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