static void Main(string[] args)
        {
            //创建容器对象
            IUnityContainer mycontainer = new UnityContainer();

            try
            {
                //配置文件注册
                UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
                section.Configure(mycontainer); //默认调用第一个容器

                //section.Containers["test"].Configure(mycontainer);//通过名称调用
                //调用依赖
                ISqlHelper mysql = mycontainer.Resolve <ISqlHelper>();
                Console.WriteLine(mysql.SqlConnection());

                IOtherHelper Imysql = mycontainer.Resolve <IOtherHelper>();
                Console.WriteLine(Imysql.GetSqlConnection());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadKey();
        }
Exemple #2
0
        static void Main(string[] args)
        {
            IUnityContainer mycontainer = new UnityContainer();


            //已有对象实例的配置容器注册
            MysqlHelper d = new MysqlHelper();

            mycontainer.RegisterInstance <ISqlHelper>(d);

            //类型的配置容器注册
            mycontainer.RegisterType <ISqlHelper, MysqlHelper>();

            //配置文件注册
            UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");

            section.Configure(mycontainer, "MysqlHelper");
            //mycontainer.LoadConfiguration(section);

            //调用依赖
            ISqlHelper mysql = mycontainer.Resolve <ISqlHelper>();

            Console.WriteLine(mysql.SqlConnection());



            //构造函数注入
            mycontainer.RegisterType <IOtherHelper, MyOtherHelper>();
            IOtherHelper other = mycontainer.Resolve <IOtherHelper>();

            Console.WriteLine(other.GetSqlConnection());


            //test
            MssqlHelper m = new MssqlHelper();

            mycontainer.RegisterInstance <ISqlHelper>(m);
            mycontainer.RegisterType <ISqlHelper, MssqlHelper>();
            UnityConfigurationSection mssession = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");

            section.Configure(mycontainer, "MssqlHelper");
            ISqlHelper mssql = mycontainer.Resolve <ISqlHelper>();

            Console.WriteLine(mssql.SqlConnection());

            Console.ReadKey();
        }
Exemple #3
0
        public static void Demo3()
        {
            IUnityContainer mycontainer = new UnityContainer();

            //mysql
            mycontainer.RegisterInstance <ISqlHelper>(new MysqlHelper());
            mycontainer.RegisterType <IOtherHelper, MyOtherHelper>();
            IOtherHelper mysql = mycontainer.Resolve <IOtherHelper>();

            Console.WriteLine(mysql.GetSqlConnection());

            //mssql
            mycontainer.RegisterInstance <ISqlHelper>(new MssqlHelper());
            mycontainer.RegisterType <IOtherHelper, MyOtherHelper>();
            IOtherHelper mssql = mycontainer.Resolve <IOtherHelper>();

            Console.WriteLine(mssql.GetSqlConnection());
        }