4

I am using the Unity framework as IoC container.

My Config looks some like this:

<unity>
  <container>
    <register type="Namespace1.IFoo, FooAsm"
              mapTo="Namespace2.Bar, BarAsm">
    </register>
</conainer>

I would like to register a container to use a factory method. How do i achive it using the app.config?

I am looking for something like this:

<unity>
  <container>
    <register type="Namespace1.IFoo, FooAsm"
              factory="Namespace2.Bar, BarAsm"
              method="create">
    </register>
  </conainer>
</unity>

Any suggestsions?

Steven
  • 166,672
  • 24
  • 332
  • 435
Jaster
  • 8,255
  • 3
  • 34
  • 60
  • 1
    Switch to code based configuration, seriously. If you can, stay away from configuring the container through XML, because it is brittle and error prone. Code based configuration is much more flexible and gives you compile time support. – Steven Mar 30 '12 at 12:50
  • how is code based configuration more flexible? Either you add reflection or you need strong references. Both is not realy flexible. – Jaster Mar 30 '12 at 12:52
  • It is more flexible, because you can register lambda expressions for instance. What's the use of not having a strong reference? Since you name the type in your XML, you implicitly still have a reference to the type, but now you loose compile time checking. – Steven Mar 30 '12 at 12:55
  • 1
    I too would advise code based configuration, the advantages offered by compile time checking far outweigh the benefits offered by XML (which I can't really think of any) – Lukazoid Mar 30 '12 at 12:59
  • 1
    Possible duplicate of [How to declare the Unity InjectionFactory in XML configuration](http://stackoverflow.com/q/7143089/921321) – Lukazoid Mar 30 '12 at 13:02
  • Ok so you recompile and redeploy your whole application whenever you change a plugin or a layer? Simulated devices require recompilation? Or do you use "flags" to emmit simulated behavior? Lambda expressions are just method calls, its not even an argument. Please stop spamming if you can not help with my request. I don't want to offend anyone but sometimes I am really wondering if people writing such comments have ever released a _large_ project -including the complete lifecycle of a software... – Jaster Mar 30 '12 at 13:06
  • 2
    XML is indeed well suited for part of your configuration that needs to change during deployment, but that doesn't mean that your complete configuration should be in XML. On the contrary: put the configuration in code as much as you can and only put things that need to be changeable in XML (unity allows you to mix this). Or… use a plugin model: Load assemblies dynamically from a plugin directory during startup and register their types in the container. This way you can do it all in code, and don't have to change the XML configuration when the plugins change. – Steven Mar 30 '12 at 13:29

2 Answers2

4

This thread offers a nice response about how to add support of Factory methods for Unity. I fact you have to download this bitbucket source (and change the references to 4.0 framework).
If you now add Generic support for Unity you get a pretty awesome solution that might look like this:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration"/>
  </configSections>
  <unity xmlns="schemas.microsoft.com/practices/2010/unity">
    <sectionExtension type="Unity.FactoryConfig.FactoryConfigExtension, Unity.FactoryConfig"/>
    <alias alias="Factory" type="Namespace1.GenericFactory`1, asm1"/>
    <container>
      <register type="Namespace1.ITest, asm1">
        <factory type="Factory[[Namespace1.ITest, asm1]]" method="Create" />
      </register>
    </container>
  </unity>
</configuration>

Support of generic Factories for unity framework using xml configuration!
Thanks for all comments :)

Community
  • 1
  • 1
Jaster
  • 8,255
  • 3
  • 34
  • 60
1

I don't think there is a built-in way to do this currently. I usually just register the factory itself (Map IMyFactory -to-> MyFactoryImpl) then inject the factory into the classes that need it, and have the classes call the factory.

However, I think you can probably make something call a factory straight from Unity by making an extension for Unity. There are a couple mentioned in the answers to this question: How to create objects using a static factory method?

Community
  • 1
  • 1
CodingWithSpike
  • 42,906
  • 18
  • 101
  • 138
  • 1
    http://stackoverflow.com/questions/3815581/microsoft-unity-issue-with-resolve <- it seems there is a factory support, but the samples i found go codewise. didn't manage to translate them into xml yet – Jaster Mar 30 '12 at 13:11