开发者

How do i pass a dynamic dependency to a typedfactory instance at the bottom of the decorator chain?

I am fond of using decorator chains instead of inheritance, and as long as my services have Singleton or Transient lifestyles (and their dependencies are scoped in the same manner) this works perfectly. I've started to use the TypedFactoryFacility to generate factories for services which require some sort of input which can not be resolved through the ioc container. My headache starts when something deep in the decorator chain requires a factory and Windsor refuses to pass inline dependencies down the inheritance chain. As I understand this is by design, but I don't agree that this breaks encapsulation when you are using a factory which explicitly states what you need to produce t开发者_StackOverflowhe output (I think Windsor should pass inline dependencies to components which are factory resolvable, but I'm guessing its not that straight forward. Anyway, at the end of the day I end up exposing my decorator chain like this:

    public ISessionInputOutput Create(Session session)
    {
        IServerInput baseInput = _inputFactory.GetBaseInput(session.CommunicationSocketStream);
        IServerInput threadSafeInput = _inputFactory.GetThreadSafeInput(baseInput);
        IServerOutput output = _outputFactory.Create(session.CommunicationSocketStream);

        ISessionInputOutput baseIO = _sessionIOFactory.GetBaseIO(baseInput, output);
        ISessionInputOutput commandHandlerIO = _sessionIOFactory.GetCommandHandlerIO(baseIO, session);
        ISessionInputOutput errorHandlingIO = _sessionIOFactory.GetErrorHandlingIO(commandHandlerIO, session);

        _releaseInfo.Add(errorHandlingIO, new CreatedIOInformation(baseInput, threadSafeInput, output, baseIO, commandHandlerIO));

        return errorHandlingIO;
    }

In the example I am using 3 factory proxies, and manually combine the output to produce the decorator chain.

I can't simply use ServiceOverrides as I usually do to introduce new services. This doesn't feel right and I am hoping that someone can suggest a better solution what I've used above.

I've also tried to solve this by using child containers, but this introduces a bit more ioc details into the stack than I like.


If you really feel you need to enable passing down the dependencies you can enable that by overriding RebuildContextForParameter method in DefaultDependencyResolver and passing true for propagateInlineDependencies.

Be aware that this is a double-edged sword. You can also filter this out and enable this behavior only when you're resolving one of the few services for which you need this, and stick to the default behavior for all other.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜