it's hard for me to explain, so I'll start with an example.
I'm beggining to implement an object changing history in my project. I want to track date of the change, the changer, the object changed, etc. and persist that info in my DB.
I designed a table similar to this (fits my needs the most):
Table CHANGES
change_id (pk) | change_date | changer_id (fk) | object_id | table_name | column_name
with an exemplary rows
120 | 2013-11-20 | 55 | 88 | "invoices" | "number"
121 | 2013-11-25 | 53 | 99 | "employees" | "name"
Now, i would like to have this kind of entity objects:
class Employee
{
public virtual string Name {get; set;}
public virtual IList<Changes> ChangesList {get; set;}
}
class Invoice
{
public virtual string Number {get; set;}
public virtual IList<Changes> ChangesList {get; set;}
}
The ChangesList from both Invoice and Employee should come from the same table in DB of course. Is it possible and how to achieve this by mapping? Are there any alternatives/minor changes to make this possible?
Before you invent your own take a look at NHibernate.Envers which handles auditing.
If it does not fit your needs it is possible to map it like
public ChangeMap
{
Id(x => x.Id);
Map(x => x.Date, "change_date");
References(x => x.Changer, "changer_id");
...
Map(x => x.TableName, "table_name");
}
// in EmployeeMap
HasMany(x => x.ChangesList).Where("table_name = 'employees'");
// in InvoiceMap
HasMany(x => x.ChangesList).Where("table_name = 'invoices'");