我决定尝试召唤一个与之交互的容器组件 FubuMVC。好吧,很酷的部分是它通过了所有的测试 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
    }
}

My 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注入适当的依赖项吗?

这是使用Ninject 2b,因为FubuMvc需要CommonServiceLocator 支持。

有帮助吗?

解决方案

没关系,我发现我制作的东西比以前更复杂。这是我新的Create方法:

public object Create(IContext context)
{
    IControllerActionBehavior behavior = context.Kernel.Get<DefaultBehavior>();
    _config.GetBehaviors().Reverse().Each(t =>
                                          {
                                              var temp = behavior;
                                              behavior = (IControllerActionBehavior) context.Kernel.Get(t);
                                              t.GetProperty(INSIDE_PROP_NAME).SetValue(behavior, temp, null);
                                          });
    _type = behavior.GetType();
    return behavior;
}

我所要做的就是获得具体的行为类型,每个人都很开心。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top