【发布时间】:2010-11-28 06:10:10
【问题描述】:
我决定尝试变出一个容器组件来与之交互 富布MVC。很酷的部分是它通过了所有测试 FubuMVC.Container.StructureMap 程序集确实如此。然而,当我掉下 它进入 FubuSample。我收到了激活错误。
错误是因为在提供者的行为中,我只是 调用无参数构造函数来构建行为实例。那 在现实生活中似乎是不可接受的。
以下是它的设置方式:
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);
}
在我的提供者的 Create 方法中是:
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 2b,因为 FubuMvc 需要 CommonServiceLocator 支持。
【问题讨论】:
-
您能提供一些示例代码吗?我不确定您是如何使用该提供商的?
-
请注意,如果可以的话,Ninject 会解析 Provider 构造函数中的所有参数。
标签: .net dependency-injection ioc-container ninject fubumvc