Exemplo n.º 1
0
 protected virtual void RegisterObjectContainer()
 {
     IObjectContainerFactory factory = new ObjectContainerFactory();
     var container = factory.CreateContainer(Server.MapPath("~/unity.config"));
     GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
     //GlobalConfiguration.Configuration.ServiceResolver.SetResolver(new UnityDependencyResolver(container));
 }
Exemplo n.º 2
0
        protected virtual void RegisterObjectContainer()
        {
            IObjectContainerFactory factory = new ObjectContainerFactory();
            var container = factory.CreateContainer(Server.MapPath("~/unity.config"));

            GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
            //GlobalConfiguration.Configuration.ServiceResolver.SetResolver(new UnityDependencyResolver(container));
        }
Exemplo n.º 3
0
        public virtual void EnvTest_IOC_TypeWithoutRegister()
        {
            var container = ObjectContainerFactory.CreateContainer();
            var dog       = container.Resolve <Dog>();

            Assert.AreEqual(dog.GetType(), typeof(Dog));
            var bigDog = container.Resolve <BigDog>();

            Assert.AreEqual(bigDog.GetType(), typeof(BigDog));
        }
Exemplo n.º 4
0
        public virtual void EnvTest_IOC_TypeBySelf()
        {
            var container = ObjectContainerFactory.CreateContainer();

            container.RegisterType <Dog, Dog>();

            var instance = container.Resolve <Dog>();

            Assert.AreEqual(instance.GetType(), typeof(Dog));
        }
Exemplo n.º 5
0
        /// <summary>
        /// 后注册的项,可以覆盖前面注册的项。
        /// </summary>
        public virtual void EnvTest_IOC_Register_ReplaceByAfter()
        {
            var container = ObjectContainerFactory.CreateContainer();

            container.RegisterType <Dog, Dog>();
            container.RegisterType <Dog, BigDog>();
            container.RegisterType <Dog, SmallDog>();

            var instance = container.Resolve <Dog>();

            Assert.AreEqual(instance.GetType(), typeof(SmallDog));
        }
Exemplo n.º 6
0
        public virtual void EnvTest_IOC_Interface_RegisterByType()
        {
            var container = ObjectContainerFactory.CreateContainer();

            container.RegisterInstance(typeof(IPlant), typeof(Tree));

            var instance = container.Resolve <IPlant>();

            Assert.IsTrue(instance is Tree);
            var instance2 = container.Resolve <IPlant>();

            Assert.IsTrue(instance == instance2);
        }
Exemplo n.º 7
0
        public virtual void EnvTest_IOC_CtorNeedInterface()
        {
            var container = ObjectContainerFactory.CreateContainer();
            var smallDog  = container.Resolve <SmallDog>();

            Assert.IsNotNull(smallDog.Parent);

            var bigDog = new BigDog();

            container.RegisterInstance(bigDog);
            var smallDog2 = container.Resolve <SmallDog>();

            Assert.AreEqual(bigDog, smallDog2.Parent);
        }
Exemplo n.º 8
0
        /// <summary>
        /// 没有注册的接口是无法获取实例的。
        /// </summary>
        public virtual void EnvTest_IOC_Interface_ErrorWithoutRegister()
        {
            var  container = ObjectContainerFactory.CreateContainer();
            bool success   = true;

            try
            {
                container.Resolve <IAnimal>();
            }
            catch
            {
                success = false;
            }
            Assert.IsFalse(success);
        }
Exemplo n.º 9
0
        /// <summary>
        /// 实例依赖的接口还没有注册的话,是无法创建实例的。
        /// </summary>
        public virtual void EnvTest_IOC_Interface_RegisterByType_Dependency_Error()
        {
            var container = ObjectContainerFactory.CreateContainer();

            container.RegisterInstance(typeof(IAnimal), typeof(Rabbit));
            bool success = false;

            try
            {
                var rabbit = container.Resolve <IAnimal>();
                success = true;
            }
            catch { }
            Assert.IsFalse(success);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Rabbit 依赖 IPlant,但是一样可以获取到它之后注册的 Tree 实例。
        /// </summary>
        public virtual void EnvTest_IOC_Interface_RegisterByType_Dependency()
        {
            var container = ObjectContainerFactory.CreateContainer();

            container.RegisterInstance(typeof(IAnimal), typeof(Rabbit));
            container.RegisterInstance(typeof(IPlant), typeof(Tree));

            var instance = container.Resolve <IAnimal>();

            Assert.IsTrue(instance is Rabbit);

            var rabbit = instance as Rabbit;

            Assert.IsTrue(rabbit.Food is Tree);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Create a new type adapter from currect factory
        /// </summary>
        /// <returns>Created type adapter</returns>
        public static ITypeAdapter CreateAdapter()
        {
            if (_currentTypeAdapterFactory == null)
            {
                lock (lock_flag)
                {
                    if (_currentTypeAdapterFactory == null)
                    {
                        _currentTypeAdapterFactory = ObjectContainerFactory.CreateContainer().Resolve <ITypeAdapterFactory>();
                    }
                }
            }

            return(_currentTypeAdapterFactory.Create());
        }
Exemplo n.º 12
0
        public virtual void EnvTest_IOC_Interface()
        {
            var container = ObjectContainerFactory.CreateContainer();

            container.RegisterType <IAnimal, Dog>();

            var instance = container.Resolve <IAnimal>();

            Assert.IsTrue(instance is Dog);

            var cat = new Cat();

            container.RegisterInstance <IAnimal>(cat);
            instance = container.Resolve <IAnimal>();
            Assert.IsTrue(instance == cat);
        }
Exemplo n.º 13
0
        public virtual void EnvTest_IOC_TypeByType()
        {
            var container = ObjectContainerFactory.CreateContainer();

            container.RegisterType <Dog, BigDog>();

            var instance = container.Resolve <Dog>();

            Assert.IsTrue(instance is BigDog);

            var smallDog = new SmallDog(instance as BigDog);

            container.RegisterInstance <Dog>(smallDog);
            instance = container.Resolve <Dog>();
            Assert.IsTrue(instance == smallDog);
        }
Exemplo n.º 14
0
 public static ICacheProvider GetCache(string key)
 {
     if (string.IsNullOrWhiteSpace(key))
     {
         return(instance);
     }
     if (!instances.ContainsKey(key))
     {
         lock (_cacheLock)
         {
             if (!instances.ContainsKey(key))
             {
                 instances.Add(key, ObjectContainerFactory.CreateContainer().Resolve <ICacheProvider>(key));
             }
         }
     }
     return(instances[key]);
 }
Exemplo n.º 15
0
        public virtual void EnvTest_IOC_ResolveAll()
        {
            var container = ObjectContainerFactory.CreateContainer();

            container.RegisterType <IAnimal, Dog>();
            container.RegisterType <IAnimal, BigDog>("BigDog");
            container.RegisterType <IAnimal, SmallDog>("SmallDog");
            container.RegisterInstance <IAnimal>(new Cat {
                Name = "null"
            });
            container.RegisterInstance <IAnimal>(new Cat {
                Name = "another"
            }, "another");

            var instances = container.ResolveAll <IAnimal>();

            Assert.AreEqual(instances.Count(), 3);
            Assert.IsTrue(instances.Any(i => i is BigDog));
            Assert.IsTrue(instances.Any(i => i is SmallDog));
            Assert.IsTrue(instances.Any(i => i is Cat));
            Assert.IsTrue(instances.Any(i =>
            {
                var smallDog = i as SmallDog;
                if (smallDog != null)
                {
                    return(smallDog.Parent != null);
                }
                return(true);
            }));
            Assert.IsTrue(instances.Any(i =>
            {
                var cat = i as Cat;
                if (cat != null)
                {
                    return(cat.Name == "another");
                }
                return(true);
            }));
        }