Fluent NHibernate 中的映射修订表
我想通过在更新 Post
行之前保留对 PostRevisions
表的整个记录更改来审核我的 Posts
表。 PostRevision
实体应存储所有 Post
实体列以及 RevisionId
列。
我想用 Fluent NHibernate 来映射它。 PostRevision
实体应该镜像 Post
实体的属性,但不必维护两个实体类和映射类。
我应该如何设计我的实体和映射来实现这一目标?
所需的伪代码
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();
Post Edit PostRevision
组合类
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;
}
}
:可能的流畅映射:
public class PostRevisionMap : ClassMap<PostRevision>
{
public PostRevisionMap()
{
Id(x => x.Id);
Component(x => x.Post); // will something like this work?
}
}
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?
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在寻找的关系(我认为)是一对多:一个帖子有许多帖子修订。
PostRevision 引用单个帖子。
因此,我认为 Post 端的正确映射是
PostRevision 端:
请参阅 nHibernate 文档 更完整地了解如何映射这些关联。
编辑
如果您想保留帖子的每次修订历史记录,您有 2 个选择:
1. 将布尔值“IsHistoric”字段添加到您的 Post 类中。每当修改 Post 时,您都不会更改 Post 对象本身,而是将其标记为“IsHistoric = true”并创建一个新的 Post 对象来表示修改后的 Post。这是我在项目中使用的方法。
2. 创建一个继承自Post 的“HistoricPost”类。您不必重复映射,并且可以为此类使用单独的表(每个子类一个表策略)。
请参阅此处了解更多详细信息。
我认为您还可以在“HistoricPost”的映射中使用
Id(x => x.SomeOtherId);
为子类指定不同的 Id 列。我还没有尝试过,所以我不能100%确定。the relation (I think) you're looking for is one-to-many: a Post has many PostRevisions.
a PostRevision references a single Post.
Therefore I think the correct mapping on the Post side would be
and on the PostRevision side:
see the nHibernate docs for a more complete look on how to map these associations.
Edit
If you want to keep a history record for each revision of your post, you have 2 options:
1. add a boolean 'IsHistoric' field to your Post class. Whenever a Post is revised, you don't change the Post object itself, but rather mark it as 'IsHistoric = true' and create a new Post object which represents the revised Post. This is the method I use in my project.
2. Create a 'HistoricPost' class which inherits from Post. You don't have to repeat your mapping, and you can use a seperate table for this class (table-per-subclass strategy).
see here for further details.
I think you can also specify a different Id column for the child class, using
Id(x => x.SomeOtherId);
in the mapping of 'HistoricPost'. I haven't tried it though, so I'm not 100% sure.