我的目标是允许我的域模型(通过EF持久化)包含一些逻辑.
开箱即用,实体框架对于如何将实体添加到图形然后保持不变非常不受限制.
举例来说,我的域名为POCO(没有逻辑):
public class Organization { private ICollection<Person> _people = new List<Person>(); public int ID { get; set; } public string CompanyName { get; set; } public virtual ICollection<Person> People { get { return _people; } protected set { _people = value; } } } public class Person { public int ID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public virtual Organization Organization { get; protected set; } } public class OrganizationConfiguration : EntityTypeConfiguration<Organization> { public OrganizationConfiguration() { HasMany(o => o.People).Withrequired(p => p.Organization); //.Map(m => m.MapKey("OrganizationID")); } } public class PersonConfiguration : EntityTypeConfiguration<Person> { public PersonConfiguration() { Hasrequired(p => p.Organization).WithMany(o => o.People); //.Map(m => m.MapKey("OrganizationID")); } } public class MyDbContext : DbContext { public MyDbContext() : base(@"Data Source=(localdb)\v11.0;Initial Catalog=stackoverflow;Integrated Security=true") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Configurations.Add(new PersonConfiguration()); modelBuilder.Configurations.Add(new OrganizationConfiguration()); } public IDbSet<Organization> Organizations { get; set; } public IDbSet<Person> People { get; set; } }
我的示例域名是组织可以有很多人.一个人只能属于一个组织.
创建组织并向其添加人员非常简单:
using (var context = new MyDbContext()) { var organization = new Organization { CompanyName = "Matthew's Widget Factory" }; organization.People.Add(new Person {FirstName = "Steve",LastName = "McQueen"}); organization.People.Add(new Person {FirstName = "Bob",LastName = "Marley"}); organization.People.Add(new Person {FirstName = "Bob",LastName = "Dylan" }); organization.People.Add(new Person {FirstName = "Jennifer",LastName = "Lawrence" }); context.Organizations.Add(organization); context.SaveChanges(); }
我的测试查询是.
var organizationsWithSteve = context.Organizations.Where(o => o.People.Any(p => p.FirstName == "Steve"));
上面的类布局不符合域的工作方式.例如,所有人都属于组织为组合根的组织.能够做context.People.Add(…)并没有意义,因为这不是域的工作方式.
如果我们想要在组织模型中添加一些逻辑来限制该组织中可以有多少人,我们就可以实现一种方法.
public Person AddPerson(string firstName,string lastName) { if (People.Count() >= 5) { throw new InvalidOperationException("Your organization already at max capacity"); } var person = new Person(firstName,lastName); this.People.Add(person); return person; }
但是,对于类的当前布局,我可以通过调用organization.Persons.Add(…)来绕过AddPerson逻辑,或者通过执行context.Persons.Add(…)来完全忽略聚合根,这两者都不是我的想做.
我提出的解决方案(不起作用,也就是我在这里发布的原因)是:
public class Organization { private List<Person> _people = new List<Person>(); // ... protected virtual List<Person> WritablePeople { get { return _people; } set { _people = value; } } public virtual IReadOnlyCollection<Person> People { get { return People.AsReadOnly(); } } public void AddPerson(string firstName,string lastName) { // do domain logic / validation WriteablePeople.Add(...); } }
这不适用于映射代码HasMany(o => o.People).Withrequired(p => p.Organization);不编译,因为HasMany期望ICollection< TEntity>而不是IReadOnlyCollection.我可以暴露ICollection本身,但我想避免使用Add / Remove方法.
我可以“忽略”People属性,但我仍然希望能够针对它编写Linq查询.
我的第二个问题是我不希望我的上下文暴露直接添加/删除人的可能性.
在上下文中我想要:
public IQueryable<Person> People { get; set; }
但是,即使IDbSet实现了IQueryable,EF也不会填充我的上下文的People属性.我能想到的唯一解决方案就是在MyDbContext上编写一个外观,它暴露了我想要的功能.对于只读数据集来说,似乎过度杀伤和大量维护.
如何在使用Entity Framework时实现干净的DDD模型?
编辑
我正在使用Entity-Framework v5
解决方法
我建议另一种方法,一个完全干净的域模型和一个单独的持久性模型在较低层.您可能需要在这两者之间使用转换机制,AutoMapper会很好.
这将完全解除您的顾虑.没有办法“削减”只是因为EF使事情变得必要并且域层不能提供上下文,因为它只是来自“另一个世界”,它不属于域.
我已经看到人们制作部分模型(又名“有界背景”)或只是创建一个普通的EF poco结构并假装这个IS DDD,但它可能不是,你的担忧正好在头脑中.