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 9001191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:24:12+00:00 2026-06-16T00:24:12+00:00

I saw a code as below in a tutorial of EF code first ,

  • 0

I saw a code as below in a tutorial of EF code first , MVC and StructureMap to create a Context Per Request pattern:

    protected void Application_Start()
    {
        ...

        initStructureMap();

    }

    private static void initStructureMap()
    {

        ObjectFactory.Initialize(x =>
        {
            x.For<IUnitOfWork>().HttpContextScoped().Use(() => new Context());
            x.For<IFirstEntity>().Use<FirstEntity>();
            x.For<ISecondEntity>().Use<SecondEntity>();
            x.For<IThirdEntity>().Use<ThirdEntity>();
        });

        ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory());
    }

    protected void Application_EndRequest(object sender, EventArgs e)
    {
        ObjectFactory.ReleaseAndDisposeAllHttpScopedObjects();
    }


public class StructureMapControllerFactory : DefaultControllerFactory
{
    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
        return ObjectFactory.GetInstance(controllerType) as Controller;
    }
}

FirstEntity ,SecondEntity and … all need IunitOfWork in their constructor .

as you can see it just uses HttpContextScoped() for the Context not others and in the EndRequest event it calls ReleaseAndDisposeAllHttpScopedObjects() .

1- is this a correct approach ?

2- shall I use HttpContextScoped() for all other Service layer Interfaces or not just for IUnitOfWork? e.g

x.For<IFirstEntity>().Use<FirstEntity>();

or

x.For<IFirstEntity>().HttpContextScoped().Use(() => new FirstEntity());

3- ReleaseAndDisposeAllHttpScopedObjects() disposes all instances or just disposes Context ?

  • 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-06-16T00:24:13+00:00Added an answer on June 16, 2026 at 12:24 am

    The convention for web applications is that you keep the same ORM context/UnitOfWork during the entire http request. This is in order to work with the same entities during the request, keep the data consistent and minimize the database calls made. The HttpContextScoped life cycle ensures that the same UoW instance is used during a request for all instances having a dependency on it.

    So 1) yes, it’s correct

    Regarding the rest of the “service layer interfaces”, it depends on whether it needs to be the same instance during the entire request. Ask yourself: “will the state of this object be needed during the entire request”? For most “services” this is not the case. Also note that making something “HttpContextScoped” will also make all it’s dependencies stay during that scope.

    This leads me to say 2) In most cases, no

    ReleaseAndDisposeAllHttpScopedObjects disposes all objects in the container registered with HttpContextScoped. By default objects are scoped as transient (new instance per call) in Structuremap.

    So 3) Just the IUnitOfWork instance will be disposed.

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

Sidebar

Related Questions

Comparing the HashMap and Hashtable source code in JDK 1.6, I saw the below
While I was studying assembly code, I saw below code segment. I am a
I saw some code as below during a peer-code-review session: char *s = new
I saw this below code in an website. I could not able to understsnd
I'm learning multi-threading in C# and I saw a code below static readonly object
I saw the following in some code (see below). Have not come across the
I saw the code below in this answer to What is your favorite C
I saw a below code: Map(1 -> one, 2 -> two) map _._1 this
I once saw this nice little snippet of code below, here at SO: template<typename
I am not exposed to Spring as yet. I saw the below code in

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.