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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:38:03+00:00 2026-05-13T22:38:03+00:00

I’m using Structure map and want to inject instance (constructed by container) into controller’s

  • 0

I’m using Structure map and want to inject instance (constructed by container) into controller’s property. Instance should be named and stored in http session context container.
In the previous version of my application I’ve used custom DI framework and it was easy enough to make such things:

public class MyController : Controller
{
    [InjectSession("MySessionInstanceKey")]
    public MyManager Manager {get; set;}
}

Is there any easy ways to do it with structuremap ?
Or maybe i can introduce my custom attributes and injection logic into SM framework (extend framework somehow) ?
Please help me to find a way to resolve this and Thanks a lot !

P.S. i’ve found temporary solution, but it increases cohesion of controller with IoC framework and contains a lot of code:

private const string ordersBulkManagerKey = "_OrdersBulkManager";
public BulkManager OrdersBulkManager
{
    get
    {
        var manager = Session[ordersBulkManagerKey] as BulkManager;
        if(manager == null)
        Session[ordersBulkManagerKey] = manager
            = ObjectFactory.GetInstance<BulkManager>();
        return manager;
    }
}

So, I don’t want to use ObjectFactory.GetInstance there …

  • 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-13T22:38:04+00:00Added an answer on May 13, 2026 at 10:38 pm

    There’s a few blog post out there on how to wire up configure ASP.NET MVC and StructureMap together. (So most of the following code is a summary from various posts out there.)

    The common way to get this going is by declaring our own controller factory class, which will allow us inject our dependencies via the constructor. (The default controller factory that ASP.NET MVC uses requires a default constructor to be present)

    So in your MyController class, you would have a constructor which accept a MyManager parameter (which will be injected for you by our very own controller factory class)

    public class MyController : Controller
    {
       private readonly ISomeService _someService;
       //Constructor Injection. 
       public MyController(ISomeService someService){
           _someService = someService;
        }
    }
    

    Next you would configure ASP.NET MVC do use this new controller factory class (which we will call StructureMapControllerFactory).

    protected void Application_Start()
    {
    
       //This is where you register your concrete types with StructureMap
        Bootstrapper.ConfigureStructureMap();
    
        //Our very own Controller Factory
        ControllerBuilder.Current.SetControllerFactory
        (new StructureMapControllerFactory());
    
        RegisterRoutes(RouteTable.Routes);
    }
    

    In the StructureMapControllerFactory, we call ObjectFactory.GetInstance to you return us the controller (provided we have wired up all our dependencies)

    public class StructureMapControllerFactory : DefaultControllerFactory
    {
        protected override IController GetControllerInstance(Type controllerType)
        {
            if (controllerType == null) return null;
            try
            {
                return ObjectFactory.GetInstance(controllerType) as Controller;
            }
            catch (StructureMapException)
            {
                System.Diagnostics.Debug.WriteLine(ObjectFactory.WhatDoIHave());
                throw;
            }
        }
    }
    

    Hopefully my explanation is clear, but let me know if it isn’t and I can expand on it.

    BTW, the snippet below is an example on how your bootstrap code might look like.

    public static class Bootstrapper 
    {
        public static void ConfigureStructureMap()
        {
            ObjectFactory.Initialize(
             x => x.AddRegistry(new MyApplicationRegistry()));            
        }
    }
    
    public class MyApplicationRegistry : Registry
    {
        public MyApplicationRegistry()
        {
             ForRequestedType<ISomeService>()
             .CacheBy(InstanceScope.Your_Choice_Here)
             .TheDefault.Is.OfConcreteType<SomeService>();
        }
    }
    

    Note: Please refer to StructureMap doc for various InstanceScope options.

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

Sidebar

Ask A Question

Stats

  • Questions 448k
  • Answers 448k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can attache an event handler to the Navigating event.… May 15, 2026 at 7:44 pm
  • Editorial Team
    Editorial Team added an answer More data points: If we use std::tr1::tuple and explicitly state… May 15, 2026 at 7:44 pm
  • Editorial Team
    Editorial Team added an answer If security is a concern, is it actually safe to… May 15, 2026 at 7:44 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.