Setter node using a named session instance strategy with a structural map

I am using a Structural map and want to insert an instance (built by the container) into the controller property. The instance must be named and stored in the http session context container. In a previous version of my application, I used a custom DI structure, and it was easy enough to do such things:

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

Are there any simple ways to do this with a structure?
Or maybe I can introduce my custom attributes and injection logic into the SM environment (somehow expand the structure)?
Please help me find a way to solve this problem and many thanks!

PS I found a temporary solution, but it increases the adhesion of the controller to the 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;
    }
}

, ObjectFactory.GetInstance ...

+3
2

, ASP.NET MVC StructureMap. ( , .)

factory, . ( factory, ASP.NET MVC, )

, MyController , MyManager ( factory)

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

ASP.NET MVC, factory ( 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);
}

StructureMapControllerFactory ObjectFactory.GetInstance, ( , )

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

, , , , .

, - , .

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

. StructureMap InstanceScope.

+2

StructureMap ,

For<IBulkManager>()
    .LifecycleIs(Lifecycles.GetLifecycle(InstanceScope.HttpSession))
    .Use<BulkManager>();

,

public IBulkManager OrdersBulkManager
{
    get { return ObjectFactory.GetInstance<IBulkManager>(); }
}
+2

Source: https://habr.com/ru/post/1712649/


All Articles