public void AddAssembly_NullType_ExpectedArgumentNullException()
 {
     Assert.Throws <ArgumentNullException>(() =>
     {
         _container.AddAssembly(null);
     });
 }
示例#2
0
 public void AddAssembly_AssemblyWithDependenciesTwice_ThrowsError()
 {
     Assert.That(() =>
     {
         container.AddAssembly(Assembly.GetExecutingAssembly());
         container.AddAssembly(Assembly.GetExecutingAssembly());
         container.Get <ICustomerDAL>();
     }, Throws.Exception);
 }
示例#3
0
        public void GenericConstructorInjectionTest()
        {
            _container.AddAssembly(Assembly.GetExecutingAssembly());

            var customerBll = _container.CreateInstance <CustomerBLL>();

            Assert.IsNotNull(customerBll);
            Assert.IsTrue(customerBll.GetType() == typeof(CustomerBLL));
        }
示例#4
0
        public void AddAssembly_GetICustomerDAL_ReturnICustomerDAL()
        {
            container.AddAssembly(Assembly.GetExecutingAssembly());

            var customerBLL = container.Get<ICustomerDAL>();

            Assert.That(customerBLL, Is.Not.Null);
            Assert.That(customerBLL, Is.InstanceOf<ICustomerDAL>());
        }
示例#5
0
        public void AddAssembly_ImportConstructor_ReturnCustomerBLL()
        {
            container.AddAssembly(Assembly.GetExecutingAssembly());

            var ñustomerBLL = container.Get <CustomerBLL>();

            Assert.That(ñustomerBLL, Is.Not.Null);
            Assert.That(ñustomerBLL, Is.InstanceOf <CustomerBLL>());
        }
示例#6
0
        public void CreateInstance_AssemblyAttributes_ConstructorInjectionTest()
        {
            container.AddAssembly(Assembly.GetExecutingAssembly());

            var customerBll = container.CreateInstance(typeof(CustomerBLL));
            var CustomerDal = (CustomerDAL)container.CreateInstance(typeof(ICustomerDAL));

            Assert.IsTrue(customerBll.GetType() == typeof(CustomerBLL));
            Assert.IsTrue(CustomerDal.GetType() == typeof(CustomerDAL));
        }
示例#7
0
        public void CreateInstance_UsingAssemblyAttributes_ConstructorDependencies()
        {
            container.AddAssembly(Assembly.GetExecutingAssembly());

            var customerConstr        = container.CreateInstance(typeof(CustomerBLL));
            var customerConstrGeneric = container.CreateInstance <CustomerBLL>();

            Assert.IsNotNull(customerConstr, "Customer instance was not created.");
            Assert.IsNotNull(customerConstrGeneric, "Customer instance was not created using generic method.");
            Assert.IsTrue(customerConstr.GetType() == typeof(CustomerBLL), "Wrong type returned by CreateInstance method.");
            Assert.IsTrue(customerConstrGeneric.GetType() == typeof(CustomerBLL), "Wrong type returned by CreateInstance generic method.");
        }
示例#8
0
        public void SetUp()
        {
            Assembly assembly = Assembly.Load("VideoLib");;

            _container = new Container();
            _container.AddAssembly(assembly);
        }
示例#9
0
        public void TestClassWithProperty()
        {
            Container container   = new Container();
            int       someMockAge = 23;

            container.AddAssembly(Assembly.GetExecutingAssembly());

            container.AddType(typeof(SomeIBazDependantClass), InstanceMode.Singleton);
            container.RegisterComponents(
                new Component().For(typeof(SomeFooDependantClass))
                .DependsOn(new {
                age = someMockAge
            })
                .WithInstanceMode(InstanceMode.Transient)
                );

            Assert.DoesNotThrow(() => {
                container.WireUp();
            });

            SomeIBazDependantClass x1 = container.CreateInstance <SomeIBazDependantClass>();
            SomeIBazDependantClass x2 = container.CreateInstance <SomeIBazDependantClass>();
            bool ibazDependantAreSame = Object.ReferenceEquals(x1, x2);

            Assert.IsTrue(ibazDependantAreSame);

            SomeFooDependantClass y1 = container.CreateInstance <SomeFooDependantClass>();
            SomeFooDependantClass y2 = container.CreateInstance <SomeFooDependantClass>();
            bool fooDependantAreSame = Object.ReferenceEquals(y1, y2);

            Assert.IsFalse(fooDependantAreSame);
        }
示例#10
0
        public void ExportAttributeTestCyclicDependency()
        {
            var container = new Container();

            container.AddAssembly(Assembly.GetExecutingAssembly());
            container.CreateInstance <CyclicDependency>();
        }
示例#11
0
        public void ImportAttributeTestNotRegisteredExeption()
        {
            var container = new Container();

            container.AddAssembly(Assembly.GetExecutingAssembly());
            container.CreateInstance <DummyPropertyInjectionClass>();
        }
示例#12
0
        public void ImportConstructorAttributeTestNotAllParametersRegistered()
        {
            var container = new Container();

            container.AddAssembly(Assembly.GetExecutingAssembly());
            container.CreateInstance <WrongConstructorInjection>();
        }
示例#13
0
        public void AddAssemblyTest()
        {
            var container = new Container();

            container.AddAssembly(Assembly.GetExecutingAssembly());
            Assert.IsNotNull(container.CreateInstance <Shop>());
        }
        public void AddAssemblyTest()
        {
            Container container = new Container();

            container.AddAssembly(Assembly.LoadFrom("MyIoc.dll"));
            var service              = (Service)container.CreateInstance(typeof(Service));
            var repository           = (SomeRepository)container.CreateInstance(typeof(SomeRepository));
            var serviceGenericCreate = container.CreateInstance <Service>();

            Assert.Multiple(() =>
            {
                Assert.IsNotNull(service);
                Assert.IsNotNull(service.repository);
                Assert.IsNotNull(((SomeRepository)service.repository).Connection);
                Assert.IsNotNull(repository);
                Assert.IsNotNull(repository.Connection);
                Assert.IsNotNull(serviceGenericCreate);
                Assert.IsNotNull(serviceGenericCreate.repository);
                Assert.IsNotNull(((SomeRepository)serviceGenericCreate.repository).Connection);

                Assert.IsInstanceOf <Service>(service);
                Assert.IsInstanceOf <SomeRepository>(service.repository);
                Assert.IsInstanceOf <Connection>(((SomeRepository)service.repository).Connection);
                Assert.IsInstanceOf <SomeRepository>(repository);
                Assert.IsInstanceOf <Connection>(repository.Connection);
                Assert.IsInstanceOf <Service>(serviceGenericCreate);
                Assert.IsInstanceOf <SomeRepository>(serviceGenericCreate.repository);
                Assert.IsInstanceOf <Connection>(((SomeRepository)serviceGenericCreate.repository).Connection);
            });
        }
示例#15
0
        public void CreateInstance_ExecutingAssembly_CustomerBLL()
        {
            container.AddAssembly(Assembly.GetExecutingAssembly());

            var actial = (CustomerBLL)container.CreateInstance(typeof(CustomerBLL));

            Assert.IsTrue(actial.GetType() == typeof(CustomerBLL));
        }
示例#16
0
        public void ImportAttributeTest()
        {
            var container = new Container();

            container.AddAssembly(Assembly.GetExecutingAssembly());
            var propertyInjection = container.CreateInstance <PropertyInjectionClass>();

            Assert.IsNotNull(propertyInjection);
            Assert.IsNotNull(propertyInjection.Person);
        }
示例#17
0
        public void GetAssemblyAndCheckInstance()
        {
            var container = new Container();

            container.AddAssembly(Assembly.Load("MyIoC"));

            var customerBll = container.CreateInstance <CustomerBLL>();

            Assert.AreNotEqual(null, customerBll);
        }
        public void AddAssembly_PassAssembly_AddTypesToTypeResolversDictionary()
        {
            var container = new Container();
            var initialTypeResolversCount = container.TypeResolvers.Count;

            container.AddAssembly(Assembly.Load("TestAssembly"));

            Assert.AreEqual(0, initialTypeResolversCount);
            Assert.AreEqual(4, container.TypeResolvers.Count);
        }
示例#19
0
        public void ExportAttributeTest()
        {
            var container = new Container();

            container.AddAssembly(Assembly.GetExecutingAssembly());
            var customer = container.CreateInstance <ICustomer>();

            Assert.IsNotNull(customer);
            Assert.IsNotNull(customer.Person);
            Assert.IsNotNull(customer.Product);
            Assert.IsNotNull(customer.Shop);
        }
示例#20
0
        public void ImportConstructorAttributeTest()
        {
            var container = new Container();

            container.AddAssembly(Assembly.GetExecutingAssembly());
            var tricky = container.CreateInstance <TrickyClass>();

            Assert.IsNotNull(tricky);
            Assert.IsNotNull(tricky.Shop);
            Assert.IsNull(tricky.Person);
            Assert.IsNull(tricky.Customer);
        }
示例#21
0
文件: Program.cs 项目: isaikin/Ioc
        public static void Main(string[] args)
        {
            var contaner = new Container();

            //contaner.AddType<INewSpaperBLL, NewSpaperBLLProperty>();
            //contaner.AddType<INewSpaperDao, NewSpaperDao>();
            //contaner.AddType<INewSpaperIssueDao, NewSpaperIssueDao>();
            //contaner.AddType(typeof(Logger));
            //contaner.AddType(typeof(NewSpaperBLLAttribyte));


            var ass = Assembly.GetExecutingAssembly();

            contaner.AddAssembly(ass);
            var temp = contaner.CreateInstance <INewSpaperBLL>();

            Console.WriteLine(temp.Show());
        }
示例#22
0
        static void Main(string[] args)
        {
            var container = new Container();

            //container.AddType(typeof(CustomerDAL), typeof(ICustomerDAL));
            //var customerDAL = container.CreateInstance<ICustomerDAL>();
            //container.AddType(typeof(CustomerBLL));

            //container.AddType(typeof(Logger));
            //var ins = container.CreateInstance<CustomerBLL>();

            container = new Container();
            Type t = typeof(CustomerBLL2);

            container.AddAssembly(t.Assembly);
            var customerBLL  = container.CreateInstance <CustomerBLL>();
            var customerBLL2 = container.CreateInstance <CustomerBLL2>();

            customerBLL2.Logger.ToString();
            Console.ReadLine();
        }
示例#23
0
        private static void Main(string[] args)
        {
            try
            {
                var container = new Container();
                container.AddAssembly(Assembly.Load("IoC.Sample"));

                var customerBLL  = (CustomerBLL)container.CreateInstance(typeof(CustomerBLL));
                var customerBLL2 = container.CreateInstance <CustomerBLL2>();
                var logger       = container.CreateInstance <Logger>();
            }
            catch (ContainerException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (Exception)
            {
                Console.WriteLine("Unexpected error.");
            }

            Console.ReadLine();
        }
示例#24
0
        public void ResolveWithExportAssembly()
        {
            Container container = new Container();

            container.AddAssembly(Assembly.GetExecutingAssembly());

            container.RegisterComponents(
                new Component().For(typeof(SomeFooDependantClass))
                .DependsOn(new {
                age = 1
            })
                .WithInstanceMode(InstanceMode.Transient)
                );

            Assert.DoesNotThrow(() => {
                container.WireUp();
            });

            SomeFooDependantClass y1 = container.CreateInstance <SomeFooDependantClass>();
            SomeFooDependantClass y2 = container.CreateInstance <SomeFooDependantClass>();

            Assert.IsFalse(ReferenceEquals(y1, y2));
        }
示例#25
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            var container = new Container();

            container.AddAssembly(Assembly.GetExecutingAssembly());
            //container.AddType(typeof(CustomerBLL));
            //container.AddType(typeof(Logger));
            //container.AddType(typeof(CustomerDAL), typeof(ICustomerDAL));

            try
            {
                var customerBLL = container.CreateInstance <CustomerBLL>();
                //var customerBLL = (CustomerBLL)container.CreateInstance(
                //    typeof(CustomerBLL));

                customerBLL.Test();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
示例#26
0
        static void Main(string[] args)
        {
            Assembly assembly = Assembly.Load("VideoLib");

            Console.WriteLine(assembly.FullName);
            Console.WriteLine("------------");
            Console.WriteLine();

            Console.WriteLine("классы помеченные ImportConstructorAttribute");
            var types1 = assembly.GetTypes()
                         .Where(t => t.IsClass && t.GetCustomAttributes <ImportConstructorAttribute>().Any());

            foreach (Type t in types1)
            {
                Console.WriteLine(t.FullName);
            }
            Console.WriteLine();

            Console.WriteLine("классы со свойствами помеченными ImportAttribute");
            var types2 = assembly.GetTypes()
                         .Where(t => t.IsClass && t.GetProperties().Any(p => p.GetCustomAttributes <ImportAttribute>().Any()));

            foreach (Type t in types2)
            {
                Console.WriteLine(t.FullName);
                var propType = t.GetProperties().Where(p => p.GetCustomAttributes <ImportAttribute>().Any());
                foreach (PropertyInfo pt in propType)
                {
                    Console.WriteLine(" - " + pt.Name + " - " + pt.PropertyType);
                }
            }
            Console.WriteLine();

            Console.WriteLine("интерфейсы");
            var types3 = assembly.GetTypes()
                         .Where(t => t.IsInterface);

            foreach (Type t in types3)
            {
                Console.WriteLine(t.FullName);
            }
            Console.WriteLine();

            Console.WriteLine("классы помеченные ExportAttribute");
            var types4 = assembly.GetTypes()
                         .Where(t => t.IsClass && t.GetCustomAttributes <ExportAttribute>().Any() && t.GetInterfaces().Any(i => i == typeof(IVideoProvider)));

            foreach (Type t in types4)
            {
                Console.WriteLine(t.FullName);
            }
            Console.WriteLine();

            Console.WriteLine("атрибут ImportConstructorAttribute класса VideoManager2");
            var Attribs = typeof(VideoManager2).GetCustomAttributes <ImportConstructorAttribute>();

            foreach (Attribute a in Attribs)
            {
                Console.WriteLine(a.GetType());
            }
            Console.WriteLine();

            Console.WriteLine("свойства VideoManager1 помеченные ImportAttribute");
            var prop = typeof(VideoManager1).GetProperties().Where(p => p.GetCustomAttributes <ImportAttribute>().Any());

            foreach (PropertyInfo p in prop)
            {
                Console.WriteLine(p.Name);
            }
            Console.WriteLine();

            Console.WriteLine("Конструктор VideoManager2");
            var constr = typeof(VideoManager2).GetConstructors();

            foreach (ConstructorInfo c in constr)
            {
                Console.WriteLine(c.Name);
                var constrPar = c.GetParameters();
                foreach (ParameterInfo p in constrPar)
                {
                    Console.WriteLine(" - " + p.Name);
                }
            }
            Console.WriteLine();

            Container container = new Container();

            container.AddAssembly(assembly);

            Console.WriteLine("создаем объект VideoManager1 через публичные свойства ");
            VideoManager1 videoManager1 = (VideoManager1)container.CreateInstance(typeof(VideoManager1));

            videoManager1.VideoProvider.PlayVideo();
            Console.WriteLine();

            Console.WriteLine("создаем объект VideoManager2 через конструктор");
            VideoManager2 videoManager2 = (VideoManager2)container.CreateInstance(typeof(VideoManager2));

            videoManager2.VideoProvider.PlayVideo();
            Console.WriteLine();

            Console.ReadKey();
        }
 public ContainerTests()
 {
     _container = new Container();
     _container.AddAssembly(Assembly.GetExecutingAssembly());
 }
 public void MefInit()
 {
     container = new Container();
     container.AddAssembly(Assembly.GetExecutingAssembly());
 }
 public void AddAssembly_PassNull_ThrowArgumentNullException()
 {
     Assert.ThrowsException <ArgumentNullException>(() => _container.AddAssembly(null));
 }
 public ContainerTests()
 {
     _container = new Container();
     _container.AddAssembly(Assembly.Load("TestAssembly"));
 }