ASP.NET MVC:存储库模式高并发更新 [英] ASP.NET MVC: Repository pattern high concurrency updates

查看:98
本文介绍了ASP.NET MVC:存储库模式高并发更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个应用程序,稍后我们可能会关闭存储库(当前为实体框架),以使用亚马逊或Windows Azure存储.

I'm writing an app that we may be switching out the repository later (currently entity framework) to use either amazon or windows azure storage.

我有一个通过ID禁用用户的服务方法,它所做的全部就是将属性设置为true并设置DisabledDate.我应该调用存储库,获取该用户,在服务中设置属性,然后调用存储库中的save函数吗?如果这样做,那么多数民众赞成在2个数据库调用中,我应该为此担心吗?如果用户在管理员调用disable方法的同时更新配置文件,并调用用户调用存储库中的save方法(当前对IsDisabled属性持有false)怎么办?如果在禁用方法之后立即调用,该如何启用?

I have a service method that disables a user by the ID, all it does is set a property to true and set the DisabledDate. Should I call to the repository, get that user, set the properties in the service, then call to the save function in the repository? If I do this, then thats 2 database calls, should I worry about this? What if the user is updating the profile at the same time the admin is calling the disable method, and calls the user calls the save method in the repository (which currently holds false for the IsDisabled property?) Wouldn't that set the user back to being enabled if called right after the disabled method?

解决此问题的最佳方法是什么?如何在高并发系统中更新数据?

What is the best way to solve this problem? How do I update data in a high concurrent system?

推荐答案

CustomerRepository:

// Would be called from more specific method in Service Layer - e.g DisableUser
public void Update(Customer c)
{
   var stub = new Customer { Id = c.Id }; // create "stub"
   ctx.Customers.Attach(stub); // attach "stub" to graph
   ctx.ApplyCurrentValues("Customers", c); // override scalar values of "stub"
   ctx.SaveChanges(); // save changes - 1 call to DB. leave this out if you're using UoW 
}

那应该在您的存储库中用作通用的"UPDATE"方法.仅在实体存在时使用.

That should serve as a general-purpose "UPDATE" method in your repository. Should only be used when the entity exists.

那只是一个例子-实际上,您应该/应该使用泛型,在附加之前检查图中是否存在实体,等等.

That is just an example - in reality you should/could be using generics, checking for the existence of the entity in the graph before attaching, etc.

但这将使您走上正确的轨道.

But that will get you on the right track.

这篇关于ASP.NET MVC:存储库模式高并发更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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