Fluent nhibernate 使用fluent nhibernate是否可以自动映射实体内的值对象?
我使用的是Sharp架构,在很多情况下,实体中使用了值对象。下面是一个显而易见的简单示例:Fluent nhibernate 使用fluent nhibernate是否可以自动映射实体内的值对象?,fluent-nhibernate,s#arp-architecture,automapping,Fluent Nhibernate,S#arp Architecture,Automapping,我使用的是Sharp架构,在很多情况下,实体中使用了值对象。下面是一个显而易见的简单示例: public class Person : Entity { protected Person(){} public Person(string personName) { this.PersonName = personName; } public virtual string PersonName { get; protected set;}
public class Person : Entity
{
protected Person(){}
public Person(string personName)
{
this.PersonName = personName;
}
public virtual string PersonName { get; protected set;}
public virtual StreetAddress MailingAddress { get; set; }
}
public class StreetAddress : ValueObject
{
protected StreetAddress(){}
public StreetAddress(string address1, string address2, string city, string state, string postalCode, string country )
{
this.Address1 = address1;
this.Address2 = address2;
this.City = city;
this.State = state;
this.PostalCode = postalCode;
this.Country = country;
}
public virtual string Address1 { get; protected set; }
public virtual string Address2 { get; protected set; }
public virtual string City { get; protected set; }
public virtual string State { get; protected set; }
public virtual string PostalCode { get; protected set; }
public virtual string Country { get; protected set; }
}
当然,这会引发:来自表Person的关联引用了一个未映射的类:Project.Domain.StreetAddress
An association from the table Person refers to an unmapped class: Project.Domain.StreetAddress
因为AutoPersistenceModelGenerator只包含类型为IEntityWithTypedId的类。尚不清楚Sharp架构期望如何实现这种常见条件。是否必须使用大量覆盖来处理此问题?您可能希望将其映射为一个组件。您可以使用Fluent NHibernate中的映射覆盖来完成此操作 您可以将AutoPersistenceModelGenerator中的GetSetup()方法更改为类似以下内容:
private Action<AutoMappingExpressions> GetSetup()
{
return c =>
{
c.IsComponentType = type => type.BaseType == typeof (ValueObject);
};
}
private Action GetSetup()
{
返回c=>
{
c、 IsComponentType=type=>type.BaseType==typeof(ValueObject);
};
}
我会设法把我看到的关于这篇文章的博文发到网上,以获得好评。我同意亚历克的观点。我将把它映射为一个组件 有关这方面的更多信息,请参见此问题: 在这里,您还可以找到有关如何映射复合元素集合的信息