StructureMap Setter Injection not setting property
Asked Answered
K

1

6

I am trying to setup setter/property injection for my MVC project using StructureMap, but I can't seem to get it to set the properties. I am well aware that Constructor injection is the recommended practice, but I have a strict requirement that requires we do it using setter injection, so please hold the comments attempting to tell me otherwise.

I have the normal boilerplate setup code such as the following in my Global.asax

ControllerBuilder.Current.SetControllerFactory(new TestControllerFactory());

ObjectFactory.Initialize(x => {
            x.For<IPaymentService>().Use<PaymentService>();
            x.ForConcreteType<HomeController>().Configure.Setter<IPaymentService>(y => y.PaymentService).IsTheDefault();
            x.SetAllProperties(y =>
            {
                y.OfType<IPaymentService>();
            });

        });

My TestControllerFactory looks like the following:

public class TestControllerFactory:System.Web.Mvc.DefaultControllerFactory
{
    protected  IController GetControllerInstance(Type controllerType)
    {
        if (controllerType == null)
            throw new ArgumentNullException("controllerType");
        return ObjectFactory.GetInstance(controllerType) as IController ;
    }
}

I have the following Service/Implementation class pair

public interface IPaymentService
{

}

public class PaymentService:IPaymentService
{

}

And finally, I have my controller that will have the property that needs to have the concrete payment service implementation injected into it:

public class HomeController:Controller { public IPaymentService Service {get;set;}

 public ActionResult Index(){
        var test = Service... //Service is Null
 }

}

Shown above, the property remains null when I debug.

Additionally, I have tried using the [SetterProperty] just to see if it worked(I have no intention of coupling my controllers with those attributes), and it still didnt work.

I am not sure if I need to do something else, or what the problem might be. I have been using constructor injection with StructureMap for quite awhile.

Kristofor answered 21/9, 2012 at 2:4 Comment(2)
Did you have any luck with Setter Injection? I'm trying in on Action Filters but it doesn't seem to work also.Abolish
After struggling to work with setter injection, I decided to move away from it.Connally
A
3

Try dropping this line:

x.ForConcreteType<HomeController>().Configure
  .Setter<IPaymentService>(y => y.PaymentService).IsTheDefault();

It shouldn't be necessary.

Given the following controller:

public class HomeController : Controller
{
    public IMsgService Service { get; set; }

    public ActionResult Index()
    {
        return Content(Service.GetMessage());
    }
}

This was all that was required to configure StructureMap to set the property:

ObjectFactory.Initialize(cfg =>
{
    cfg.For<IMsgService>().Use<MyMsgService>();

    cfg.SetAllProperties(prop =>
    {
        prop.OfType<IMsgService>();
    });
});

ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory());
Almund answered 27/12, 2013 at 21:52 Comment(4)
This solution just works if the property is in a controller class , in my case the property is non-controller class so your answer wouldn't help me.Indite
It should not make any differences providing you have configured StructureMap with all the dependencies in the object graph.Almund
I put my property IUnitOfWork in my controller and it gets the value but when I put it in another class MyServiceFactory and instantiate the MyServiceFactory class in my controller the IUnitOfWork property is null.Indite
If you instantiate MyServiceFactory then StructureMap will not resolve any dependencies. You either need to make MyServiceFactory a dependency of your controller or call ObjectFactory.GetInstance<MyServiceFactory> within your controller.Almund

© 2022 - 2024 — McMap. All rights reserved.