views:

398

answers:

2

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)

+3  A: 

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

Have one table defining the name (e.g. ID, FIELDNAME, DESCRIPTION) and another defining the value (e.g. 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>();
A: 

Hi there, what did you end up using in the end?

kmoo01
The feature was canceled just before I started implementing it
Nir