[英]Creating a simple unit of work with Entity Framework and no repository
I've been trying to implement a .net MVC Unit of Work API (rather than creating a separate repository), but it doesn't feel right. 我一直在尝试实现.net MVC工作单元API(而不是创建单独的存储库),但是感觉不对。 Am I going about this the correct way? 我要用正确的方法吗?
BaseController BaseController
public class BaseController : ApiController
{
protected DBEntities _dbEntities;
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
_dbEntities = new DBEntities();
}
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
_dbEntities.SaveChanges();
}
}
MyController 我的控制器
public class MyController : BaseController
{
public HttpResponseMessage PutMyObject(int id, int id2)
{
if (id != 0)
{
var myObject = _dbEntities.MyObjects.Where(x => x.id == id);
if (myObject.Count() > 0)
{
MyObject temp = myObject.SingleOrDefault();
temp.Processed = true;
return Request.CreateResponse(HttpStatusCode.OK);
}
else
{
return Request.CreateErrorResponse(HttpStatusCode.NotFound);
}
}
else
{
/* do some other stuff */
}
}
}
My thinking around this is that the controller action is a single Unit of Work. 我对此的想法是,控制器动作是单个工作单元。 The database connection is opened when the controller action starts, and SaveChanges is called once the response is returned. 控制器操作开始时将打开数据库连接,并在返回响应后调用SaveChanges。
Am I going about this in the correct manner? 我会以正确的方式处理吗? do i need to dispose of _dbentities after savechanges is called within the BaseController? 在BaseController中调用savechanges之后,我需要处理_dbentities吗? (or is this a better question for code review exchange) (或者这是一个更好的代码审查交流问题)
I think, It would be a bad design for OOP architecture and flexibility. 我认为,对于OOP架构和灵活性而言,这将是一个糟糕的设计。 Because, you have to provide database actions in your main MVC project and you couldn't provide them in other layers. 因为,您必须在主MVC项目中提供数据库操作,而不能在其他层中提供它们。 So, you should provide repository pattern and you should access the repositories from main MVC project and other required projects. 因此,您应该提供存储库模式,并且应该从MVC主项目和其他所需项目访问存储库。 So, I suggest you to handle _dbEntities
object in a singleton class for per thread. 因此,建议您在每个线程的单例类中处理_dbEntities
对象。
Sample singleton class would be like this; 示例单例类将是这样;
public class UnitOfWorkSampleContextBase : IDisposable
{
[ThreadStatic]
private static UnitOfWorkSampleContextBase _instance;
public static UnitOfWorkSampleContextBase Instance
{
get
{
if (_instance == null)
{
_instance = new UnitOfWorkSampleContextBase();
}
return _instance;
}
}
public SampleDbContext Context { get; private set; }
private UnitOfWorkSampleContextBase()
{
}
public void Commit()
{
Context.SaveChanges();
}
public void ResolveContext()
{
Context = new SampleDbContext(ConfigurationManager.ConnectionStrings["MainDatabase"].ConnectionString);
}
public void Dispose()
{
Context.Dispose();
Context = null;
}
}
And you can create the DbContext like this; 您可以像这样创建DbContext;
UnitOfWorkSampleContextBase.Instance.ResolveContext();
Then, you can perform actions in context like this; 然后,您可以在这样的上下文中执行操作;
var context = UnitOfWorkSampleContextBase.Instance.Context;
var records = context.sampleEntities.ToList();
Finally, you can commit and dispose the context like this; 最后,您可以像这样提交和处理上下文。
UnitOfWorkSampleContextBase.Instance.Commit();
UnitOfWorkSampleContextBase.Instance.Dispose();
The singleton class should be in repository or base layer to be accessed desired repository classes. 单例类应位于存储库或基础层中,以便可以访问所需的存储库类。
Note : If you are using CastleWindsor or something like that, creating and commiting the context would be better in an interceptor. 注意:如果使用CastleWindsor或类似的东西,则在拦截器中创建和提交上下文会更好。 Also, it is very important that the singleton context class should be initialized for per thread. 同样,为每个线程初始化单例上下文类也很重要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.