Fluent NHibernate 中的映射修订表

发布于 2024-11-03 04:03:47 字数 1202 浏览 1 评论 0原文

我想通过在更新 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

中性美 2024-11-10 04:03:47

您正在寻找的关系(我认为)是一对多:一个帖子有许多帖子修订。
PostRevision 引用单个帖子。
因此,我认为 Post 端的正确映射是

HasMany(x=> x.PostRevisions);  

PostRevision 端:

References(x=> x.Post).  

请参阅 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

HasMany(x=> x.PostRevisions);  

and on the PostRevision side:

References(x=> x.Post).  

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文