.NET N 层架构:我该如何处理模型对象?
我正在使用 ASP.NET Web 表单 C# 从头开始创建一个解决方案。
我担心模型对象,因为我不想在每一层中创建重复的模型对象集。在 Web 表单
中使用 3 层架构中的模型对象的最佳实践是什么?
我想到的结构如下:
- UI
- BLL
- DAL
- 模型
该模型将包含可在层的每个部分中使用的所有模型类。我认为这会很有用,因为每一层都需要访问模型对象。例如:
- UI调用BLL中的方法,传入填充数据的模型对象。
- 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:
- UI calls a method in BLL passing in a model object filled with data.
- BLL calls a method in DAL passing through the object which is saved
in the database etc.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
模型可以是层的横切关注,这是一种快速的方法。或者,您可以为模型创建接口,这样您就可以简单地用 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.
在相对较小的应用程序中,您可以共享您的
域实体
一直到您的表示层
,但请注意这引入的耦合。如果在您的数据绑定中,您除了具有
Address
属性的Customer
类型实体以及StreetLine1
和StreetLine2
属性,则所有层都紧密耦合在一起,一层的更改可能会导致其他层的更改。因此,您的决定应该基于项目的规模和可以拥有的耦合量。
如果您采用低耦合设计,那么您的
BLL
将使用您的DAL
来检索实体并使用这些实体来执行行为。然后,BLL
将使用数据传输对象
传递到您的表示层
,因此您的表示层
之间没有耦合> 和您的领域模型
。In a relatively small application, you can share your
Domain Entities
all the way up to yourPresentation layer
but be aware of the coupling this introduces.If in your Databinding you except an Entity of type
Customer
with a propertyAddress
with aStreetLine1
andStreetLine2
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 yourDAL
to retrieve entities and use those entities to execute behavior. TheBLL
will then useData Transfer Objects
to pass to yourPresentation layer
so there is no coupling between yourpresentation layer
and yourDomain Model
.看看我在这里的答案: 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...
作为相关主题,请参阅此相关答案 我最近发布了关于在跨平台客户端/服务器系统中避免代码重复和正确架构的文章。
我已经为该帖子中的其他发帖者 +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,