Problem in Unity application Block

Apr 1, 2010 at 2:22 PM

Hi I am using Unity Application block for crating object at run time. i have two classes "SqlServer", "OracleServer" which implement IDatabase Interface and save data in their respective datbases.

Now there is Customer and CustomerSecond class which implement ICustomer and  decide which data base should be used.

Code is working fine if i call respective method of these classes with help of resolveall method.

I m moving one step ahead. there can be multiple type of customer class as shown above(Customer and CustomerSecond) which will use SqlServer and OracleServer class to save their data. as explain above.

So i am creating one more class ManageCustomer and getting obect of respective Customer class defined in Config File with unity application block. while doing so i am getting error.

here is my config file:

<configSections>    <section name="unity" type ="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration"/>

      <container name="Sql">     
          <type type="UnitySample.IDatabase,UnitySample" mapTo="UnitySample.SqlServer,UnitySample" name="Sql" />
          <type type="UnitySample.IDatabase,UnitySample" mapTo="UnitySample.OracleServer,UnitySample" name="Oracle"/>
          <type type="UnitySample.ICustomer,UnitySample" mapTo="UnitySample.CustomerSecond,UnitySample" name="Customer" />

Error is :

Resolution of the dependency failed, type = "ICustomer", name = "Customer". Exception message is: The current build operation (build key Build Key[UnitySample.CustomerSecond, Customer]) failed: The value for the property "DataBase" could not be resolved. (Strategy type Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy, index 2)

Code for using first level of Unity app block

public class CustomerSecond:ICustomer
        public IDatabase DataBase { get; set; }

        public void Save(string str)
            IUnityContainer containet = new UnityContainer();

            UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");


            //IDatabase obj = containet.Resolve<IDatabase>();
            IEnumerable<IDatabase> obj = containet.ResolveAll<IDatabase>();
            IEnumerator<IDatabase> enu = obj.GetEnumerator();

            while (enu.MoveNext())

Code for second level of using Unity App block

public class GetFactory
        public ICustomer Customer { get; set; }
        public static void getCustomers()
            IUnityContainer containet = new UnityContainer();

            UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");


            ICustomer cust = containet.Resolve<ICustomer>("Customer");


            //return cust;

I am not able to get solution.

Apr 8, 2010 at 3:08 PM


This is the discussions for the CodePlex site itself. You will probably get a better response by posting this on the project's discussions.