Background
I use an outdated database with all kinds of ugly corners. One bit is auditing. There is a table that lists combinations of tablename / field fields that should have an audit trail. For example, if there is a row that has “WORKORDER” for the table name and “STATUS” for the field name, then I need to add rows (rows) to the audit table whenever the Workorder.Status property changes in the application. I know the approach: NH events or interceptors, but I have a problem to figure out before I get to this stage.
Question
I need to know how to get a list of key / value pairs for one constant class that contains (a) the name of the database field and (b) the name of the property associated with it in the class. So for my example, I have a class called Workorder that is associated with the (not surprisingly) WORKORDER table. I have a property in this Workorder class called CurrentStatus. The matching property in the WORKORDER table is STATUS. Notice the mismatch between the property name and the column name of the table? I need to know the name of the property to access data before and after the audit. But I also need to know the name of the support column so that I can query the dumb table "Audit TheseColumns".
What i tried
in my application, I change Workorder.CurrentStatus from "TS" to "IP". I look at the audit tracking table and see that the WORKORDER.STATUS column is being tracked. Therefore, after calling Session.SaveOrUpdate (workorder), I need to find the Workorder property associated with the STATUS column and execute Session.Save (auditRecord), telling it the old ("TS") and new ("IP") values.
As far as I can tell, you can get class information:
var fieldNames = new List<string>(); IClassMetadata classMetadata = SessionFactory(Resources.CityworksDatasource).GetClassMetadata(typeof(T)); int propertyCount = 0; foreach (IType propertyType in classMetadata.PropertyTypes) { if (propertyType.IsComponentType) { var cp = (ComponentType)propertyType; foreach (string propertyName in cp.PropertyNames) { fieldNames.Add(propertyName); } } else if(!propertyType.IsCollectionType) { fieldNames.Add(classMetadata.PropertyNames[propertyCount + 1]); } propertyCount++; }
And table information:
var columnNames = new List<string>(); PersistentClass mappingMeta = ConfigureCityworks().GetClassMapping(typeof(T)); foreach (Property property in mappingMeta.PropertyIterator) { foreach (Column selectable in property.ColumnIterator) { if (columnNames.Contains(selectable.Name)) continue; columnNames.Add(selectable.Name); } }
But not at the same time. Any ideas? I’m at a loss where to look further.
nhibernate metadata
Dylan
source share