NHibernate的 - 如何审核实体的各个领域? [英] NHibernate - how to audit all fields of entity?
问题描述
我使用NHibernate和寻找一个解决方案,让我审核变更的实体中的所有领域。我希望能够创建一个历史表的每一个实体,即用户 - > UsersHistory将具有相同的结构,用户表和其他字段,如操作类型(更新,删除),用户的用户ID,使得变化,等等。我不'T要为每一个实体定义这样的类。我在寻找类似历史< T>
(即历史<使用者>
),因为这些条目不属于我的域名,并只用于对实体所做的更改prepare列表。我也认为,这将是最好创建插入到code,而不是创建SQL触发器这些表。基本上,我只需要创建记录的历史记录表上更新的副本或删除,我想通过NHibernate的产生插入。我还需要从历史记录表读取记录 - 正如我所说的这些表将包括实体领域和一些常见的历史记录字段的
I'm using NHibernate and looking for a solution that will allow me to audit changes to all fields in entity. I want to be able to create a history table for every entity i.e. Users -> UsersHistory that will have same structure as Users table and additional fields such as operation type (update, delete), userid of user that made change, etc. I don't want to define such class for every entity. I'm looking for something like History<T>
(i.e. History<User>
) because these entries don't belong to my domain and will only be used to prepare list of changes made to the entity. I also think that it would be better to create inserts to these tables in code rather than creating sql triggers. Basically, I just need to create a copy of record in history table on update or delete and I want the insert to be generated by NHibernate. I will also need to read records from history tables - as I said these tables will consist of entity fields and some common history fields.
我找不到关于如何创建这样的解决方案的指导。所有我能找到的是增加UserModified,UpdatedTimestamp等。如果我已经有实体等领域。不过,我需要的实体,而不仅仅是信息完整的历史谁最后更改的条目。
I cannot find guidance on how to create such solution. All I can find is adding UserModified, UpdatedTimestamp etc. if I already have such fields on entity. However, I need full history of entity not just the information who last changed the entry.
先谢谢您的帮助。
推荐答案
有很酷,对NHibernate的开放源代码的审计线索称为nhibernate.envers <一href="https://bitbucket.org/RogerKratz/nhibernate.envers">https://bitbucket.org/RogerKratz/nhibernate.envers ,所以你不必推倒重来。
There is cool, open source audit trail for NHibernate called nhibernate.envers https://bitbucket.org/RogerKratz/nhibernate.envers , so you do not have to reinvent the wheel.
它集成到透明NHibernate的,没有改变你的域模型和映射。
It integrates transparently into NHibernate, no changes to your domain model or mappings.
这就是这么简单,添加引用和呼叫:
It's as simple as, adding the reference and call:
var enversConf = new FluentConfiguration();
enversConf.Audit<User>();
nhConf.IntegrateWithEnvers(enversConf);
,而 nhConf
是NHibernate的配置对象。
whereas nhConf
is your NHibernate config object.
有关创建新的修订你的对象每一个变化,你可以问Envers通过调用来检索修订:
For every change on your object a new revision is created, you can ask Envers to retrieve a revision by calling:
var reader = AuditReaderFactory.Get(session);
var userInRevOne = reader.Find<User>(user.Id, 1);
或列出所有修订等的修订数据本身可以用一个用户名,用户ID,时间戳等。(任何你能想到的关闭)。富集
or list all revisions etc. The revision data itself can be enriched with a username, userid, timestamp etc. (whatever you can think off).
编辑: 而且它是在的NuGet:<一href="http://nuget.org/packages/NHibernate.Envers">http://nuget.org/packages/NHibernate.Envers
And it is available at NuGet: http://nuget.org/packages/NHibernate.Envers
这篇关于NHibernate的 - 如何审核实体的各个领域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!