简体   繁体   中英

User Defined Fields with NHibernate

I need to add a user defined fields feature to an asp.net c# application that uses NHibernate.

The user must be able to add and remove fields from several objects in the system "on the fly", preferably without any system downtime.

One important constraint is that the database schema can't be changed by the user - that is, I can add whatever fields/tables I need to support this feature but when the user adds or removes a field he can't change the database schema.

EDIT: I also have to sort and filter by the values of the user defined fields.

I know how to do it in c#/SQL with a key/value table, but I don't know how to do it with NHibrenate (including filtering and sorting by the user defined fields)

It sounds like you just want to add a name/value properties table.

Have one table defining the name (eg ID, FIELDNAME, DESCRIPTION) and another defining the value (eg ID, NAME_FK, OBJECT_FK, VALUE).

Have the user adding new rows to the NAME table to add a new property and adding values by adding rows to the VALUE table, foreign-keyed to the NAME table and whatever object you want to attach it to.

Your view can then query the VALUE table keyed against the OBJECT_FK and use the NAME_FK to reference the property name.

Edit: NHibernate won't see the new values as actual properties, but if you map them as collections you should be able to query & filter using ICriteria:

IList<MyProp> props = session
  .CreateCriteria(typeof(MyProp))
  .Add(Expression.Eq("ObjectName", "Widget"))
  .Add(Expression.Eq("Name", "Size"))
  .List<MyProp>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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