by Mikael Henriksson
14. September 2009 02:12
While waiting for my NServiceBus article to finish I have yet another quick post on the Fluent NHibernate and particularly the mapping. I see people do mapping conventions and mapping overrides and struggling with getting the results they want. Part of me want to laugh in their faces for being silly. For me it’s not an option because as Andreas Öhlund said : “is the local dba police on your back” Yup he is so I can’t really have anything auto generated. I simply doubt that many of us have that luxury even. What I did learn however was to sort of connect the two and besides it feels quite nice knowing my mappings will just work. I shortened my mappings a bit with inheritance.
public class PersistentEntityMap<T> :
ClassMap<T> where T : PersistentEntity
{
public PersistentEntityMap()
{
Id(x => x.Id)
.Column(typeof (T).Name.ToLower() + "_id")
.GeneratedBy.Identity();
Map(x => x.CreatedAt)
.Column("created_at")
.Not.Nullable();
Map(x => x.CreatedBy)
.Column("created_BY")
.Not.Nullable();
Map(x => x.LastChangedAt)
.Column("changed_at")
.Nullable();
Map(x => x.LastChangedBy)
.Column("changed_by")
.Nullable();
}
}
In this totally uninteresting project I am working on for myself because I like it I just saved myself from having to repeat myself unless of course someone needs something else. This could be improved further though. I am not completely happy with this solution. Something is missing. I don’t need change tracking for all my persistent entities. Darn it I would really love multiple class inheritance for this but I guess I’ll have to make due with the following:
First a persistent map
public abstract class PersistentEntityMap<T> :
ClassMap<T> where T : PersistentEntity
{
public PersistentEntityMap()
{
Id(x => x.Id)
.Column(typeof(T).Name.ToLower() + "_id")
.GeneratedBy.Identity();
Map(x => x.CreatedAt)
.Column("created_at")
.Not.Nullable();
Map(x => x.CreatedBy)
.Column("created_by")
.Not.Nullable();
}
}
And then a map with change tracking that inherits for the main map that my classes that make use of change tracking could benefit from:
public abstract class PersistantChangeTrackingMap<T> :
PersistentEntityMap<T> where T : PersistentEntity
{
public PersistantChangeTrackingMap()
{
Map(x => x.LastChangedAt)
.Column("changed_at")
.Nullable();
Map(x => x.LastChangedBy)
.Column("changed_by")
.Nullable();
}
}
This might come as a shock for someone but I am used