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

  • Home
  • SEARCH
  • 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 680737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:24:20+00:00 2026-05-14T01:24:20+00:00

My web app solution consists of 3 projects: Web App (ASP.NET MVC) Business Logic

  • 0

My web app solution consists of 3 projects:

  1. Web App (ASP.NET MVC)
  2. Business Logic Layer (Class Library)
  3. Database Layer (Entity Framework)

I want to use Ninject to manage the lifetime of the DataContext generated by the Entity Framework in the Database Layer.

The Business Logic layer consists of classes which reference repositories (located in the database layer) and my ASP.NET MVC app references the business logic layer’s service classes to run code. Each repository creates an instance of the MyDataContext object from the Entity Framework

Repository

public class MyRepository
{
     private MyDataContext db;
     public MyRepository
     {
        this.db = new MyDataContext();
     }

     // methods
}

Business Logic Classes

public class BizLogicClass
{
     private MyRepository repos;
     public MyRepository
     {
          this.repos = new MyRepository();
     }

     // do stuff with the repos
}

Will Ninject handle the lifetime of MyDataContext despite the lengthy dependency chain from the Web App to the Data Layer?

  • 1 1 Answer
  • 2 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-14T01:24:20+00:00Added an answer on May 14, 2026 at 1:24 am

    EDIT

    I has some problems with it some time ago, but now it seems to work:

    Bind<CamelTrapEntities>().To<CamelTrapEntities>().Using<OnePerRequestBehavior>();
    

    Instead of using HttpModule, you can use OnePerRequestBehavior and it will take care of handling context in current request.

    EDIT 2

    OnePerRequestBehavior needs to be registered in web.config, because it depends on HttpModule too:

    In IIS6:

    <system.web>
      <httpModules>
        <add name="OnePerRequestModule" type="Ninject.Core.Behavior.OnePerRequestModule, Ninject.Core"/>
      </httpModules>
    </system.web> 
    

    With IIS7:

    <system.webServer> 
      <modules>
        <add name="OnePerRequestModule" type="Ninject.Core.Behavior.OnePerRequestModule, Ninject.Core"/>
      </modules>
    </system.webServer>
    

    PREVIOUS ANSWER

    It is your responsibility to dispose context when it is not needed. Most popular way in ASP.NET is to have one ObjectContext per request. I do it by having HttpModule:

    public class CamelTrapEntitiesHttpModule : IHttpModule
    {
        public void Init(HttpApplication application)
        {
            application.BeginRequest += ApplicationBeginRequest;
            application.EndRequest += ApplicationEndRequest;
        }
    
        private void ApplicationEndRequest(object sender, EventArgs e)
        {
            ((CamelTrapEntities) HttpContext.Current.Items[@"CamelTrapEntities"]).Dispose();
        }
    
        private static void ApplicationBeginRequest(Object source, EventArgs e)
        {
            HttpContext.Current.Items[@"CamelTrapEntities"] = new CamelTrapEntities();            
        }
    }
    

    This is injection rule:

    Bind<CamelTrapEntities>().ToMethod(c => (CamelTrapEntities) HttpContext.Current.Items[@"CamelTrapEntities"]);
    

    My Repository takes ObjectContext in constructor:

    public Repository(CamelTrapEntities ctx)
    {
        _ctx = ctx;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've a solution with two projects on VS2008: an ASP.NET Web App and a
I've a solution which consists of a web project and a class library project.
I'm working on a web app for a class. It's basically a project management
I am building a new web app and I am using Spring, JPA/Hibernate, and
My company recently developed a small file management web app for a law firm.
Is there a way to allow a Flash app to access local files? I've
they asked me to build an iPad app for a clothes company, the client
I have an AJAX intensive web application where the requests are unsecured, meaning there
how can a drag and drop a folder with music and other directories of
I'm new to Java and have searched for quite a time now on the

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.