I want to audit my Posts
table by preserving whole record changes to a PostRevisions
table before Post
rows are updated. PostRevision
entities should store all the Post
entity columns along with a RevisionId
column.
I want to map this with Fluent NHibernate. PostRevision
entities should mirror the properties of Post
entities, but without having to maintain two entity classes and mapping classes.
How should I design my entities and mappings to do achieve this?
Desired pseudocode for Post Edit
var post = _unitOfWork.CurrentSession.Get<Post>(id);
var postRevision = new PostRevision(post);
post.Content = "changed value"; // change some things here
_unitOfWork.CurrentSession.Save(post);
_unitOfWork.CurrentSession.Save(postRevision);
_unitOfWork.Commit();
PostRevision
composition class:
public class PostRevision
{
public virtual Guid Id { get; private set; }
public virtual Post Post { get; set; }
public PostRevision()
{
}
public PostRevision(Post post)
{
this.Post = post;
}
}
Possible Fluent Mapping:
public class PostRevisionMap : ClassMap<PostRevision>
{
public PostRevisionMap()
{
Id(x => x.Id);
Component(x => x.Post); // will something like this work?
}
}