简体   繁体   English

Nhibernate的历史

[英]History with Nhibernate

I'm using NHibernate to develop an application in asp.net mvc. 我正在使用NHibernate在asp.net mvc中开发一个应用程序。 I have some entities in my model inherit from Entity abstract class, something like this: 我的模型中有一些实体继承自Entity抽象类,如下所示:

public abstract class Entity
{
    private IList<History> _history = null;

    public virtual long Id { get; set; }
    public virtual IEnumerable<History> History { get { return _history; } }

    public abstract TypeOfHistory GetTypeHistory(); // to return the right type
}

// is it right ?
public enum TypeOfHistory { 
    Product = 1, Employee = 2, Customer = 3
}

public class Product : Entity { /* code */ }
public class Employee : Entity { /* code */ }
public class Customer : Entity { /* code */ }

And I have a History class to have a log of each modify of entity: 我有一个History类来记录每个实体的修改:

public class History
{
    public virtual string Description { get; set; }
    public virtual DateTime Date { get; set; }
    public virtual string User { get; set; }
    public virtual Entity Entity { get; set; }
    public virtual TypeOfHistory Type { get; set; } // is it right ?

    protected History() { }
}

I would like to know, how can I map this History class and History property in Entity class? 我想知道,如何在Entity类中映射此History类和History属性? I mean, I will have a table for Product, another table for Employee, and another for Customer etc and only one table called History for all entities (product, customer etc), but I won't create a foreign key. 我的意思是,我将为Product提供一个表,为Employee提供另一个表,为Customer等提供另一个表,并且只有一个名为History的表用于所有实体(产品,客户等),但我不会创建外键。 I was thinking in create a field to save the type of entity but I don't know how to do it work and map the property to filter in right way. 我正在考虑创建一个字段来保存实体的类型,但我不知道如何工作并映射属性以正确的方式过滤。 (based on type). (基于类型)。 I know how to use the Listerens in nhibernate to do the log. 我知道如何使用nhibernate中的Listerens来做日志。

Thanks 谢谢

Just a suggestion... Have you considered using an Auditing Framework? 只是一个建议......您是否考虑过使用审计框架? Hibernate (java's) has Hibernate Envers which does a wonderful job with very simple setup. Hibernate(java的)有Hibernate Envers,它通过非常简单的设置完成了很棒的工作。 And it seems that there is a port to .NET: https://bitbucket.org/RogerKratz/nhibernate.envers 似乎.NET有一个端口: https//bitbucket.org/RogerKratz/nhibernate.envers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM