NHibernate 日志表跟踪表的所有更改
我有以下数据库结构:
Users:
- UserID
- UserName
- FirstName
- LastName
...
UsersLog:
- UserLogID
- UserID
- UserName
- FirstName
- LastName
...
- DateCreated
每次进行插入或编辑时,日志表都会针对用户插入一条记录。我拥有日志表的原因是,当提交订单时,我可以简单地根据订单记录用户日志 ID,这意味着我拥有订单时的用户数据的表示。我想知道绘制此图的最佳方法是什么?目前我已经映射了两个表,这让我可以说:
var user = new User { ... };
userRepository.Insert(user);
userLogRepository.Insert(new UserLog { User = user, UserName = user.UserName, FirstName = user.FirstName, ..., DateCreated = DateTime.UtcNow });
到目前为止我最大的问题是在插入日志表时必须重新添加每个字段。我知道我可以创建一个构造函数来处理这个问题,但我想知道是否有更好的方法来处理这个问题。如果有人可以提供帮助,我将不胜感激。
谢谢
i have the following db structure:
Users:
- UserID
- UserName
- FirstName
- LastName
...
UsersLog:
- UserLogID
- UserID
- UserName
- FirstName
- LastName
...
- DateCreated
The log table simply inserts a record against the user everytime an insert or edit is made. The reason i have the log table is that when an order is submitted i can simply log the user log id against the order which means i have a representation of the user data at the time of the order. I was wondering what is the best way to map this? Currently i have both tables mapped which allows me to say:
var user = new User { ... };
userRepository.Insert(user);
userLogRepository.Insert(new UserLog { User = user, UserName = user.UserName, FirstName = user.FirstName, ..., DateCreated = DateTime.UtcNow });
My biggest problem so far is that i have to re-add every field when inserting into the log table. I know i can create a constructor to handle this but i was wondering if there was a better way of handling this. I'd appreciate it if someone could help.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您只关心映射代码,可以使用 Automapper
你的代码看起来像这样(这超出了我的想象,但我认为这是正确的):
或者,如果你不喜欢代码所在的位置,你可以考虑使用 审核事件侦听器
If you're just concerned about the mapping code, you could use Automapper
Your code would look something like (this is off the top of my head, but I think it's about right):
Alternatively, if you don't like where the code is sitting, you could look at using an Audit Event Listener