views:

494

answers:

1

In this Autofac IoC article they show an example of mapping an interface to an implementation with a parameter. You'll find it halfway down the article.

What is the Unity equivalent in XML? Can't use the fluent syntax for what I'm doing. Needs to be an external config file.

UPDATE:
This is the specific piece of code I want to know how to do in Unity -

<component id="DataStoreProvider"
 service="Company.Server.IDataStoreProvider,Company.Server.Interface"
 type="Company.Server.DataStoreProvider,Company.Server.Core">
  <parameters>
    <connectionString>My Connection String</connectionString>
  </parameters>
</component>

Maybe not the greatest example passing in the connection string this way... but you get the point. I'd like to know how to do parameters in XML in Unity.

+5  A: 

You could do this. Refer to this MSDN article

<configuration>
<configSections>
 ...
 <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
 ...
</configSections>
...
<unity>
    <typeAliases>
      <!-- Lifetime manager types -->
      <typeAlias alias="singleton"  type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />
      <typeAlias alias="external" type="Microsoft.Practices.Unity.ExternallyControlledLifetimeManager, Microsoft.Practices.Unity" />
      <typeAlias alias="ILoginService" type="Company.Shared.ILoginService,Company.Shared.Interface" />
      <typeAlias alias="LoginService" type="Company.Server.LoginService,Company.Server.Core" />
      <typeAlias alias="INavigationService" type="Company.Shared.INavigationService,Company.Shared.Interface" />
      <typeAlias alias="NavigationService" type="Company.Server.NavigationService,Company.Server.Core" />
    </typeAliases>
    <containers>
      <container name="Services">
        <types>
          <type type="ILoginService" mapTo="LoginService" />  
    <type type="INavigationService" mapTo="NavigationService" />
        </types>
      </container>      
    </containers>
  </unity> 
  ....

UPDATE: If you look into the MSDN article, there is a section that describes what I believe that fits your requirements.

<type type="IMyService" mapTo="MyDataService" name="DataService">
      <typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement,
                                 Microsoft.Practices.Unity.Configuration">
        <constructor>
          <param name="connectionString" parameterType="string">
            <value value="AdventureWorks"/>
          </param>
          <param name="logger" parameterType="ILogger">
            <dependency />
          </param>
        </constructor> 
      </typeConfig>
    </type>
Vasu Balakrishnan
see update. Can you show me how to pass parameter in your XML that you posted?
tyndall
Thanks, Vasu. Missed that in the first scan through. +1 and the win.
tyndall