NHibernate 审计和继承类的问题

发布于 2024-09-15 02:53:47 字数 2043 浏览 1 评论 0原文

我有一个实现 IDomainObjectAnimal 类。还有另一个类 Cat,它继承自 Animal。我对 NHibernate 映射使用 Table Per Subclass 继承策略,并将 CreatedDateLastModified 属性映射到 Animal 表中的列以及Cat 表中的列。

我还使用 PreUpdate 事件处理程序为 LastModified 分配一个值。

当我更新 Cat 对象中的属性时,两个 UPDATES 会发送到数据库:一个用于 Animal,另一个用于 Cat。但是,Animal 的 UPDATE 会获取新的 LastModified 值,而 Cat 的 UPDATE 会保留旧值。

当在另一个会话中再次检索 Cat 实体时,这会导致一些问题。似乎 LastModified 被新值(来自 Animal)覆盖,有效地标记实体已脏,并触发另一个 UPDATE。

我的整体设计似乎有问题,但我不确定它是什么。有什么更好的方法来解决这个问题?

public class Animal : IDomainObject {
    // ... various properties

    public virtual DateTime CreatedDate { get; set; }
    public virtual DateTime LastModified { get; set; }
    public virtual int Version { get; protected set; }
}

public class Cat : Animal {
    // ... various properties

    public override DateTime CreatedDate { get; set; }
    public override DateTime LastModified { get; set; }
    public override int Version { get; protected set; }
}

public interface IDomainObject {
    DateTime CreatedDate { get; set; }

    DateTime LastModified { get; set; }

    int Version { get; }
}

public bool OnPreUpdate(PreUpdateEvent eventItem) {
    if (!(eventItem.Entity is IDomainObject)) {
        return false;
    }

    var entity = eventItem.Entity as IDomainObject;

    if (entity == null) {
        return false;
    }

    var lastModified = DateTime.Now;

    PersistState(eventItem.Persister, eventItem.State, "LastModified", lastModified);

    entity.LastModified = lastModified;

    return false;
}

private void PersistState(IEntityPersister persister, object[] state, string propertyName, object value) {
    var index = Array.IndexOf(persister.PropertyNames, propertyName);

    if (index == -1) {
        return;
    }

    state[index] = value;
}

I have an Animal class that implements IDomainObject. There is another class, Cat, that inherits from Animal. I'm using a Table Per Subclass inheritance strategy for my NHibernate mappings and map the CreatedDate and LastModified properties to columns in the Animal table and to columns in the Cat table.

I also use a PreUpdate event handler to assign LastModified with a value.

When I update a property in a Cat object, two UPDATES get sent to the database: one for Animal and one for Cat. However, the UPDATE for Animal is the one that gets the new LastModified value, and the UPDATE for Cat keeps the old value.

This causes some issues when the Cat entity is retrieved again in another Session. It seems that LastModified gets overwritten with the new value (from Animal), effectively marking the entity has dirty, and triggering another UPDATE.

There seems to be something wrong with my overall design, but I'm not sure what it is. What is a better way to go about this?

public class Animal : IDomainObject {
    // ... various properties

    public virtual DateTime CreatedDate { get; set; }
    public virtual DateTime LastModified { get; set; }
    public virtual int Version { get; protected set; }
}

public class Cat : Animal {
    // ... various properties

    public override DateTime CreatedDate { get; set; }
    public override DateTime LastModified { get; set; }
    public override int Version { get; protected set; }
}

public interface IDomainObject {
    DateTime CreatedDate { get; set; }

    DateTime LastModified { get; set; }

    int Version { get; }
}

public bool OnPreUpdate(PreUpdateEvent eventItem) {
    if (!(eventItem.Entity is IDomainObject)) {
        return false;
    }

    var entity = eventItem.Entity as IDomainObject;

    if (entity == null) {
        return false;
    }

    var lastModified = DateTime.Now;

    PersistState(eventItem.Persister, eventItem.State, "LastModified", lastModified);

    entity.LastModified = lastModified;

    return false;
}

private void PersistState(IEntityPersister persister, object[] state, string propertyName, object value) {
    var index = Array.IndexOf(persister.PropertyNames, propertyName);

    if (index == -1) {
        return;
    }

    state[index] = value;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

二智少女猫性小仙女 2024-09-22 02:53:47

我不明白两件事:

  1. 为什么要重写 Cat 类中的 CreatedDate、LastModified 和 Version - 当它们没有新功能时?

  2. 为什么两个表中都有这三列?

在基类中拥有三列/属性应该足够了 - 其他任何东西都会带来麻烦。

Two things I don't understand:

  1. Why do you override CreatedDate, LastModified and Version in your Cat-class - when they have no new functionality?

  2. Why do you have those three columns in both tables?

It should be enough to have the three columns/properties in the baseclass - anything else is asking for trouble.

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