static void Main(string[] args)
        {
            int money;

            using (IUnityContainer container = new UnityContainer())
            {
                UnityConfigurationSection section =
                    (UnityConfigurationSection)ConfigurationManager.GetSection("unity");

                section.Configure(container, "Employee");

                Salary ms1 = container.Resolve <Salary>();

                money = ms1.Calculate(80, 100, 1);
                Console.WriteLine("Unity =>" + money);

                section.Configure(container, "Boss");

                ms1 = container.Resolve <Salary>();

                money = ms1.Calculate(80, 100, 1);
                Console.WriteLine("Boss =>" + money);

                Console.ReadKey();
            }
        }
Exemple #2
0
 static ServiceLocatorFactory()
 {
     createServiceLocator = name =>
     {
         IUnityContainer container;
         name = name ?? string.Empty;
         if (!containers.ContainsKey(name))
         {
             container = new UnityContainer();
             UnityConfigurationSection config = ConfigurationManager.GetSection(UnityConfigurationSection.SectionName) as UnityConfigurationSection;
             if (null != config)
             {
                 if (string.IsNullOrEmpty(name))
                 {
                     config.Configure(container);
                 }
                 else
                 {
                     config.Configure(container, name);
                 }
             }
             lock (createServiceLocator)
             {
                 containers[name] = container;
             }
         }
         else
         {
             container = containers[name];
         }
         return(new UnityServiceLocator(container));
     };
 }
        /// <summary>
        /// 注册IUnityContainer
        /// </summary>
        /// <param name="section"></param>
        /// <param name="containerName"></param>
        /// <param name="parentContainer"></param>
        /// <returns></returns>
        public static IUnityContainer GetContainer(UnityConfigurationSection section, string containerName, IUnityContainer parentContainer)
        {
            if (section == null)
            {
                throw new ArgumentNullException("section");
            }

            IUnityContainer unityContainer;

            if (parentContainer == null)
            {
                unityContainer = new UnityContainer();
            }
            else
            {
                unityContainer = parentContainer.CreateChildContainer();
            }

            if (string.IsNullOrEmpty(containerName))
            {
                section.Configure(unityContainer);
            }
            else
            {
                section.Configure(unityContainer, containerName);
            }

            return(unityContainer);
        }
Exemple #4
0
        public static void LoadUnityConfigFile(IUnityContainer container, string containerName, string exeConfigFileName)
        {
            ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap
            {
                ExeConfigFilename = exeConfigFileName
            };

            UnityConfigurationSection unitySection =
                ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None)
                .GetSection(UnityConfigurationSection.SectionName) as UnityConfigurationSection;

            if (unitySection == null)
            {
                throw new ConfigurationErrorsException("unity section missing!");
            }

            if (string.IsNullOrEmpty(containerName))
            {
                unitySection.Configure(container);
            }
            else
            {
                unitySection.Configure(container, containerName);
            }
        }
Exemple #5
0
        /// <summary>
        /// Creates an instance of <see cref="Microsoft.Practices.Unity.UnityContainer"/> from configuration using the provided container name.
        /// </summary>
        /// <param name="containerName">Container name to configure.  If containerName is null or empty, the default container is configured.</param>
        /// <returns>Created and configured <see cref="Microsoft.Practices.Unity.UnityContainer"/>.</returns>
        private static IUnityContainer CreateUnityContainer(string containerName)
        {
            IUnityContainer unityContainer = new UnityContainer();

            try
            {
                UnityConfigurationSection section = ConfigurationManager.GetSection(
                    UnityInstanceProvider.UnityConfigurationSectionName) as UnityConfigurationSection;
                if (section != null)
                {
                    if (string.IsNullOrEmpty(containerName))
                    {
                        section.Configure(unityContainer);
                    }
                    else
                    {
                        section.Configure(unityContainer, containerName);
                    }
                }
            }
            catch (Exception)
            {
                unityContainer.Dispose();
                throw;
            }

            return(unityContainer);
        }
 public UnityServiceBehaviorAttribute(string containerName)
 {
     containerName = containerName ?? string.Empty;
     if (containers.ContainsKey(containerName))
     {
         this.UnityContainer = containers[containerName];
     }
     else
     {
         lock (typeof(UnityServiceBehaviorAttribute))
         {
             IUnityContainer           container     = new UnityContainer();
             UnityConfigurationSection configuration = (UnityConfigurationSection)ConfigurationManager.GetSection(UnityConfigurationSection.SectionName);
             if (containerName == string.Empty)
             {
                 configuration.Configure(container);
             }
             else
             {
                 configuration.Configure(container, containerName);
             }
             containers[containerName] = container;
             this.UnityContainer       = container;
         }
     }
 }
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="containerName">容器名称</param>
        private RepositoryUnityIoc(string containerName)
        {
            UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");

            if (containerName == "DbRepositoryIoc")
            {
                _container = new UnityContainer();
                section.Configure(_container, containerName);
            }
            else if (section.Containers.Count > 1)
            {
                _container = new UnityContainer();
                section.Configure(_container, containerName);
            }
        }
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="containerName">容器名称</param>
        private UnityIocHelper(string containerName)
        {
            UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");

            if (containerName == "IOCcontainer")
            {
                _container = new UnityContainer();
                section.Configure(_container, containerName);
            }
            else if (section.Containers.Count > 1)
            {
                _container = new UnityContainer();
                section.Configure(_container, containerName);
            }
        }
Exemple #9
0
        public DataProviderFactory()
        {
            container = new UnityContainer();
            UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection(Constants.Unity);

            section.Configure(container, Constants.DataAccessProviderContainer);
        }
Exemple #10
0
        public UnityContext()
        {
            this.IOCContainer = new UnityContainer();
            UnityConfigurationSection config = (UnityConfigurationSection)ConfigurationManager.GetSection(UnityConfigurationSection.SectionName);

            config.Configure(this.IOCContainer, "Default");
        }
        private ServiceLocator()
        {
            _container = new UnityContainer();
            UnityConfigurationSection configurationSection = UnityConfiguration.GetConfigurationFromFile();

            configurationSection.Configure(_container);
        }
        public UnityControllerFactory()
        {
            string containerName = "xzmcwjzsContainer";

            if (UnityContainerDictionary.ContainsKey(containerName))
            {
                this.UnityContainer = UnityContainerDictionary[containerName];
                return;
            }
            else
            {
                lock (syncHelper)
                {
                    if (UnityContainerDictionary.ContainsKey(containerName))
                    {
                        this.UnityContainer = UnityContainerDictionary[containerName];
                        return;
                    }
                    else
                    {
                        //配置UnityContainer
                        IUnityContainer container = new UnityContainer();

                        ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
                        fileMap.ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "ConfigFiles\\UnityConfig.xml");
                        Configuration             configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
                        UnityConfigurationSection configSection = (UnityConfigurationSection)configuration.GetSection(UnityConfigurationSection.SectionName);
                        configSection.Configure(container, "xzmcwjzsContainer");

                        UnityContainerDictionary.Add(containerName, container);
                        this.UnityContainer = UnityContainerDictionary[containerName];
                    }
                }
            }
        }
Exemple #13
0
        public static void Show()
        {
            User user = new User()
            {
                Name     = "caixukun",
                Password = "******"
            };
            {
                //通过Unity实例化对象
                //IUnityContainer container = new UnityContainer();
                //container.RegisterType<IUserProcessor, UserProcessor>();
                //IUserProcessor processor = container.Resolve<IUserProcessor>();
                //processor.RegUser(user);
            }
            {
                //配置UnityContainer实现AOP动态代理
                IUnityContainer container = new UnityContainer();
                //ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
                //fileMap.ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "CfgFiles\\Unity.Config");
                //Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

                //UnityConfigurationSection configSection = (UnityConfigurationSection)configuration.GetSection(UnityConfigurationSection.SectionName);
                UnityConfigurationSection configSection = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
                configSection.Configure(container, "aopContainer");

                IUserProcessor processor = container.Resolve <IUserProcessor>();
                //processor.RegUser(user);

                Console.WriteLine(processor.GetUser(user));
                Console.WriteLine(processor.GetUser(user));
            }
        }
        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 #15
0
        public static void Run()
        {
            //AOP 面向切面编程,在执行具体业务的前后做其他的操作
            Console.WriteLine("*****************UnityContainer*********************");
            {
                ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
                fileMap.ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "CfgFiles\\Unity.Config");                //找配置文件的路径
                Configuration             configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
                UnityConfigurationSection section       = (UnityConfigurationSection)configuration.GetSection(UnityConfigurationSection.SectionName);

                IUnityContainer container = new UnityContainer();
                section.Configure(container, "testContainer");

                //给Iphone注册aop
                container.AddNewExtension <Interception>().Configure <Interception>()
                .SetInterceptorFor <IPhone>(new InterfaceInterceptor());

                IPhone phone = container.Resolve <IPhone>();
                phone.Call();

                IPhone android = container.Resolve <IPhone>("Android");
                android.Call();

                var _ = 0;
            }
        }
Exemple #16
0
        public UnityDependencyResolver()
            : this(new UnityContainer())
        {
            UnityConfigurationSection configuration = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");

            configuration.Configure(_container, _ContainerName);
        }
        public static void Show()
        {
            User user = new User()
            {
                Name     = "Ivan",
                Password = "******"
            };

            {
                IUnityContainer container = new UnityContainer();
                container.RegisterType <IUserProcessor>();
                IUserProcessor processor = container.Resolve <IUserProcessor>();
                processor.RegUser(user);
            }

            {
                IUnityContainer container = new UnityContainer();

                ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();

                fileMap.ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "Unity.config");

                Configuration configuration =
                    ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
                UnityConfigurationSection configurationSection =
                    (UnityConfigurationSection)configuration.GetSection(UnityConfigurationSection.SectionName);

                configurationSection.Configure(container, "UnityAOPContainer");

                IUserProcessor processor = container.Resolve <IUserProcessor>();
                processor.RegUser(user);
                processor.GetUser(user);
            }
        }
Exemple #18
0
        public static void Show()
        {
            User user = new User()
            {
                Name     = "Eleven",
                Password = "******"
            };
            {
                IUnityContainer container = new UnityContainer();
                container.RegisterType <IUserProcessor, UserProcessor>();
                IUserProcessor processor = container.Resolve <IUserProcessor>();
                processor.RegUser(user);
            }
            {
                //配置UnityContainer
                IUnityContainer         container = new UnityContainer();
                ExeConfigurationFileMap fileMap   = new ExeConfigurationFileMap();
                fileMap.ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "CfgFiles\\Unity.Config");
                Configuration             configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
                UnityConfigurationSection configSection = (UnityConfigurationSection)configuration.GetSection(UnityConfigurationSection.SectionName);
                configSection.Configure(container, "aopContainer");

                IUserProcessor processor = container.Resolve <IUserProcessor>();
                processor.RegUser(user);
                User userNew1 = processor.GetUser(user);
                User userNew2 = processor.GetUser(user);
            }
        }
Exemple #19
0
        public IUnityContainer GetUnityContainer()
        {
            IUnityContainer container = GetCache(_containerName);

            if (container != null)
            {
                return(container);
            }
            ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap
            {
                ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "App_Data\\Unity.Config.xml")
            };
            //找配置文件的路径
            Configuration             configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
            UnityConfigurationSection section       = (UnityConfigurationSection)configuration.GetSection(UnityConfigurationSection.SectionName);

            if (section.Containers.ToList().Find(n => n.Name == _containerName) == null)//查找当前容器名称是否存在配置文件中
            {
                return(null);
            }
            container = new UnityContainer();
            section.Configure(container, _containerName);
            SaveCache(_containerName, container);
            return(container);
        }
Exemple #20
0
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="containerName">容器名称</param>
        private UnityIocHelper(string containerName)
        {
            UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");

            _container = new UnityContainer();
            section.Configure(_container, containerName);
        }
Exemple #21
0
        static void Main(string[] args)
        {
            //一般員工(以Container方式實現DI)
            using (IUnityContainer container = new UnityContainer())
            {
                UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
                //只有container不同
                section.Configure(container, "containerEmployee");
                //其他code完全相同
                SalaryCalculator SC     = container.Resolve <SalaryCalculator>();
                float            amount = SC.Calculate(8 * 19, 200, 8);
                Console.Write("\nSalaryFormula--->amount:" + amount);
                Console.ReadKey();
            }

            //老闆
            using (IUnityContainer container = new UnityContainer())
            {
                UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
                //只有container不同
                section.Configure(container, "containerBoss");
                //其他code完全相同
                SalaryCalculator SC     = container.Resolve <SalaryCalculator>();
                float            amount = SC.Calculate(8 * 19, 200, 8);
                Console.Write("\nSalaryFormula--->amount:" + amount);
                Console.ReadKey();
            }
        }
Exemple #22
0
        public ContainerFactory()
        {
            container = new UnityContainer();
            UnityConfigurationSection section = (UnityConfigurationSection)Configuration.UnitySection;

            section.Configure(container);
        }
Exemple #23
0
        static void Main(string[] args)
        {
            try
            {
                IUnityContainer         container = new UnityContainer();
                ExeConfigurationFileMap fileMap   = new ExeConfigurationFileMap();
                fileMap.ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "Config\\Unity.Config");
                Configuration             configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
                UnityConfigurationSection configSection = (UnityConfigurationSection)configuration.GetSection(UnityConfigurationSection.SectionName);
                configSection.Configure(container, "aopContainer");


                User user = new User()
                {
                    Name    = "Michael",
                    MotPass = "******"
                };
                IUserProcessor processor = container.Resolve <IUserProcessor>();
                processor.RegUser(user);
                Console.WriteLine("-------------------------------------------");
                User user1 = processor.GetUser(user);
                Console.WriteLine("-------------------------------------------");
                User user2 = processor.GetUser(user);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                Console.ReadKey();
            }
        }
        public BaseController()
        {
            UnityConfigurationSection config = (UnityConfigurationSection)ConfigurationManager.GetSection(UnityConfigurationSection.SectionName);

            //加载到容器
            config.Configure(container, "MyContainer");
        }
Exemple #25
0
        /// <summary>
        /// 检查Unity配置文件是否正确
        /// </summary>
        /// <param name="path">默认配置文件的路径“/config/unity.config”</param>
        public static void Check(string path = @"/config/unity.config")
        {
            UnityContainer            ucontainer = new UnityContainer();
            UnityConfigurationSection section    = ConfigurationManager.GetSection("unity") as UnityConfigurationSection;
            StringBuilder             err        = new StringBuilder();

            foreach (var container in section.Containers)
            {
                foreach (var regsiter in container.Registrations)
                {
                    string[] mapto = regsiter.MapToName.Split(',');
                    try
                    {
                        Activator.CreateInstance(mapto[1].Trim(), mapto[0].Trim());
                    }
                    catch (Exception ex)
                    {
                        err.Append(ex.Message + " : " + regsiter.MapToName + "\r\n");
                    }
                }
            }
            foreach (var item in section.Containers)
            {
                try
                {
                    section.Configure(ucontainer, item.Name);
                }
                catch (Exception ex)
                {
                    throw new Exception("Unity配置文件出错,请检查\r\n" + ex.Message + "\r\n" + err.ToString());
                }
            }
        }
        static UnityContainerHelper()
        {
            UnityConfigurationSection configuration =
                ConfigurationManager.GetSection(UnityConfigurationSection.SectionName) as UnityConfigurationSection;

            configuration?.Configure(Container, "Material");
        }
        /// <summary>
        /// Initializes a new instance of <c>ServiceLocator</c> class.
        /// </summary>
        private ServiceLocator()
        {
            UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");

            container = new UnityContainer();
            section.Configure(container);
        }
Exemple #28
0
        public void createCustomer(Customer customer)
        {
            try
            {
                log4net.Config.XmlConfigurator.Configure();
                CustomerModel cmodel = new CustomerModel();
                cmodel.firstname  = customer.firstname;
                cmodel.lastname   = customer.lastname;
                cmodel.email      = customer.email;
                cmodel.password   = customer.password;
                cmodel.username   = customer.username;
                cmodel.engineerid = customer.engineerid;

                IUnityContainer           container = new UnityContainer();
                UnityConfigurationSection section   = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
                section.Configure(container);
                IEngineerBL rservicebl = container.Resolve <IEngineerBL>();
                rservicebl.createCustomer(cmodel);
            }
            catch (Exception exp)
            {
                log.Error("Customer via Soap konnte nicht gespeichert werden.");
                throw new Exception("Customer via Soap konnte nicht gespeichert werden.", exp);
            }
        }
        private static void RegisterContainer()
        {
            unityContainer = new UnityContainer();
            UnityConfigurationSection config = (UnityConfigurationSection)ConfigurationManager.GetSection(UnityConfigurationSection.SectionName);

            config.Configure(unityContainer, "Programmer");
        }
        public static void RegisterTypesFromConfig()
        {
            UnityConfigurationSection configuration =
                (UnityConfigurationSection)ConfigurationManager.GetSection(UnityConfigurationSection.SectionName);

            configuration.Configure(container, "DBTypes");
        }