Constructor injection with dynamic type mapping in config

Oct 7, 2008 at 8:00 PM
Edited Oct 7, 2008 at 9:16 PM
Hi experts,

there is a client app which uses a service agent in order to talk to a (WCF) service.

I am trying to inject the actual ServiceAgent instance through Unity so that the client can just talk to an IServiceAgent interface.
This works well:

---
IUnityContainer container = new UnityContainer();
var section = ConfigurationManager.GetSection("unity")
    as UnityConfigurationSection;

if (section == null)
    throw new InvalidOperationException("No Unity configuration section found.");

section.Containers["tt"].Configure(container);

container.Configure<InjectedMembers>()
    .ConfigureInjectionFor<ServiceAgent>(
        new InjectionConstructor(
            relayedCommunication, duplexConnection));

return container.Resolve<IServiceAgent>();
---

Please note that relayedCommunication and duplexConnection are both bool values which are dependent on what the user chose.

Now, I want to get rid of the ServiceAgent dependency above in ConfigureInjectionFor<> - I would rather have it in the configuration file.

---
<unity>
  <containers>
    <container name="tecTeacher">
      <types>
        <type type="Thinktecture..IServiceAgent, ServiceAgentInterfacesLib"
            mapTo="Thinktecture.ServiceAgent, ServiceAgentLib" />
      </types>
    </container>
  </containers>
</unity>
---

What I am looking is is something like ConfigureInjectionFor but not taking the actual target type but rather a hint into a configuration entry where the mapping is specified.

Is this possible?

Thanks,
Christian
Oct 7, 2008 at 10:03 PM
Read the documentation for the <typeConfig> element.

-Chris
Oct 7, 2008 at 10:22 PM
Hi Chris,

I looked at it - but honestly I cannot see how I can use it to solve my above problem: variable ctor parameters in the code and type mapping in config.

Thanks for any further hint.

Cheers,
-Christian
Oct 7, 2008 at 10:48 PM
BTW: I saw this related discussion:
http://www.codeplex.com/unity/Thread/View.aspx?ThreadId=26895

Seems like my scenario is currently not supported?

Thanks,
-Christian
Oct 8, 2008 at 5:45 PM

I put this together rather quickly, but it allows you to make your implementation of IServiceAgent Pluggable via the config file and pass in runtime values for its configuration.

using System;
using System.Configuration;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;

namespace ConsoleApplication3
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            IUnityContainer container = new UnityContainer();
            UnityConfigurationSection section = ConfigurationManager.GetSection("unity")
                                                as UnityConfigurationSection;

            if (section == null)
                throw new InvalidOperationException("No Unity configuration section found.");

            section.Containers["tecTeacher"].Configure(container);

            var config = new ServiceAgentConfig(true, true);

            container.RegisterInstance(config);

            var serviceAgent = container.Resolve<IServiceAgent>();
        }
    }

    public interface IServiceAgent
    {
    }

    public class ServiceAgent : IServiceAgent
    {
        private ServiceAgentConfig _config;

        public ServiceAgent(ServiceAgentConfig config)
        {
            _config = config;
        }
    }

    public class ServiceAgentConfig
    {
        public bool RelayedCommunication { get; private set; }
        public bool DuplexCommunication { get; private set; }

        public ServiceAgentConfig(bool relayedCommunication, bool duplexCommunication)
        {
            RelayedCommunication = relayedCommunication;
            DuplexCommunication = duplexCommunication;
        }
    }
}


Regards,

Dave

David Hayden
PnPGuidance

Oct 8, 2008 at 6:20 PM
Wonderful, David.
After refactoring my code a bit - it works. A bit 'unexpected' in the API ;) but still fine.

Thanks a lot!

Cheers,
-Christian