原子插入或获取和比较和更新 - 存储库违规? [英] Atomic Insert-or-Get and Compare-and-Update - A Repository violation?

查看:17
本文介绍了原子插入或获取和比较和更新 - 存储库违规?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用存储库模式作为 C#4 和 MVC3 项目的一部分,我只是想确保我没有做坏事;要么是这样,要么只是我的建筑学究有点被自己冲昏了头脑,我只需要在脸上打一个比喻性的巴掌:)

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:

  • 在存储库中创建一个新实例只要不存在具有特定状态值的另一个实例
  • 更新存储库中实例的状态值仅当它尚未处于该状态时

在这两种情况下,操作必须是原子的,因为它们用于做出关键决策.

In both cases it is crucial that the operations be atomic as they are used to make crucial decisions.

由于数据库将成为存储库的初始端点,因此可以通过几个存储过程中的事务来实现.然而,这意味着我需要 Get_Or_InsertCompare_And_Update 语义,而不是我的 Repository 接口上的 Get/Insert/Update 方法,以便应用程序可以期待来自 Repository 而不是的原子性自己强制执行.

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.

我真的不可能将事务推回调用代码,特别是因为我不能保证直接存储库将是一个数据库,但可能是一个 Web 服务(或其他东西);坦率地说,虽然我知道这是可能的,但对于我书中这样一个简单的操作来说,在网络服务之间进行事务中介有点太重了(尤其是在实现方面)!

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?

推荐答案

为了它的价值,请允许我提供一个友好的比喻性的耳光":)
请记住,模式的存在是为您服务,而不是相反.如果您有正当理由需要稍微弯曲"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).

这篇关于原子插入或获取和比较和更新 - 存储库违规?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆