views:

239

answers:

1

I decided to try to conjure up a container assembly to interact with FubuMVC. Well the cool part is that it pass all the test that the FubuMVC.Container.StructureMap assembly does. However, when I dropped it into the FubuSample. I received an activation error.

The error is because in the provider for the behaviors I'm only calling a parameterless constructor to build behavior instances. That is unacceptable in real life it would seem.

Here is how it is set up:

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 method has these binding in it:

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

In my provider's Create method is:

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

So my question is how should I set up to provider to create an instance of a service when all I don't know what the constructor will look like? Or rather if I use the ConstructorInfo to ascertain the constructor will Ninject inject the appropriate dependencies?

This is using Ninject 2b, since FubuMvc requires CommonServiceLocator support.

A: 

Nevermind, I found that I was making thing more complicated than it had to be. Here is my new Create method:

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

All I had to do was just get the concrete behavior types and everybody was happy.

Thedric Walker