Max
Max

Reputation: 601

Parameters of the TypedFactory are not bypassed further

  1. There is a typed factory:

    public interface IDataProviderFactory
    {
        IDataProvider Create(ConfigurationItem configurationItem);
        void Release(IDataProvider dataProvider);
    }
    
  2. There are several implementations of IDataProvider

  3. Some implementations are depend on ICalculator
  4. There are several implementations of ICalculator
  5. There is a configuration class:

    public class ConfigurationItem
    {
         public CalculatorsEnum CalculatorsEnum { get; set; }
         public DataPriversEnum DataPriversEnum { get; set; }        
    }
    

When I am trying to call the factory's method Create, Windsor is trying to resolve the IDataProvider's dependencies, which is ICalculator.

Here I need the container to use the information from the original parameter so that to understand which version of the ICalculator to use.

Update

I would like to add some details:

  1. There is a class where I use IDataProviderFactory. There I control the lifecycle of the IDataProviders, I create and destroy them from time to time using the factory. So at this level I don't want to know anything about the IDataProvider implementation- there might be the DataProvider without a calculator.
  2. At the beginning I had my own implementation of the IDataProviderFactory, where I could resolve ICalculator and inject it into the DataProvider. But when it comes to the recycling, I was disposing the DataProvider, but it appeared that disposing is not enough in case with Windsor when you manually resolve something, you need explicitly release it.

Possible solutions:

Upvotes: 0

Views: 77

Answers (1)

Phil Degenhardt
Phil Degenhardt

Reputation: 7274

Constructor parameters are only passed to the constructor of the top level service being resolved. This is by design.

The recommended solution would be to make a factory that resolves both ICalculator and IDataProvider. When resolving you would explicitly resolve ICalculator and then pass that into the factory function for IDataProvider.

See this question for further discussion.

Update

Yes every Resolve should have a matching Release, and this is true whether using factories or the container directly.

It would only be speculation to suggest what "the Windsor way" of addressing your particular scenario might be.

Windsor provides a number of different ways of specifying inline dependencies. See here for further details. Generally the methods outlined are preferable to using factories etc. because the resolving/releasing is managed by the container.

Upvotes: 1

Related Questions