原子插入或获取以及比较和更新 - 存储库违规?
我正在使用存储库模式作为 C#4 和 MVC3 项目的一部分,我只是想确保我没有做坏事;要么是这样,要么只是我的建筑学究气有点得意忘形,我只需要一个象征性的耳光:)
对于特定的模型类型,我需要能够执行以下两个操作:
- 在存储库只要不存在具有特定状态值的另一个存储库
- 更新存储库中实例的状态值仅当它尚未处于该状态时
在这两种情况下操作的原子性至关重要因为它们被用来做出关键决策。
由于数据库将成为存储库的初始端点,因此可以通过几个存储过程中的事务来实现。然而,这意味着我需要 Get_Or_Insert 和 Compare_And_Update 语义,而不是 Repository 接口上的 Get/Insert/Update 方法,以便应用程序可以期望来自 Repository 的原子性,而不是强制执行它本身。
我实际上不可能将事务推回调用代码,特别是因为我不能保证直接存储库将是数据库,但可能是 Web 服务(或其他事物);坦率地说,虽然我知道这是可能的,但是对于我书中这样一个简单的操作来说,跨网络服务调解交易有点太繁重了(特别是在实现方面)!
那么,我是否可以愉快地将这些特殊情况操作添加到存储库中,或者我是否遵循了错误的模式?
I'm using a the Repository pattern as part of a C#4 and MVC3 project and I just want to make sure I'm not doing something bad; either that or it's just my architectural pedantry getting a bit carried away with itself and I just need a figurative slap round the face :)
For a particular model type I need to be able to do the following two operations:
- Create a new instance in the repository so long as another doesn't exist with a certain state value
- Update the state value of an instance in the repository only if it's not already at that state
In both cases it is crucial that the operations be atomic as they are used to make crucial decisions.
Since a database is going to be the initial endpoint for the repository, this can be implemented with transactions in a couple of stored procedures. However it means that instead of Get/Insert/Update methods on my Repository interface, I need Get_Or_Insert
and Compare_And_Update
semantics so that the application can expect the atomicity from the Repository rather than enforcing it itself.
It's not really possible for me to push the transaction back to the calling code, in particular because I can't guarantee the immediate repository will be a DB, but could be a web service (or other things); and frankly mediating transactions across web services, whilst I know it's possible, is just a bit too heavy (especially in terms of implementation) for such a simple operation in my book!
So, can I happily add these special case operations to the repository, or am I following the pattern wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不管怎样,请允许我友好地“打一巴掌”:)
请记住,模式的存在是为了为您服务,而不是相反。如果您有合理的需要稍微“弯曲”DP - 我相信这是可以的。
特别是考虑到您目前没有更好的想法(顺便说一句,我也没有)。
for what it's worth, allow me to offer a friendly "figurative slap round the face" :)
remember that patterns exist to serve you, and not the other way round. If you have a justifyable need to 'bend' the DP a little- I believe it's OK.
Especially in light of the fact that you don't currently have any better idea (nor do I, by the way).