如果域名实体暴露接口或纯对象? [英] Should Domain Entities be exposed as Interfaces or as Plain Objects?

查看:115
本文介绍了如果域名实体暴露接口或纯对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应该领域实体公开为接口或纯对象

Should Domain Entities be exposed as Interfaces or as Plain Objects ?

用户界面:

public interface IUser
{
    string FirstName { get; set; }
    string LastName { get; set; }
    string Email { get; set; }
    Role Role { get; set; }
}



对用户实行(落实到LinqToSql数据访问层):

The User Implementation (Implemented into LinqToSql Data Access Layer) :

public class User : IUser
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public Role Role { get; set; }
}



对用户实行(落实到NHibernate的数据访问层):

The User Implementation (Implemented into NHibernate Data Access Layer) :

[NHibernate.Mapping.Attributes.Class]
public class User : IUser
{
    [NHibernate.Mapping.Attributes.Property]
    public string FirstName { get; set; }

    [NHibernate.Mapping.Attributes.Property]
    public string LastName { get; set; }

    [NHibernate.Mapping.Attributes.Property]
    public string Email { get; set; }

    [NHibernate.Mapping.Attributes.Property]
    public Role Role { get; set; }
}

这只是说明了一些具体的DAL实现,没有一个更好的样本在这个时候。

this only illustrate some DAL specific implementations, don't have a better sample at this time.

推荐答案

我对这个问题的感觉是,域对象(不是域的实体的,因为这个称号意味着什么与数据库做)不应该是接口,除非你有一个非常令人信服的理由相信,你将需要在未来的某个时刻,支持多种实现。

My feeling on this is that domain objects (not domain entities, as that title implies something to do with a database) should not be interfaces unless you have a very compelling reason to believe that you will need to support multiple implementations at some point in the future.

认为,领域模型是人体模型。业务/服务/文档,从字面上看,域。我们大多数人都为开发一个单一的企业或用途的软件。如果域模型的变化,这是因为业务规则已经改变,因此,旧的域模型不再有效 - 没有理由保持周围的旧人,沿着新的运行

Consider that the domain model is the human model. The business/service/document is, literally, the domain. Most of us are developing software for a single business or purpose. If the domain model changes, it is because the business rules have changed, and therefore the old domain model is no longer valid - there is no reason to keep the old one around, running alongside the new one.

的争论显然不是非黑即白。你可能会开发在很大程度上在多个客户现场定制软件。你可能真的需要在同一时间实现不同组的业务规则,同时有一个真正的需要,以适应他们到一个统一的架构。但是,在我的经验,至少,这些案件是例外而非规则,虽然我一般不喜欢这个词,这可能是在那里你应该考虑到自己的情况下,的YAGNI

The debate is obviously not black-and-white. You might be developing software that is heavily customized at multiple client sites. You might really need to implement different sets of business rules at the same time, and simultaneously have a genuine need to fit them into a unified architecture. But, in my experience at least, these cases are the exception rather than the rule, and although I am not generally fond of the term, this might be a case where you should be thinking to yourself, YAGNI.

数据访问是希望更好的抽象(的persistence无知)。在你的榜样,你有你的模型类NHibernate的属性。但是,增加的持久性的属性使得它不再是一个真正的域类,因为它介绍了NHibernate的依赖。 NHibernate和流利的NHibernate支持映射波苏斯使用外部映射声明而不是在数据类属性,这往往使用的ORM如NHibernate的或EF4当是首选方法,因为它打破了持久化模型和领域模型之间的依赖关系。

Data access is a common area where you want better abstractions (persistence ignorance). In your example, you have NHibernate attributes on your model class. But adding persistence attributes makes it no longer a true domain class because it introduces a dependency on NHibernate. NHibernate and Fluent NHibernate support mapping POCOs using an external mapping declaration instead of attributes on the data class, and this tends to be the preferred approach when using ORMs such as NHibernate or EF4, because it breaks the dependency between persistence model and domain model.

如果不支持这些映射方法,你的中有的使用属性,那么我可能确实建议使用接口代替,但今天的ORM是比这更复杂,使用反射和动态代理和方法拦截做最繁重的工作,这样你就不需要在这里创建自己的抽象。

If these mapping methods weren't supported, and you had to use attributes, then I might indeed suggest using interfaces instead, but ORMs today are more sophisticated than that, using reflection and dynamic proxies and method interception to do most of the heavy lifting, so you don't need to create your own abstractions here.

一些你的对象的类型的希望公开为接口包括:

Some types of objects that you would want to expose as interfaces are:


  • 存储库,它负责装载/节能领域对象;

  • 插件/扩展程序;

  • 查看/演示模式,让不同的用户界面,可插入;

  • 抽象数据类型有许多实现(数组,列表,字典,记录集和数据表中的所有序列AKA 的IEnumerable );

  • 有很多可能的算法(排序,搜索,比较)摘要操作;

  • 通讯模式(通过TCP / IP相同的操作,命名管道,RS-232)

  • 任何特定于平台的,如果你计划部署到多个键(Mac / Windows的/ * nix中)

  • Repositories, which are responsible for loading/saving domain objects;
  • Plugins/extensions to your program;
  • View/presenter models, so that different UIs can be plugged in;
  • Abstract data types with many implementations (array, list, dictionary, record set, and data table are all sequences AKA IEnumerable);
  • Abstract operations with many possible algorithms (sort, search, compare);
  • Communication models (same operations over TCP/IP, named pipes, RS-232);
  • Anything platform-specific, if you plan to deploy to more than one (Mac/Windows/*nix).

这决不是一个完整的清单,但它应该在这里阐明的基本原则,这是最适合的事情接口抽象的东西是:

That's by no means a complete list but it should illuminate the basic principle here, which is that the things best-suited to interface abstractions are the things that:


  1. 取决于有可能超出你的控制的因素;

  2. 有可能在未来改变;和

  3. 是水平的功能(在app /架构的很多地方使用)。

域类将被广泛使用,但不适合进入第一两类;它不会改变,你必须在设计几乎完全控制。因此,除非类本身会采取间接依赖关系(这是一个情况下,你应该尽量避免只要有可能),我不会去通过在域模型中的每个类创建接口的额外的努力。

A domain class will be widely used, but does not fit into either of the first two categories; it is not likely to change, and you have almost complete control over the design. Therefore, unless the classes themselves will be taking on indirect dependencies (which is a situation you should try to avoid whenever possible), I would not go through the extra effort of creating an interface for every class in the domain model.

这篇关于如果域名实体暴露接口或纯对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆