NHibernate add unmapped column in interceptor

我与影子孤独终老i 提交于 2019-12-06 22:06:31

I can't say for sure since I've never actually done this (like Stefan, I also prefer to just add a private property), but can you just add a NHibernate.Type.BooleanType to the types array?

List<IType> typeList = types.ToList();
typeList.Add(new BooleanType());
types = typesList.ToArray();

EDIT

Yes, it looks like you are right; the types have an internal constructor. I did some digging and found TypeFactory:

Applications should use static methods and constants on NHibernate.NHibernateUtil if the default IType is good enough. For example, the TypeFactory should only be used when the String needs to have a length of 300 instead of 255. At this point NHibernate.String does not get you thecorrect IType. Instead use TypeFactory.GetString(300) and keep a local variable that holds a reference to the IType.

So it looks like what you want is NHibernateUtil:

Provides access to the full range of NHibernate built-in types. IType instances may be used to bind values to query parameters. Also a factory for new Blobs and Clobs.

typeList.Add(NHibernateUtil.Boolean);

Personally I wouldn't do it so complicated. I would add the private property and assign it a default value - finished. You could also consider a default value in the database, then you don't need to do anything else.

private virtual bool COM_HOLD 
{ 
  get { return false; } 
  set { /* make NH happy */ } 
}

Before writing a interceptor for that I would consider to write a database trigger. Because with the Interceptor you are "polluting" your data access layer. It could make it unstable and you could have strange problems.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!