原子插入或 - Get和比较 - 更新 - 数据存储的侵犯? [英] Atomic Insert-or-Get and Compare-and-Update - A Repository violation?

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

问题描述

我使用Repository模式作为一个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:


  • 在资源库中​​创建一个新的实例的只要另一个不具有一定的状态值存在

  • 更新实例的状态值在仓库中的只有当它是不是已经在那个状态

  • 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.

由于数据库将是存储库的初始端点,这可以与交易的几个存储过程来实现。然而,它意味着,而不是我的仓库接口上获取/插入/更新的方法,我需要 Get_Or_Insert Compare_And_Update 语义,所以该应用程序会从资源库中的原子而不是强制执行它本身。

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.

这不是真的有可能对我来说,推动交易回调用code,特别是因为我不能保证眼前的库将是一个数据库,但可能是一个Web服务(或其他东西);并坦言在整个调解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?

推荐答案

有关它的价值,让我提供一个友好的比喻巴掌轮面对:)结果
请记住,该模式的存在为你服务,而不是反过来。如果你有一个justifyable需要'弯'的DP一个little-我相信这是确定。结果
特别是在事实,你目前没有任何更好的主意(我也不由的方式)的光。

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).

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

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