Vishful thinking…

Windsor Container – Satisfying dependencies

Posted in Uncategorized by viswaug on November 24, 2008

I have been using the Windsor container Dependency Injection (DI) framework for my Inversion of Control (IoC) needs lately and have been highly pleased with the flexibility it offers to satisfy the different dependencies for components. Some of the ways by which Windsor container allows for the dependencies to be satisfies are:

1) Setting constructor parameters
2) Setting properties on components
3) Setting up dependencies on other components that have been configured – the ${…} syntax
4) Setting up common properties that can be used to instantiate different components – the #{…} syntax
5) Passing in the dependencies as a dictionary
6) Using the Factory Facility to obtain the dependency from a custom factory class

That should cover all the scenarios that you would ever come across right? Wrong. I had a weird requirement wherein I wanted to pass in one of the dependencies (as a dictionary) and configure two more dependencies in the configuration file.

Here is the definition of the class that I had to create

public LOBLayer(IFeatureClass featureClass, string layerName, string keyFieldName)

I wanted to configure the ‘layerName’ and the ‘keyFieldName’ constructor parameters in the configuration file and pass in the ‘featureClass’ object in the dictionary parameter of the Resolve(…) method. So, I needed to be able to do the following

<component id=”LOB” lifestyle=”transient” service=”GIS.IGISLayer, GIS” type=”BizLogic.LobLayer, BizLogic”>
  <parameters>
    <layerName>LOB</layerName>
    <keyFieldName>OBJECTID</keyFieldName>
  </parameters>
</component>

and

System.Collections.IDictionary parameters = new System.Collections.Hashtable();
parameters.Add(“featureClass”, featureLayer.FeatureClass);
IGISLayer destinationLayer = _container.Resolve<IGISLayer>( featureType, parameters );

 

Initially, I was skeptical as to whether Windsor would be smart enough to handle it. But, the Windsor container came through for me. I was able to configure two of the constructor parameters in the configuration file and pass in the third dependency in a dictionary to the Resolve(…) method.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: