How to get the container in MVC e.g. RoleProvider

Jul 20, 2013 at 8:36 AM
Edited Jul 20, 2013 at 8:39 AM
Hi, im using Unity in an MVC Application to Inject my Repositories. There are some points where I don't know how to get the container because I don't know the context.

a) RoleProvider / MembershipProvider
b) Display/Editortemplates
c) Handlers (ashx)

Can somebody help me?
Editor
Jul 22, 2013 at 6:15 PM
Edited Jul 22, 2013 at 6:18 PM
To state the problem in general: the scenario is classes that integrate into a predefined framework (for example ASP.NET) where the objects are instantiated by the framework. In these cases there is no opportunity to use constructor injection. The next option to look at as an alternative is to use property injection along with a call to BuildUp(). One common example where this can be done is for ASP.NET Web Forms. This example uses the approach with a RoleProvider:
    public class MyRoleProvider : RoleProvider
    {
        public Common.Logging.ILog Logger { get; set; }
    }

    container.RegisterType<MyRoleProvider>(
        new InjectionProperty("Logger", 
            new ResolvedParameter<Common.Logging.ILog>()));

    container.BuildUp((MyRoleProvider)Roles.Provider);

If property injection is not viable then you may have to use the container as a service locator by providing access to the container via a singleton.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to