Wish List Item: Reusable Injection Definitions

Jan 17, 2009 at 4:00 PM
If this can be done today, then I apologize and pls let me know how.
I would  like a way to specify an injectable set of variables that can be easily reused with any type I want.

Most of classes in a layer use the same set of parameters. For instance, my DAOs use the same connection string. My services require more or less the same set of DAOs. My services and DAOs use the same loggers. I don't want to have to specify these for every type being looked up.

I was thinking that in the configuration xml (pls extend this to the programmatic config as well) it would be great if I could do this:

<typeConfigs> <!-- this is new -->
  <typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement, ..."
                name="myDAOParameters"> <!-- the name is new (I believe) -->
                <param name="connectionString" parameterType="string">
                    <value value="AdventureWorks"/>
  <typeConfig extensionType="..." name="myServiceParameters">
              <param name="SomeDAO" parameterType="ISomeDAO">
                  <dependency />
  <typeConfig extensionType="..." name="myCommonParams">
            <param name="logger" parameterType="ILogger">
                  <dependency />

  <type type="IMyFirstDAO" mapTo="MyFirstDAOImpl">
     <typeConfig key="myDAOParameters"/>
     <typeConfig key="myCommonParams"/>
  <type type="IMyMainService" mapTo="MyMainServiceImpl">
     <typeConfig key="myServiceParameters"/>
     <typeConfig key="myCommonParams"/>

Jan 17, 2009 at 7:54 PM


To inject your connection string automatically create an interface something like

public interface IConfigurationService
string ConnectionString { get; }

Container.RegisterType<IConfigurationService, ConfigurationService>(new ..................LifetimeManager());

Then register a type that returns the value you require.  Your other objects should then be changed to accept this interface type rather than a string and Unity will inject it automatically.