5

I worked a little with StructureMap and I managed to inject in my controller (through constructor injection) a concrete type repository for an interface.

Now, I need to inject a repository type into my custom membership provider. But how? My custom membership provider is created through Membership.Provider.ValidateUser (for example).

For controller I created a class like this:

public class IocControllerFactory : DefaultControllerFactory
{
    protected override IController GetControllerInstance(
        System.Web.Routing.RequestContext requestContext, 
        Type controllerType)
    {
        return (Controller)
            ObjectFactory.GetInstance(controllerType);
    }
}

and in Global.asax, in Application_Start():

//...
ObjectFactory.Initialize(x =>
{
    x.AddRegistry(new ArticleRegistry());
}
                                    );


ControllerBuilder.Current.SetControllerFactory(
    new IocControllerFactory());
//...

But how inject an concrete type in my custom membership provider with StructureMap?

tereško
  • 58,060
  • 25
  • 98
  • 150
Blocked
  • 340
  • 2
  • 5
  • 20
  • possible duplicate of [Property injection in custom membership provider using Castle](http://stackoverflow.com/questions/10174636/property-injection-in-custom-membership-provider-using-castle) – Steven Jul 31 '12 at 12:28

1 Answers1

3

There'a no way to inject in this case I think.

Injection only works when the "factory" creating the instances required, know about injection, and the Membership provider area unfortunately is showing it's age. It was coded back when things were done differently :).

If you're using your custom membership provider with ASP.NET MVC (version 2+), you can get what you need by requesting it from the DependencyResolver like so:

DependencyResolver.Current.GetService<IRepository<User>>();

Or you could probably use ObjectFactory directly.

Steen Tøttrup
  • 3,755
  • 2
  • 22
  • 36
  • I not understand.I use this in my custom membership provider and not write anything in and in Global.asax, in Application_Start() ? – Blocked Jul 31 '12 at 11:29
  • No, you still need to add your custom Dependency Resolver in the Application_Start() or in a bootstrap type class like DependencyResolver.SetResolver(new StructureMapDependencyResolver(container)); where StructureMapDependencyResolver is your custom class and container is your StructureMap IContainer. more info here: http://ericsowell.com/blog/2011/1/20/structuremap-and-asp-net-mvc-3-getting-started – stephenbayer Jul 10 '13 at 20:52