How I register the IContinuationDirector in fubuMVC

I’m blogging about my experiences with fubuMVC and an interesting thing I came across recently was the lack of a registration for a concrete IContinuationDirector in the structure map container.

The IContinuationDirector abstraction enables you to transfer or redirect a request efficiently and I have a need for this in quite a few places. Here’s the interface:

namespace FubuMVC.Core.Continuations
{
  public interface IContinuationDirector
  {
    void InvokeNextBehavior();

    void RedirectTo(object input);

    void RedirectToCall(ActionCall call);

    void TransferTo(object input);

    void TransferToCall(ActionCall call);
  }
}

So I registered the concrete implementation that exists in fubuMVC:

 x.For<IContinuationDirector>().Use(c => new ContinuationHandler(c.GetInstance<IUrlRegistry>(),
                                                                c.GetInstance<IOutputWriter>(),
                                                               c.GetInstance<IFubuRequest>(),
                                                             c.GetInstance<IPartialFactory>()));

But at runtime, this known problem reared its head:

Unable to cast object of type 'FubuMVC.Core.Diagnostics.Tracing.BehaviorTracer'
to type 'FubuMVC.Core.Continuations.IContinuationDirector'.

The solution was easy – defer creation of the continuation handler until it is required by the behaviour that needs it and it is then not wrapped by any diagnostics:

x.For<Func<IContinuationDirector>>().Use(c => 
() => new ContinuationHandler(c.GetInstance<IUrlRegistry>(), c.GetInstance<IOutputWriter>(), c.GetInstance<IFubuRequest>(), c.GetInstance<IPartialFactory>()));

So now I just take a Func<IContinuationDirector> as a dependency rather than the interface and my problems are solved.

Note: there could very well be a known solution to this issue by now, so I’m merely illustrating my own method for resolving this. Also note that I don’t use it as a behaviour node, but as a redirection/transfer abstraction in my actions.



0 comments: