I am looking to implement the Onion Architecture into our ASP.NET MVC application. I understand the need to separate View Models from Domain Entities, however I am finding myself writing redundant code. There is redundant code because my view models and domain entities look exactly the same with the exception that my view models have the [Serializable] data annotation. I need these models serializable because I am using ASP.NET Session State in which the State Server needs objects to be serializable.
我希望将Onion Architecture实现到我们的ASP.NET MVC应用程序中。我理解将视图模型与域实体分开的必要性,但我发现自己正在编写冗余代码。存在冗余代码,因为我的视图模型和域实体看起来完全相同,除了我的视图模型具有[Serializable]数据注释。我需要这些模型可序列化,因为我使用的是ASP.NET会话状态,其中State Server需要可序列化的对象。
I personally feel the domain entities should NOT be serializable because it would then become dependent on a particular technology. However, how can I avoid redundant code?
我个人觉得域实体不应该是可序列化的,因为它会依赖于特定的技术。但是,如何避免冗余代码?
I should add that my service methods are dependent on these serializable data models.
我应该补充一点,我的服务方法依赖于这些可序列化的数据模型。
3 个解决方案
#1
6
I would avoid annotating my domain objects with anything persistence or non-domain related. This way, my Domain project wouldn't depend on another layer and I won't have it cluttered with things that aren't relevant to the Domain. While we need to bend the rules, I prefer bending them in a way not involving dependency on a persistence detail.
我会避免使用任何持久性或非域相关的东西来注释我的域对象。这样,我的Domain项目就不会依赖于另一层了,我也不会把它与那些与Domain无关的东西弄得乱七八糟。虽然我们需要弯曲规则,但我更喜欢以不依赖于持久性细节的方式弯曲它们。
The point is to keep the layers focused on their purpose because it's very easy to mix'em up and create (in time) the big ball of mud.
关键是要保持层的注意力集中在它们的目的上,因为它很容易混合起来并创造(及时)大泥球。
In your case, I have the feeling you don't really have a rich domain or it's improperly modeled. It seems you only have data structures and your needs are CRUDy.
在你的情况下,我觉得你并没有真正拥有丰富的域名或者它是不正确的建模。看起来你只有数据结构,你的需求是CRUDy。
If you are certain the app won't evolve to become more complex i.e it will be just data structure manipulations then you can have one model to use them for all the purposes. Basically you can cut corners and use the 'business' model for everything. No need for abstractions and other stuff.
如果您确定应用程序不会变得更加复杂,即只是数据结构操作,那么您可以使用一个模型将它们用于所有目的。基本上你可以偷工减料,并将“商业”模式用于一切。不需要抽象和其他东西。
But if you think the app will evolve or they are or will be business rules and processes i.e you'll need to model behaviour as perceived by the business, then it's best to keep things very decoupled, even if at this stage they seem to be identical.
但是如果你认为应用程序会发展,或者它们是或将成为业务规则和流程,即你需要对业务所感知的行为进行建模,那么最好保持事物非常分离,即使在这个阶段他们似乎是相同。
To make it easier, for your view model you can define one (with copy paste) and use automapper to map the business object to the view model one. Other approach maybe that your query service/repository/object could return directly that view model (map the query result to the view model)
为了简化操作,您可以为视图模型定义一个(使用复制粘贴)并使用automapper将业务对象映射到视图模型。其他方法可能是您的查询服务/存储库/对象可以直接返回该视图模型(将查询结果映射到视图模型)
#2
0
Viewmodels can contain domain entities/models. My domain entities are partial classes and all (eventually) inherit from a base entity which is serialized. Since I use my domain models within some of my viewmodels, I use data annotations on the domain models as well. Your domain model library should not depend on/reference anything else (domain driven).
Viewmodels可以包含域实体/模型。我的域实体是部分类,并且所有(最终)都从序列化的基本实体继承。由于我在一些视图模型中使用了我的域模型,因此我也在域模型上使用数据注释。您的域模型库不应依赖/引用其他任何内容(域驱动)。
#3
0
I wouldn't call [Serializable]
a data annotation per se, since it's part of the core .Net platform (mscorlib.dll). Data Annotations refers to specific attributes used to perform data validation and other operations in ASP.Net or Entity Framework.
我不会将[Serializable]本身称为数据注释,因为它是核心.Net平台(mscorlib.dll)的一部分。数据注释是指用于在ASP.Net或实体框架中执行数据验证和其他操作的特定属性。
Should an Entity be [Serializable]
? Probably not, but I don't think this attribute is as "adulterating" to your domain layer as other attributes that come from external, web- or persistence-oriented libraries. It doesn't tie you to a particular third-party framework.
实体是否应该[可序列化]?可能不是,但我不认为这个属性与来自外部,面向Web或面向持久性的库的其他属性一样“掺杂”到您的域层。它不会将您绑定到特定的第三方框架。
The whole "redundant code" issue depends IMO on the type of system you're building. In a genuine DDD application, duplication between Domain entities and View Models will probably not be all that blatant, and the benefits of a separate presentation model will typically outweigh the costs. A good article on that subject : Is Layering Worth The Mapping ?
整个“冗余代码”问题取决于IMO您正在构建的系统类型。在真正的DDD应用程序中,域实体和视图模型之间的重复可能不是那么明显,并且单独的表示模型的好处通常会超过成本。一篇关于这个主题的好文章:分层值得映射吗?
#1
6
I would avoid annotating my domain objects with anything persistence or non-domain related. This way, my Domain project wouldn't depend on another layer and I won't have it cluttered with things that aren't relevant to the Domain. While we need to bend the rules, I prefer bending them in a way not involving dependency on a persistence detail.
我会避免使用任何持久性或非域相关的东西来注释我的域对象。这样,我的Domain项目就不会依赖于另一层了,我也不会把它与那些与Domain无关的东西弄得乱七八糟。虽然我们需要弯曲规则,但我更喜欢以不依赖于持久性细节的方式弯曲它们。
The point is to keep the layers focused on their purpose because it's very easy to mix'em up and create (in time) the big ball of mud.
关键是要保持层的注意力集中在它们的目的上,因为它很容易混合起来并创造(及时)大泥球。
In your case, I have the feeling you don't really have a rich domain or it's improperly modeled. It seems you only have data structures and your needs are CRUDy.
在你的情况下,我觉得你并没有真正拥有丰富的域名或者它是不正确的建模。看起来你只有数据结构,你的需求是CRUDy。
If you are certain the app won't evolve to become more complex i.e it will be just data structure manipulations then you can have one model to use them for all the purposes. Basically you can cut corners and use the 'business' model for everything. No need for abstractions and other stuff.
如果您确定应用程序不会变得更加复杂,即只是数据结构操作,那么您可以使用一个模型将它们用于所有目的。基本上你可以偷工减料,并将“商业”模式用于一切。不需要抽象和其他东西。
But if you think the app will evolve or they are or will be business rules and processes i.e you'll need to model behaviour as perceived by the business, then it's best to keep things very decoupled, even if at this stage they seem to be identical.
但是如果你认为应用程序会发展,或者它们是或将成为业务规则和流程,即你需要对业务所感知的行为进行建模,那么最好保持事物非常分离,即使在这个阶段他们似乎是相同。
To make it easier, for your view model you can define one (with copy paste) and use automapper to map the business object to the view model one. Other approach maybe that your query service/repository/object could return directly that view model (map the query result to the view model)
为了简化操作,您可以为视图模型定义一个(使用复制粘贴)并使用automapper将业务对象映射到视图模型。其他方法可能是您的查询服务/存储库/对象可以直接返回该视图模型(将查询结果映射到视图模型)
#2
0
Viewmodels can contain domain entities/models. My domain entities are partial classes and all (eventually) inherit from a base entity which is serialized. Since I use my domain models within some of my viewmodels, I use data annotations on the domain models as well. Your domain model library should not depend on/reference anything else (domain driven).
Viewmodels可以包含域实体/模型。我的域实体是部分类,并且所有(最终)都从序列化的基本实体继承。由于我在一些视图模型中使用了我的域模型,因此我也在域模型上使用数据注释。您的域模型库不应依赖/引用其他任何内容(域驱动)。
#3
0
I wouldn't call [Serializable]
a data annotation per se, since it's part of the core .Net platform (mscorlib.dll). Data Annotations refers to specific attributes used to perform data validation and other operations in ASP.Net or Entity Framework.
我不会将[Serializable]本身称为数据注释,因为它是核心.Net平台(mscorlib.dll)的一部分。数据注释是指用于在ASP.Net或实体框架中执行数据验证和其他操作的特定属性。
Should an Entity be [Serializable]
? Probably not, but I don't think this attribute is as "adulterating" to your domain layer as other attributes that come from external, web- or persistence-oriented libraries. It doesn't tie you to a particular third-party framework.
实体是否应该[可序列化]?可能不是,但我不认为这个属性与来自外部,面向Web或面向持久性的库的其他属性一样“掺杂”到您的域层。它不会将您绑定到特定的第三方框架。
The whole "redundant code" issue depends IMO on the type of system you're building. In a genuine DDD application, duplication between Domain entities and View Models will probably not be all that blatant, and the benefits of a separate presentation model will typically outweigh the costs. A good article on that subject : Is Layering Worth The Mapping ?
整个“冗余代码”问题取决于IMO您正在构建的系统类型。在真正的DDD应用程序中,域实体和视图模型之间的重复可能不是那么明显,并且单独的表示模型的好处通常会超过成本。一篇关于这个主题的好文章:分层值得映射吗?