应该将域实体作为接口还是作为普通对象公开?域应该以接口还是普通对象的形式公开?
的用户界面:
public interface IUser
{
string FirstName { get; set; }
string LastName { get; set; }
string Email { get; set; }
Role Role { get; set; }
}
用户执行情况(实施到LinqToSql数据访问层):
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的数据访问层):
[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特定的实现,没有更好的样品在这个时候。
@Aaronaught:好的ORM具体,现在假设我使用FullText功能,我会使用Lucene.NET也使用属性。据我所知,没有办法像FluentNhibernate那样使用外部属性映射。那么,为了让我的域名实体成为真正的POCO,最好的方法是什么?也许接口以这种方式工作? –
@Yannn。 B:我不知道Lucene.NET完全使用装饰器属性;它绝对不需要它们。如果你想使用它们,那么你有两种选择:(a)让你的领域模型依赖于Lucene.NET,这似乎是一个坏主意(如果你想使用SQL FTS呢?如果将来的版本是Lucene.NET确实支持POCOs?),或者(b)将你的搜索对象和存储库移动到不同的命名空间/程序集,并使用像AutoMapper这样的工具将它们转换为域对象。无论哪种方式,我不认为为对象创建接口会有很大帮助。 – Aaronaught
@Aaronaught:你说Lucene.NET不需要属性?它关于我发布的另一个问题(http://stackoverflow.com/questions/2356593/lucene-net-and-poco-entities),但你怎么做,而不使用Lucene.NET的属性? –