Overloaded Constructors

May 2, 2008 at 11:16 AM
I have a situation in which a class has three overloaded constructors. Unity's documentation states that it always invokes a constructor with maximum number of parameters. In my case 1st constructor takes 1 parameter, 2nd takes 2 parameters and 3rd takes 3 parameters. I may want to invoke the constructor any of the constructor by specifying parameters in the config file. How this can be achieved? Unity's documentation states that

"How Unity Resolves Target Constructors and Parameters
When a target class contains more than one constructor, Unity will use the one you decorate with the InjectionConstructor attribute. If there is more than one constructor, and none carries the InjectionConstructor attribute, Unity will use the constructor with the most parameters. If there is more than one such constructor (more that one of the "longest" with the same number of parameters), Unity will raise an exception. "

May 2, 2008 at 12:06 PM
Try this

 
using SysConfiguration = System.Configuration.Configuration;
namespace Microsoft.Practices.Unity.Tests
{
    public class Customer
    {
        private System.String _name;
        private System.String _surname;
 
        public Customer(string name)
        {
            _name = name;
            _surname = "Default Surname";
        }
        public Customer(System.String name, System.String surname)
        {
            _name = name;
            _surname = surname;
        }
        public System.String GetName
        {
            get { return _name; }
        }
        public System.String GetSurname
        {
            get { return _surname; }
        }
    }
 
[TestMethod]
        [DeploymentItem("ConfiguringInjectionConstructor.config")]
        public void ManyConstructorsTest()
        {
            IUnityContainer container = GetConfiguredContainer("Yazid");
            Customer defaultDb = container.Resolve<Customer>();
            Assert.IsNotNull(defaultDb);
            Assert.AreEqual(defaultDb.GetName, "yazid");
            Assert.AreEqual(defaultDb.GetSurname, "arezki");
            
        }
 
private IUnityContainer GetConfiguredContainer(string containerName)
        {
            UnityConfigurationSection section = GetUnitySection(configFileName);
            IUnityContainer container = new UnityContainer();
            section.Containers[containerName].Configure(container);
            return container;
        }
 
        private UnityConfigurationSection GetUnitySection(string baseName)
        {
            SysConfiguration config = OpenConfigFile(baseName);
            return (UnityConfigurationSection)config.GetSection("unity");
        }
 
        private SysConfiguration OpenConfigFile(string baseName)
        {
            ExeConfigurationFileMap map = new ExeConfigurationFileMap();
            map.ExeConfigFilename = baseName + ".config";
            return ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
        }
}
 
Config section
 
<containers>
 
	   <container name="Yazid">
			<types>
				<type type="Customer" mapTo="Customer">
					<!--<lifetime type="singleton" />-->
					<typeConfig extensionType = "Microsoft.Practices.Unity.Configuration.TypeInjectionElement,
Microsoft.Practices.Unity.Configuration" >
						<constructor>
							<param name="name"  parameterType="System.String">
								<value value="yazid" />
							</param>
							<param name="surname" parameterType="System.String">
								<value value ="arezki" />
							</param>					
						</constructor>
					</typeConfig>
				</type>
			</types>
		</container>
</containers>

This would work if I understood your question
May 2, 2008 at 5:34 PM
The documentation you read describes what Unity does in the absence of specific configuration. The above post shows the general form you use to configure calling a specific constructor.