在ASP.NET管理实体框架的ObjectContext实体、框架、ASP、NET

2023-09-03 04:05:22 作者:无声飞雪

我使用实体框架的ASP.NET Web窗体应用程序,我不知道我应该如何处理的ObjectContext 和它的使用寿命。 例如,我有一个 InviteService 类管理的邀请,如创建和接受邀请。类本身就是从Web项目中的另一个项目/命名空间。 一个 InviteUsers()方法创建一个邀请实体为用户的列表,调用库将它们保存到数据库中,邮件每个用户的邀请链接。

I'm using the Entity Framework for an ASP.NET Web Forms application and I'm wondering how I should deal with ObjectContext and it's lifetime. For example, I have an InviteService class that manages invites such as creating and accepting invites. The class itself is in another project/namespace from the Web project. An InviteUsers() method creates Invite entities for a list of users, calls a repository to save them to the database and mails each user an invite link.

该方法是从所谓的当用户点击邀请按钮。

The method is called from the Page when a user clicks the invite button.

我想知道我应该怎么使用的ObjectContext

I would like to know how I should use the ObjectContext

构造一个新的的ObjectContext 在页面上的每个请求,将它作为一个参数的构造函数中的 InviteService 类,然后在处理它的渲染方法。 在与上面相同的,而不是通过构造函数设置它,以及将它作为参数传递给每个方法,但 创建一个单独的的ObjectContext 中的每个方法与使用块。 Instantiate a new ObjectContext on the Page on each Request, passing it as a parameter to the constructor of the InviteService class and then disposing it in the Render method. Same as above but instead of setting it via the constructor, passing it along as a parameter to each method. Create a separate Objectcontext in each method with a using block.

方案一似乎最好是我根据拉吉斯拉夫这里的答案是:Entity框架和连接池 但是选项3似乎也有效,因为据我所知,没有新的数据库连接,因为连接池进行。

Option one seems best to me based on the answer of Ladislav here: Entity Framework and Connection Pooling But option 3 seems valid as well since as far as I know, no new database connections are made because of connection pooling.

推荐答案

这是不寻常的每个Web请求创建一个的ObjectContext 。我这样做是在我的Web应用程序。然而,海事组织,页面应该一无所知的ObjectContext

It is not unusual to create a single ObjectContext per web request. I do this in my web applications. However, IMO, the page should know nothing about the ObjectContext.

既然你已经在谈论注射在服务的构造背景下,看看依赖注入(如果你不使用的话)。当您使用依赖注入容器,可以让容器创建为您服务,在该容器中的对象上下文注入。你的页面必须做的唯一事情是从容器中请求服务(理想情况下,你甚至可以让在该页面的构造函数的服务被注入,但是这是不可能的Web表单)。

Since you are already talking about injecting the context in the constructor of the service, take a look at dependency injection (if you aren't using that already). When you use a dependency injection container, you can let the container create that service for you and inject the object context in that container. The only thing your page has to do is request that service from the container (ideally, you would even let that service be injected in the constructor of that page, but this is not possible with web forms).

您的网页是这样的:

public class MyPage : Page
{
    private readonly IMyService service;

    public MyPage()
    {
        this.service = Global.GetInstance<IMyService>();
    }

    protected void Btn1_OnClick(object s, EventArgs e)
    {
        this.service.DoYourThing(this.TextBox1.Text);
    }
}

在应用程序的启动路径(Global.asax中),您可以配置依赖注入框架是这样的:

In the startup path (Global.asax) of your application, you can configure the Dependency Injection framework like this:

private static Container Container;

public static T GetInstance<T>() where T : class
{
    return container.GetInstance<T>();
}

void Application_Start(object sender, EventArgs e) 
{
    var container = new Container();

    string connectionString = ConfigurationManager
        .ConnectionStrings["MyCon"].ConnectionString;

    // Allow the container to resolve your context and
    // tell it to create a single instance per request.
    container.RegisterPerWebRequest<MyContext>(() =>
        new MyContext(connectionString));

    // Tell the container to return a new instance of
    // MyRealService every time a IMyService is requested.
    // When MyContext is a constructor argument, it will
    // be injected into MyRealService.
    container.Register<IMyService, MyRealService>();

    Container = container;
}

在这些例子中我用href="https://simpleinjector.org" rel="nofollow">简单的注射器依赖注入容器的