简体   繁体   English

使用Subsonic的SimpleRepository进行细粒度CRUD

[英]Fine Grained CRUD with Subsonic's SimpleRepository

Let' say I have a TestClass in my C# app with property A and property B . 假设我的C#应用​​程序中有一个TestClass ,其属性为A ,属性为B. I change the value of B by my code, I leave property A unchanged. 我通过我的代码更改了B的值,我将属性A保持不变。 I update TestClass in database by SimpleRepository's Update method. 我通过SimpleRepository的Update方法更新数据库中的TestClass。

As I see it updates also property A value in the database. 我看到它更新了数据库中的属性A值。

It is easy to test: I change value A in my database outside my app ('by hand'), then I make the update from my app. 它很容易测试:我在我的应用程序之外的数据库中更改了值A ('手动'),然后我从我的应用程序进行更新。 Value of property A changes back to its value according to TestClass's state in my app. 属性A的值根据TestClass在我的应用程序中的状态变回其值。

So, my question: is it possible to make updates only to some properties, not for the whole class by SimpleRepository? 所以,我的问题是:是否有可能仅对某些属性进行更新,而不是由SimpleRepository对整个类进行更新? Are there some 'IgnoreFields' possibilities? 是否有一些'IgnoreFields'的可能性?

What you need is optimistic concurrency on your UPDATE statement, not to exclude certain fields. 您需要的是UPDATE语句的optimistic concurrency ,而不是排除某些字段。 In short what that means is when updating a table, a WHERE clause is appended to your UPDATE statement that ensures the values of the fields in the row are in fact what they were when the last SELECT was run. 简而言之,这意味着在更新表时, WHERE子句会附加到UPDATE语句,以确保行中字段的值实际上是最后一次运行SELECT时的值。

So, let's assume in your example I selected some data and the values for A and B were 1 and 2 respectively. 因此,我们假设在您的示例中我选择了一些数据, AB的值分别为12 Now let's assume I wanted to update B (below statement is just an example): 现在让我们假设我想更新B (下面的语句只是一个例子):

UPDATE TestClass SET B = '3' WHERE Id = 1;

However, instead of running that statement (because there's no concurrency there), let's run this one: 但是,不要运行该语句(因为那里没有并发),让我们运行这个:

UPDATE TestClass SET B = '3' WHERE Id = 1 AND A = '1' AND B = '2';

That statement now ensures the record hasn't been changed by anybody. 该声明现在确保记录没有被任何人改变。

However, at the moment it doesn't appear that Subsonic's SimpleRepository supports any type of concurrency and so that's going to be a major downfall. 但是,目前看来,Subsonic的SimpleRepository似乎不支持任何类型的并发,因此这将是一个重大的垮台。 If you're looking for a very straight forward repository library, where you can use POCO's, I would recommend Dapper. 如果您正在寻找一个非常直接的存储库,您可以在其中使用POCO,我会推荐Dapper。 In fact, Dapper is used by Stackoverflow. 实际上,Stackoverflow使用Dapper。 It's extremely fast and will easily allow you to build in concurrency into your update statements because you send down parameterized SQL statements, simple. 非常快,并且很容易让你在更新语句中构建并发性,因为你发送参数化的SQL语句很简单。

  1. This Stackoverflow article is an overall article on how to use Dapper for all CRUD ops. 这篇Stackoverflow文章是关于如何将Dapper用于所有CRUD操作的整篇文章。
  2. This Stackoverflow article shows how to perform inserts and updates with Dapper. Stackoverflow文章介绍了如何使用Dapper执行插入和更新。

NOTE: with Dapper you could actually do what you're wanting to as well because you send down basic SQL statements, but I just wouldn't recommend not using concurrency. 注意:使用Dapper,您实际上也可以执行您想要的操作,因为您发送了基本的SQL语句,但我不建议不使用并发。

Don't call the update method on the DataObject for such cases, you are basically indicating that the object has been changed and needs to be updated in the DB. 对于这种情况,不要在DataObject上调用update方法,基本上表明对象已更改并需要在DB中更新。 So subsonic will generate a query like 所以亚音速会生成一个类似的查询

UPDATE TestClass SET A ='', B='', ModifiedOn = 'DateHere' WHERE PrimaryKey = ID

to change only the property B you need to consturct the UPDATE query manually. 要仅更改属性B,您需要手动构建UPDATE查询。 have a look at the Subsonic.Update class. 看看Subsonic.Update类。
Ideally you shouldn't be forming a new instance of the data object manually, if you do so make sure the values are copied from the object retured from the Subsonic.Select query. 理想情况下,您不应手动形成数据对象的新实例,如果这样做,请确保从Subsonic.Select查询中退回的对象复制值。
So when you update the value of even only one property all other properties will hold their own value from DB rather than a default value depending on the type of the property. 因此,当您更新甚至只有一个属性的值时,所有其他属性将从DB保留其自己的值,而不是默认值,具体取决于属性的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM