1
我決定嘗試召喚一個容器組件來與FubuMVC的 交互。那麼很酷的部分是它通過了FubuMVC.Container.StructureMap程序集所做的所有測試。但是,當我將其放入FubuSample中時,它將 刪除。我收到激活錯誤。當構造函數的類型未知時,如何創建Ninject提供程序?
錯誤是因爲在行爲的提供者中,我只調用無參數構造函數來構建行爲實例。那看起來 在現實生活中是不可接受的。
這裏是它是如何設置:
public class TestBehavior2 : IControllerActionBehavior
{
public IControllerActionBehavior InsideBehavior { get; set; }
public IInvocationResult Result { get; set; }
protected FubuConfiguration Configuration { get; set; }
protected FubuConventions Conventions { get; set; }
public TestBehavior2(FubuConventions conventions, FubuConfiguration config)
{
Conventions = conventions;
Configuration = config;
}
public OUTPUT Invoke<INPUT, OUTPUT>(INPUT input, Func<INPUT, OUTPUT> func)
where INPUT : class
where OUTPUT : class
{
// Invocation stuff
}
}
public class TestBehavior : IControllerActionBehavior
{
public IControllerActionBehavior InsideBehavior { get; set; }
public IInvocationResult Result { get; set; }
public OUTPUT Invoke<INPUT, OUTPUT>(INPUT input, Func<INPUT, OUTPUT> func)
where INPUT : class
where OUTPUT : class
{
// Invocation stuff
}
}
我的Load方法中有這些結合:
foreach (var actionConfig in _configuration.GetControllerActionConfigs())
{
Bind(actionConfig.ControllerType).ToSelf();
Bind<IControllerActionBehavior>()
.ToProvider(new BehaviorInstanceProvider(actionConfig))
.WhenParentNamed(actionConfig.UniqueID);
Bind<IControllerActionInvoker>().To(actionConfig.InvokerType)
.Named(actionConfig.UniqueID);
}
在我的供應商的創建方法是:
public object Create(IContext context)
{
var behavior = ConfigureInstance(context);
foreach (var type in _config.GetBehaviors().Reverse())
{
IControllerActionBehavior previousBehavior = behavior;
behavior = (IControllerActionBehavior)Activator.CreateInstance(type);
t.GetProperty(INSIDE_PROP_NAME).SetValue(behavior, temp, null);
}
return behavior;
}
所以我的問題是我應該如何設置提供商創建一個 實例的服務時,我不知道構造函數 是什麼樣子?或者,如果我使用ConstructorInfo來確定構造函數Ninject是否注入了適當的依賴關係?
這是使用Ninject 2b,因爲FubuMvc需要CommonServiceLocator 支持。
您能否提供一些示例代碼?我不確定你是如何使用提供者? – 2009-09-15 22:43:12
請注意,Ninject會解析你的提供者的構造函數中的任何參數,如果可以的話。 – 2010-01-20 07:43:45