Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7083879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:13:58+00:00 2026-05-28T07:13:58+00:00

I’m using the Entity Framework for an ASP.NET Web Forms application and I’m wondering

  • 0

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.

I would like to know how I should use the ObjectContext

  1. 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.
  2. Same as above but instead of setting it via the constructor, passing it along as a parameter to each method.
  3. Create a separate Objectcontext in each method with a using block.

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:13:59+00:00Added an answer on May 28, 2026 at 7:13 am

    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.

    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).

    Your page would look like this:

    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);
        }
    }
    

    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;
    }
    

    In these examples I used the Simple Injector dependency injection container, although any DI container will do. The RegisterPerWebRequest is not part of the core library, but is available as (NuGet) extension package. The package ensures that your ObjectContext is disposed when the web request ends.

    This might seem complex at first, but this way the web page doesn’t have to worry at all about any details of creating and disposing an ObjectContext.

    Further, place the logic that executes a use case in a single class: a command. Let the command (or the system) ensure atomicness of that operation. Don’t let the page be responsible for this, and don’t commit on the end of the request, because at that point you won’t know if it is even OK to call commit. No, let the command handle this itself. Here is an article about writing business commands.

    This advice holds for ASP.NET MVC as well, although you should not call Global.GetInstance<IMyService>() inside the Controller’s constructor, but simply use constructor injection (since MVC has great support for this) and use the MVC3 Integration package.

    Also take a look at this Stackoverflow question, which talks about choosing between a IObjectContextFactory or having a ObjectContext per request.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
Seemingly simple, but I cannot find anything relevant on the web. What is the
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.