在运行时扩展/修改NHibernate的班 [英] Extend/Modify NHibernate classes at runtime

查看:129
本文介绍了在运行时扩展/修改NHibernate的班的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

道歉,如果有一个点上的答案已经在那里,但我还没有找到它。我使用NH3和我已经得到了我想要的添加一组到谁的类实现一个特定的接口的任何实体的用例。我有一个配置生成器类,所以我可以创建会话出厂前进行这些更改

Apologies if there's an on point answer already out there but I haven't found it. I'm using NH3 and I've got a use case where I want to add a Set onto any entity who's class implements a specific interface. I have a configuration builder class so I can make these changes before creating the session factory.

鉴于这种降低,例如:

public class Person : IHasExtraItems
{
    public Person()
    {
        this.ExtraItems = new HashSet<ExtraItem>();
    }
    public virtual Guid Id { get; set; }
    public virtual string Name { get; set; }
    public virtual DateTime Birthdate { get; set; }
    public virtual ICollection<ExtraItem> ExtraItems { get; protected set; }
}

public class ExtraItem
{
    public virtual Guid Id { get; set; }
}

和这个例子映射:

  <class name="Person">
    <id name="Id">
      <generator class="guid"/>
    </id>
    <property name="Name"/>
    <property name="Birthdate"/>
    <set name="Extra" table="PersonExtraItems" cascade="all">
      <key column="PersonId"/>
      <many-to-many column="ExtraItemId" class="ExtraItem" unique="true" />
    </set>
  </class>



因为我希望能够将此功能透明地适用于许多类 - 只要实现该接口 - 我不想把ExtraItem中的映射。相反,我想它在运行时添加。 ?所以,如果我从XML映射删除的财产,我怎么能添加这个在运行时

Since I want to be able to apply this functionality to many classes transparently -- just by implementing the interface -- I don't want to put the "ExtraItem" in the mapping. Instead I want to add it at runtime. So if I remove the property from the xml mapping, how can I add this at runtime?

有这个准确描述的变化,我试图让类型:
http://ayende.com/博客/存档/ 2008/05/01 /动态映射与 - NHibernate.aspx

There's this describing exactly the type of change I'm trying to make: http://ayende.com/Blog/archive/2008/05/01/Dynamic-Mapping-with-NHibernate.aspx

不过,这并不映射多对多集合,我的大脑微弱一直无法破译内存映射表示NHibernate的使用创造的效果。这是我来的基础上,试图让Visible属性在调试器来匹配最接近

But it doesn't map a many to many set, and my feeble brain has been unable to decipher the in-memory mapping representation nhibernate uses to create the effect. This is the closest I've come, based on trying to get properties visible in the debugger to match

foreach (var cls in cfg.ClassMappings)
{
    if (typeof(IHasExtraItems).IsAssignableFrom(cls.MappedClass))
    {
        NHibernate.Mapping.Property property = new NHibernate.Mapping.Property();
        NHibernate.Mapping.Set value = new NHibernate.Mapping.Set(cls);
        value.Role = cls.EntityName + ".ExtraItems";
        value.IsGeneric = true;
        var table = new Table();
        table.Name = cls.MappedClass.Name + "ExtraItems";
        value.CollectionTable = table;
        value.GenericArguments = new Type[] { typeof(ExtraItem) };
        value.IsOptimisticLocked = true;
        value.IsLazy = true;
        mappings.AddCollection(value);
        property.Value = value;

        property.Name = "ExtraItems";
        property.PersistentClass = cls;
        property.Cascade = "all";
        cls.AddProperty(property);
    }
}

在一个测试,这将产生一个运行时错误,因为关键是零,但是XML映射版本的作品,看起来或多或少相同的,我做了改变的时候。

In a test, this produces a runtime error because the key is null, however the XML mapped version works, and looks more or less identical at the time I'm making the changes.

加分:我想一个多到很多具体,因为我想连接表。这让我的实体映射到与真正的性能外键的扩展数据。该ExtraItems真的应该是值类型,而不是真正的实体,但我无法弄清楚如何映射,即使是在XML。

Bonus points: I want a many-to-many specifically because I want a joined table. This lets me map an entity to the extending data with true foreign keys for performance. The ExtraItems should really be a value type rather than a true entity, but I couldn't figure out how to map that, even in XML.

奖励积分第2部分:我能做到这一点与符合?我不想切换我所有现有映射符合,我找不到混合与常规XML映射符合,请不要介意修改现有的映射的一个例子。流利的将是另一种选择,但我使用NH3和我不认为流利的支持呢。

Bonus points, part 2: Could I do this with confORM? I don't want to switch all my existing mappings to confORM, and I couldn't find an example of mixing confORM with conventional XML mappings, nevermind modifying existing mappings. Fluent would be another option, but I'm using NH3 and I don't think Fluent supports that yet.

在此先感谢!

修改我相当肯定,我的问题是,我不定义集合中的元素。但是,我无法分辨如何定义设置不当的元素。

EDIT I'm fairly certain that my issue is that I'm not defining the elements of the set. However, I can't discern how to define the elements of the set properly.

推荐答案

有建立流畅的NH3,所以你可以用它(我),如果你使用的是一口流利的你可以很容易做到这一点有一个约定。你提到你的加分节,所以也许你应该给它再看看?

There are builds of fluent for NH3, so you can use it (I do), and if you are using fluent you can very easily do this with a convention. You mention that in your bonus points section, so maybe you should give it another look?

这篇关于在运行时扩展/修改NHibernate的班的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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