实体是否应该访问服务层并执行更新/保存方法?
我的应用程序要求:如果用户更改了任何属性(例如将数据输入文本框然后离开文本框),则必须立即将其更新到数据库。
我使用 WPF 和 MVVM 设计模式。我的所有实体都实现了 INotifyPropertyChanged。 如果客户的任何属性发生变化,我会做
Customer.cs
customerService.UpdateCustomer(this);
实体是否真的应该更新自身?或者诱导它自己更新数据库?
我的意思是,如果不在实体内,我还能如何立即更新财产?
My application requirements: if any property is changed by the user - like entering data into a textbox and then leaving the textbox - it must be updated immediately to the database.
I using WPF with MVVM design pattern. All my entities implement INotifyPropertyChanged
.
If any property of a CUSTOMER changes I do
Customer.cs
customerService.UpdateCustomer(this);
Should an entity really update itself? Or induce its own update into the database?
I mean how else could I do an immediate upate of a property if not within the entity ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实体不应该自我更新;另一个服务(存储库)应该这样做。
您可以通过订阅实体的
PropertyChanged
事件来向存储库指示应更新实体。在事件处理程序中,您可以确保您的实体已更新:
但是,顺便说一句,我认为每当属性更改时您的实体都应该更改,这是一个奇怪的要求。
这意味着交易毫无用处。我的意思是,您没有真正的“工作单元”,所有更改都应该提交,或者根本不应该保留?
除此之外,它还会导致数据库的大量往返。
An entity should not update itself; another service (repository) should do that.
You can induce the repository that the entity should be updated, by subscribing to the
PropertyChanged
event of the entity.In the eventhandler, you can make sure that your entity is updated:
But, on a sidenote, I think it is a strange requirement that your entity should be changed whenever a property changes.
This means that transactions are quite useless. I mean, you have no real 'unit of work', where all changes should be committed, or should not be persisted at all ?
Next to that, it also results in lots of roundtrips to the database.