Configuration Sample

Feb 20, 2008 at 4:16 AM
Using .net 2.0 Configuration, I'd imagine that config would look something like:

  <unity>
    <containers>
      <container name="myContainer">
        <types>
          <add type="MyNamespace.IFoo, MyAssembly" name="foo">
            <extensions>
              <add type="Microsoft.Practices.Unity.MappingConfigurationElement" mapTo="MyNamespace.Foo, MyAssembly">
                <constructor>
                  <parameters>
                    <add name="date" type="System.DateTime" index="0">
                      <resolver type="Microsoft.Practices.Unity.Resolver.ConstantValueParameter" value="2/19/2008" format="yyyy-mm-dd"/>
                    </add>
                  </parameters>
                </constructor>
                <properties>
                  <add name="Bar" type="MyNamespace.IBar" index="0">
                    <resolver type="Microsoft.Practices.Unity.Resolver.DependencyParameter" notFoundBehavior="BuildUp"/>
                  </add>
                </properties>
                <methods>
                  <add name="InitZaz">
                    <parameters>
                      <add name="zaz" type="MyNamespace.IZaz" index="0">
                        <resolver type="Microsoft.Practices.Unity.Resolver.MappingDependencyParameter" mapTo="MyNamespace.Zaz, MyAssembly">
                            <constructor>
                              <parameters>
                                <add name="i" type="System.Int32" index="0">
                                  <resolver type="Microsoft.Practices.Unity.Resolver.ConstantValueParameter" value="1"/>
                                </add>
                              </parameters>
                            </constructor>
                        </resolver>
                      </add>
                    </parameters>
                  </add>
                </methods>
              </add>
              <add type="Microsoft.Practices.Unity.LifetimeContainerReferenceConfigurationElement" lifetime="perSession"/>
            </extensions>
          </add>
          <add type="MyNamespace.IBar, MyAssembly">
            <extensions>
              <add type="Microsoft.Practices.MappingConfigurationElement" mapTo="MyNamespace.Bar"/>
            </extensions>
          </add>
        </types>
        <extensions>
          <add type="Microsoft.Practices.Unity.LifetimeContainerConfigurationElement" lifetimeName="perSession" lifetimeContainerType="Microsoft.Practices.Unity.PerSessionLifetimeContainer"/>
        </extensions>
      </container>
    </containers>
  </unity>

The main goals here are that multiple extensions should be able to add extra config contextual to the type being build and value parameters as well as dependencies should be supported as injected values. Dependencies could be specified as "local" or as shared. Multiple value parameter strategies should be supported such as reading from appsettings, stratic properties through reflection, instance properties on registered types in the container, ... Also support for type aliases would be helpful.

Parameters should be identified either by name, type or index in order to find the right ctor or method overload to inject.

However I'm not sure if a more compact approach similar to something as below (which cannot use .net 2.0 Configuration) wouldn't be better:

  <unity>
    <containers>
      <container name="myContainer">
        <types>
          <add type="MyNamespace.IFoo, MyAssembly" name="foo">
            <mapTo type="MyNamespace.Foo, MyAssembly" lifetime="perSession">
              <constructor>
                <date value="2/19/2008" format="yyyy-mm-dd"/>
              </constructor>
              <properties>
                <Bar notFoundBehavior="BuildUp"/>
              </properties>
              <methods>
                <InitZaz>
                  <parameter type="MyNamespace.IZaz">
                    <mapTo type="MyNamespace.Zaz, MyAssembly">
                      <constructor>
                        <i value="1"/>
                      </constructor>
                    </mapTo>
                  </parameter>
                </InitZaz>
              </methods>
            </mapTo>
          </add>
          <add type="MyNamespace.IBar, MyAssembly">
            <mapTo type="MyNamespace.Bar, MyAssembly"/>
          </add>
        </types>
        <lifeTimes>
          <perSession type="Microsoft.Practices.Unity.PerSessionLifetimeContainer"/>
        </lifeTimes>
      </container>
    </containers>
  </unity>

With good designer support, I wouldn't care much having pretty complex and verbose xml.
Feb 20, 2008 at 4:59 PM
Hellz yes... it's all about that designer support! This looks very good.