我想使用StructureMap为我的MVC项目设置setter/property注入,但我似乎无法得到它来设置属性。我很清楚Constructor注入是推荐的做法,但我有一个严格的要求,要求我们使用setter注入来进行注入,所以请保留试图告诉我的注释。StructureMap设置注入没有设置属性
我有正常的样板设置代码,如我的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>();
});
});
我TestControllerFactory如下所示:
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 ;
}
}
我有以下服务/实现类对
public interface IPaymentService
{
}
public class PaymentService:IPaymentService
{
}
最后,我有我的控制器, l具备,需要有注入到它的具体支付服务实现属性:
公共类HomeController的:控制 { 公共IPaymentService服务{获取;集;}
public ActionResult Index(){
var test = Service... //Service is Null
}
}
如上所示,当我调试时,该属性保持为空。
此外,我已经尝试使用[SetterProperty]只是为了看它是否工作(我没有打算与这些属性耦合我的控制器),它仍然没有工作。
我不确定是否需要做别的事情,或者可能出现什么问题。我一直在使用StructureMap构造函数注入。
你有Setter注入任何运气?我试图在动作过滤器,但它似乎也没有工作。 – ntombela
经过努力注入二传手后,我决定离开它。 – Joel