Pregunta

Decidí intentar conjurar un conjunto de contenedores para interactuar con FubuMVC. Bueno, lo bueno es que pasa toda la prueba de que el El ensamblaje FubuMVC.Container.StructureMap lo hace. Sin embargo, cuando caí en el FubuSample. Recibí un error de activación.

El error se debe a que en el proveedor de los comportamientos solo estoy llamar a un constructor sin parámetros para construir instancias de comportamiento. Ese parece inaceptable en la vida real.

Así es como está configurado:

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
    }
}

Mi método de carga tiene estos enlaces:

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);
}

En el método Create de mi proveedor está:

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;
}

Entonces, mi pregunta es cómo debo configurar el proveedor para crear un instancia de un servicio cuando todo lo que no sé lo que hará el constructor ¿parece? O mejor dicho, si uso el ConstructorInfo para determinar el ¿Ninject inyectará las dependencias apropiadas?

Esto está usando Ninject 2b, ya que FubuMvc requiere CommonServiceLocator apoyo.

¿Fue útil?

Solución

No importa, descubrí que estaba haciendo las cosas más complicadas de lo que tenían que ser. Aquí está mi nuevo método Crear:

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;
}

Todo lo que tenía que hacer era obtener los tipos de comportamiento concretos y todos estaban contentos.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top