Fluenthibernate映射继承/子类
问题描述:
我是新来的(流利)NHibernate和有一个问题:Fluenthibernate映射继承/子类
我下面的类结构:
<!-- language: c# -->
public class JAccount
{
public virtual long Id { get; private set; }
//some properties
public virtual JProfile Profile { get; set; }
}
public class JProfile
{
public virtual long Id { get; set; }
//some more properties
public virtual int Age { get; set; }
}
我映射后就正常了这些类..
<!-- language: c# -->
public class JProfileMap : ClassMap<JProfile>
{
public JProfileMap()
{
Id(x => x.Id).Unique().GeneratedBy.Assigned();
//some more property mappings
}
}
然后我想,我在这些实体类中有很多逻辑(方法),我想为它们中的每一个都创建一个新的类,并从基类继承并将所有逻辑存储到它中。 所以,我不喜欢的东西:
<!-- language: c# -->
public class TestAccount : JAccount
{
//all properties which did not need to be mapped into the DB
//as well as methods
}
//did the same for JProfile
问题是现在,当我加载从数据库中的一个JAccount和我丢在TestAccount 并试图挽救回来,我因为FluentHibernate没有得到一个异常映射为TestAcccount
我的问题是现在:我真的需要SubClassMap这个新类吗?我不想/需要将它们映射到数据库中,因为他们所做的只是从其基类中取出logik。是否有不同的方式来完成这个?
感谢所有帮助
答
对象的类型是应存储在数据库中的重要信息。这是为什么有一个SubclassMapping的最重要的原因。相反,继承JAccount的
您可以使用策略模式,以单独的逻辑
public class JAccount
{
private AcountType _type;
public JAccount(AcountType type)
{
_type = type;
}
public virtual long Id { get; private set; }
//some properties
public virtual JProfile Profile { get; set; }
public void Register()
{
_type.Register(this);
}
}
// in JAccountMap
References(Reveal.Member<JAccount>("_type")).Access.Field().Not.LazyLoad();
// or
Map(Reveal.Member<JAccount>("_type"), "type").CustomType<AccountTypeToStringUserType>();
它是不可能有实例化一个'JAccount'并转换为'TestAccount'。你能澄清你在这里做什么吗? – Jay 2012-03-02 01:42:22
就像我说过的,我想保留映射的基类尽可能简单。有JUST属性,另一个类从这个基类继承,实现所有其他的东西像方法..所以我有'TestAccount'类继承自'JAccount'与方法像'Register()',但这些类没有映射,因为他们没有需要存储在数据库中的属性/信息,它们只用方法扩展基类......但是当我尝试更新/保存并且实例化这些类时,我从Fluenthibernate中得到错误:( – Thomas 2012-03-02 03:26:43
你可以以相反的方式做到这一点,告诉nhibernate忽略基类例如'Profile:Entity'告诉nhibernate忽略'Entity',但是有没有办法去做oposite,忽略profile和save/cast到实体? = D – Thomas 2012-03-02 03:34:07