inheritance: concrete base and sub class

Jul 21, 2009 at 10:13 AM

Hello all,

Our clients have supplied us a wsdl file from which we've generated a concrete class (RegistrationService). I've created a stub class (and set it to be a subclass of the RegistrationService) and I would like to use this stub, through dependency injection, until I'm ready to use the live web service. So I've got the following in the app.config:

      <typeAlias alias="RegistrationServiceStub" type="Common.Stubs.RegistrationServiceStub, Common"/>
      <typeAlias alias="RegistrationService" type="Common.RegistrationService, Common"/>

    </typeAliases>
    <containers>
      <container>
        <types>
          <type type="RegistrationService" mapTo="RegistrationServiceStub">
            <typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement,Microsoft.Practices.Unity.Configuration">
            </typeConfig>
            <lifetime type="singleton"/>
          </type>
        </types>
      </container>
    </containers>

Unfortunately, when I run the following code:

_service = ConfigurationContainer.Container.Resolve<RegistrationService>();

It instantiates the actual RegistrationService class and not the Stub. Anybody see what I'm doing wrong? Is it not possible to tell Unity to either use a concrete base class or it's sub class through config settings? Or do I have to create an interface (or abstract empty class) and change both RegistrationService and the stub to implement it? The problem with that is that whenever we regenerate the RegistrationService we'll have to manually modify it so that it implements that interface.

Any suggestions?

Thanks

Sidharth

 

Jul 22, 2009 at 6:33 AM

I just wrote the following test:

    [TestClass]
    public class When_resolving_concrete_class_given_a_mapping_through_configuration_to_a_derived_type : ArrangeActAssert
    {
        private IUnityContainer container;
        private RegistrationService service;

        public override void Arrange()
        {
            container = new UnityContainer();
            var section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
            section.Containers.Default.Configure(container);
        }

        public override void Act()
        {
            service = container.Resolve<RegistrationService>();
        }

        [TestMethod]
        public void Then_resolved_object_is_instance_of_derived_class()
        {
            Assert.IsInstanceOfType(service, typeof(RegistrationServiceStub));
        }
    }

 

With this config file:

 

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
  </configSections>

  <unity>
    <typeAliases>
      <typeAlias alias="singleton" type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />
      <typeAlias alias="RegistrationServiceStub" type="UnityConcreteClasses.RegistrationServiceStub, UnityConcreteClasses"/>
      <typeAlias alias="RegistrationService" type="UnityConcreteClasses.RegistrationService, UnityConcreteClasses"/>
    </typeAliases>

    <containers>
      <container>
        <types>
          <type type="RegistrationService" mapTo="RegistrationServiceStub">
            <lifetime type="singleton"/>
          </type>
        </types>
      </container>
    </containers>
  </unity>
</configuration>

And the test passed. In general what you want to do just works. I'd double check how you're getting and configuring your container. Is it using the config section you think it is?