强类型映射。基于Lambda表达式的ORM

时间:2009-12-28 13:51:37

标签: c# .net lambda domain-driven-design

您如何看待域实体的下表映射样式?

class Customer {
 public string Name;
}

class Order
{
 public TotallyContainedIn<Customer> Parent { get { return null; } }
 public HasReferenceTo<Person> SalesPerson { get { return new HasReferenceTo<Person>(0,1); } }
}


//...

[TableOf(typeof(Customer))]
class CustomerTable
{
 //...
 public Mapping Name { get { return Column.Implements<Customer>(c=>c.Name); } }
}

[TableOf(typeof(Order))]
class OrderTable
{
 //...
 public FK CustomerID { get { return References.FK<CustomerTable>(ct=>ct.Id;); } }
}

我正在努力实现的目标是让我的域模型准备好在我输入和编译时编写代码,而不需要任何代码生成例程和依赖于任何xml工件,并具有强大的能力参考我正在使用的所有内容。

无论如何实施,您认为以这种方式使用它会很容易吗?

1 个答案:

答案 0 :(得分:2)

对于NHibernate来说,

FluentNHibernate几乎一样:

public class CatMap : ClassMap<Cat>
{
  public CatMap()
  {
    Id(x => x.Id);
    Map(x => x.Name)
      .Length(16)
      .Not.Nullable();
    Map(x => x.Sex);
    References(x => x.Mate);
    HasMany(x => x.Kittens);
  }
}

此外,它支持所谓的自动化:

var autoMappings = AutoPersistenceModel  
  .MapEntitiesFromAssemblyOf<Product>()  
  .Where(t => t.Namespace == "Storefront.Entities");  

var sessionFactory = new Configuration()  
  .AddProperty(ConnectionString, ApplicationConnectionString)  
  .AddAutoMappings(autoMappings)  
  .BuildSessionFactory();