简体   繁体   English

每个HttpContext实例一个对象

[英]One object per HttpContext instance

I'm currently writing a project in ASP.NET MVC. 我目前正在用ASP.NET MVC编写一个项目。 I have a web project and DB project which solely works with DB. 我有一个仅与DB一起使用的Web项目和DB项目。 The layers look like this and they interoperate only with sibling layers. 这些层看起来像这样,它们只能与同级层进行互操作。

DB project (EF CF) - makes db requests 数据库项目(EF CF) -发出数据库请求

Repository - abstracting the underlying db model 存储库 -抽象底层数据库模型

Service - All business logic happenes here. 服务 -所有业务逻辑都在这里发生。

ASP.NET MVC Web application - Front end presentation ASP.NET MVC Web应用程序 -前端演示

They must be loosely coupled so I'm using Unity DI/IoC framework 它们必须松散耦合,所以我正在使用Unity DI / IoC框架

What I want to achieve is to create one instance of DbContext per http request. 我要实现的是为每个http请求创建一个DbContext实例。 The following is the logic I implemented so far. 以下是到目前为止我实现的逻辑。

public class MyAppBaseController : Controller {
    protected override void OnActionExecuting(ActionExecutingContext filterContext) {
        if (HttpContext.Items["DbModel"] == null) {
            HttpContext.Items["DbModel"] = MySingleton.Container.Resolve<DbContext>();
        }
        base.OnActionExecuting(filterContext);
    }
}

What is does is that in request pipeline if the Items dictionary of the current HttpContext doesn't have DbContext , it adds one. 它的作用是在请求管道中,如果当前HttpContextItems字典没有DbContext ,它将添加一个。 All controllers inherit from it. 所有控制器都继承自它。 The reason why I'm doing so is that all repositories that will be called during execution should use exactly the same DbContext object for all sequential db calls. 我这样做的原因是,在执行过程中将被调用的所有存储库应该对所有顺序的db调用使用完全相同的DbContext对象。

  1. Is there a better way to couple the lifetime of the object with the HttpContext ? 有没有更好的方法将对象的生存期与HttpContext耦合在一起?
  2. Is it possible to do it using Unity (DI/IoC framework)? 是否可以使用Unity(DI / IoC框架)来实现?

you can take control over the instance lifetime of your dependencies in unity's object lifetime management as specified here 您可以按照此处指定的方式在unity的对象生存期管理中控制依赖项的实例生存期

you will have to write your own that injects your object instance in httpcontext 您将必须编写自己的对象,以将对象实例注入httpcontext

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

相关问题 C# 实体框架每个 HttpContext 仅使用一个 ObjectContext - C# Entity Framework using only one ObjectContext per HttpContext httpcontext.current.server.mappath 对象引用未设置为对象的实例 - httpcontext.current.server.mappath Object reference not set to an instance of an object 自定义数据库连接对象:我应该对每个对象实例使用一个通用对象(单个)还是一个? - Custom Database Connection Object: Should i use a general one (singleton) or one per object instance? 每个对象实例是否可以加锁? - Is a lock possible per instance of an object? 类实例未保存到HttpContext - Class instance not saving to HttpContext 每个进程只有一个静态变量实例吗? - Is there only one instance of a static variable per process? HttpContext.User.Identity.IsAuthenticated throws System.NullReferenceException:对象引用未设置为对象的实例 - HttpContext.User.Identity.IsAuthenticated throws System.NullReferenceException: Object reference not set to an instance of an object HttpContext.Current.Server.HtmlEncode返回未将对象引用设置为对象的实例 - HttpContext.Current.Server.HtmlEncode return Object reference not set to an instance of an object HttpContext之类的对象 - HttpContext like Object HttpContext 需要对象引用 - Object reference is required for HttpContext
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM