简体   繁体   English

NHibernate IInterceptor实现(向原始域类没有的DB表添加属性)

[英]NHibernate IInterceptor implementation(add properties to DB table that original domain class doesn't have)

How is possible to set some special column values when update/insert entities via NHibernate without extending domain classes with special properties? 当通过NHibernate更新/插入实体而不扩展具有特殊属性的域类时,如何设置一些特殊的列值?

For example: in my case I would like to get object and just moment before update/insert db add to that object some additional information (like user id or computer name) by using IInterceptor. 例如:在我的情况下,我希望获得对象,并在更新/插入db之前,通过使用IInterceptor向该对象添加一些其他信息(如用户ID或计算机名称)。 In other words I would like to add a few columns to DB Table without specifying new properties in original object's class. 换句话说,我想在DB Table中添加几列而不在原始对象的类中指定新属性。 Do I have to configure/change/add to my Object.hbm.xml or App.config in that case? 在这种情况下,我是否必须配置/更改/添加到我的Object.hbm.xml或App.config?

The problem is that I can't change my original objects and base classes.So I have to figure out if possible to add information to DB table without changing of the original objects (even no inherit from any base classes) 问题是我无法更改我的原始对象和基类。所以我必须弄清楚是否有可能在不更改原始对象的情况下向DB表添加信息(甚至没有从任何基类继承)

Example: 例:

Original Object has : FirstName ,LastName,Birthday,Address properties 原始对象具有: FirstName ,LastName,Birthday,Address properties

Customer.hbm.xml has: Customer.hbm.xml具有:

<property name="FirstName" column="FirstName" type="string" not-null="true" length="64" />
<property name="LastName" column="LastName" type="string" not-null="true" length="64" />
<property name="Birthday" column="Birthday" type="DateTime" not-null="true"  />
<property name="Address" column="Address" type="string" not-null="true"  />

My Interceptor class has method: 我的Interceptor类有方法:

public bool OnSave(object entity, object id, object[] state, string[] propertyNames, NHibernate.Type.IType[] types)

at that moment or even maybe before save I have to add to the DB Customer table additional 2 columns (Computer Name and User Name for example ) that propertyNames[] and state[] parameters don't have them from the beginning,so that should be done on the fly. 在那一刻甚至可能在保存之前我必须向DB Customer表添加另外2列(例如计算机名和用户名),即propertyNames[]state[]参数从一开始就没有它们,所以应该在飞行中完成。

MY DB Customer table has all the columns that I have described above. MY DB Customer表包含我上面描述的所有列。

Thank you. 谢谢。

I haven't tried it, but you should be able to add the dynamic properties to the Configuration prior to factory creation. 我没有尝试过,但您应该能够在创建工厂之前将动态属性添加到配置中。 Then you can populate those values in your interceptor. 然后,您可以在拦截器中填充这些值。

Ex, retrieve the relevant PersistentClass from config.ClassMappings, then add your properties to it. 例如,从config.ClassMappings中检索相关的PersistentClass,然后将属性添加到它。

private void AddProperty(
        PersistentClass pc, 
        string propertyName, 
        string columnName, 
        IType dataType)
    {
        SimpleValue val = new SimpleValue(pc.Table);
        Column col = new Column(dataType, 0);
        col.Name = columnName;
        val.AddColumn(col);
        val.Type = dataType;
        Property prop = new Property(val);
        prop.IsUpdateable = true;
        prop.Name = propertyName;
        prop.PropertyAccessorName = "nosetter.camelcase";
        prop.Cascade = "none";
        pc.AddNewProperty(prop);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM