在流利的nHibernate中使用派生类 [英] Use a derived class in fluent nHibernate

查看:137
本文介绍了在流利的nHibernate中使用派生类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个共享公共领域的表。而不是重新映射所有这些,我想有一个共同的领域的基类。对于POCO来说,这很简单:

  class Base 
{
public string commonField {get; set;}
}
类派生:Base
{
public string specificField {get; set;}
}
class OtherDerived:Base
{
public string specificOtherField {get; set;}
}

请注意,没有这样的东西作为基地的表。它只是拥有许多共同的几个表中共享的领域。是的,我知道这是不正常的,但这是我必须处理。

我的问题是 - 是否有一种方法来实现这个流利的nHibernate没有复制映射这些公共属性的代码?

解决方案

您可以继承 ClassMap 来做到这一点。我会做类似以下的事情:

  public class BaseMap< T> :ClassMap< T> T:Base 
{
public BaseMap()
{
Map(x => x.commonField,COMMON_FIELD);
}
}

public class DerivedMap:BaseMap< Derived>
{
public DerivedMap()
{
表(DERIVED_TABLE);
Polymorphism.Explicit(); //你可能想用你的情况。
Map(x => x.DerivedField,DERIVED_FIELD);




$ b $ p $注意$ Polymorphism.Explicit (); 以上。在你的情况下,我想你会想要这个。



http://www.nhforge.org/doc/nh/en/#mapping-declaration-class


I have two tables which share common fields. Rather than re-map all of these I would like to have a base class with the common fields. For POCO this is simple:

class Base
{
   public string commonField {get;set;}
}
class Derived : Base
{
   public string specificField {get;set;}
}
class OtherDerived : Base
{
   public string specificOtherField {get;set;}
}

Note that there is no such thing as a table for "base". It just holds lots of common fields shared among several tables. Yes, I know this is not well normalized, but it's what I have to work with.

My question is - is there a way to implement this in fluent nHibernate without having to duplicate the code that maps those common properties?

解决方案

You can inherit from ClassMap to do this. I would do something like the following:

public class BaseMap<T> : ClassMap<T> where T : Base
{
    public BaseMap()
    {
        Map(x => x.commonField, "COMMON_FIELD");
    }
}

public class DerivedMap : BaseMap<Derived>
{
    public DerivedMap()
    {
        Table("DERIVED_TABLE");
        Polymorphism.Explicit();  //You may want to use this in your case.
        Map(x => x.DerivedField, "DERIVED_FIELD");
    }
}

Notice the Polymorphism.Explicit(); above. In your case I think you are going to want this.

http://www.nhforge.org/doc/nh/en/#mapping-declaration-class

这篇关于在流利的nHibernate中使用派生类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆