通过提供者模式的实体框架和全局数据保存/更新处理程序
我是实体框架的新手。我之前使用过其他数据持久层,并且希望在我开发的其他应用程序中完成以下我习惯的事情。 我想问是否可以使用 EF 并获得一些关于如何操作的指南
我希望每次将实体保存到数据库(添加/更新)时,都有一个可以“附加”的钩子,通过提供者模式,必须在保存之前/之后以事务方式调用的各种功能。例如,我可能想实现一个 AuditProvider。如果要保存的实体实现了接口 IAuditEnabled,该接口添加属性 SysAddUser、SysAddDate、SysUpdateUser、SysUpdateDate(允许设置哪个用户添加/更新记录以及在什么时刻),则将调用此函数。另一个功能是调用 BusinessEntityEnforcerProvider,它检查业务对象是否实现 IBusinessEntityEnforcer 接口并调用方法 EnforceBusinessRules。只是一些想法。
只是为了澄清,我希望能够在保存之前/之后将各种功能挂钩到业务对象,我不想专门调用它们,但要确保它们始终在保存 BO 之前或之后调用。
我很感激任何建议。
谢谢
I'm new to entity framework. I used other data persistence layers before, and would like to accomplish the following things I'm used to, in the other applications I developed.
And I'd like to ask if it's possible with EF and get few guidelines on how to do
I would like that every time I save an entity to database (Add / Update), to have a hook where I can "attach", through Provider pattern, various functionalities which have to be invoked before / after save, in a transactional way. For example, I might want to implement a AuditProvider. This will be called if the entity being saved implements an Interface, IAuditEnabled, which adds properties SysAddUser, SysAddDate, SysUpdateUser, SysUpdateDate (which allow to set which user added / updated record, and at what moment). Another functionality would be to call a BusinessEntityEnforcerProvider, which check if business objects implements IBusinessEntityEnforcer interface and calls method EnforceBusinessRules. Just few thoughts.
Just to clarify, I want to have the possibility to hook various functionality to business object before / after save, which I don't want to call specificaly, but to make sure they are always called before or after BO is to be saved.
I appreciate any suggestion.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实体框架 对象上下文 生成为部分类。您可以扩展实现并重写 SaveChange 方法以执行代码每次保存之前和之后。
The Entity Framework Object Context is generated as a partial class. You can extend the implementation and override the SaveChange method in order to execute code before and after each save.