public void ServiceIsRegisteredAsSingleton_TwoResolvedInstancesAreSame()
        {
            var adapter = new AutofacAdapter();

            adapter.RegisterSingleton <ITestDependency, TestDependencyA>();
            adapter.BuildAsLifetimeScope();

            var firstDependency  = adapter.Resolve <ITestDependency>();
            var secondDependency = adapter.Resolve <ITestDependency>();

            firstDependency.Should().BeSameAs(secondDependency);
        }
        public void ServiceIsRegisteredAsTransient_TwoResolvedInstancesAreDifferent()
        {
            var adapter = new AutofacAdapter();

            adapter.RegisterTransient <ITestDependency, TestDependencyA>();
            adapter.BuildAsLifetimeScope();

            var firstDependency  = adapter.Resolve <ITestDependency>();
            var secondDependency = adapter.Resolve <ITestDependency>();

            firstDependency.Should().NotBeSameAs(secondDependency);
        }
        public void Build()
        {
            var cb = new ContainerBuilder();

            // registers all our classes.
            cb.RegisterCqsHandlers(Assembly.GetExecutingAssembly());

            cb.RegisterType<IocCommandBus>().AsImplementedInterfaces().SingleInstance();
            cb.RegisterType<IocEventBus>().AsImplementedInterfaces().SingleInstance();
            cb.RegisterType<IocQueryBus>().AsImplementedInterfaces().SingleInstance();
            cb.RegisterType<IocRequestReplyBus>().AsImplementedInterfaces().SingleInstance();
            cb.Register(x => _adapter).AsImplementedInterfaces().SingleInstance();

            _adapter = new AutofacAdapter(cb.Build());

            CqsBus.CmdBus = _adapter.Resolve<ICommandBus>();
            CqsBus.QueryBus = _adapter.Resolve<IQueryBus>();
            CqsBus.RequestReplyBus = _adapter.Resolve<IRequestReplyBus>();
            CqsBus.EventBus = _adapter.Resolve<IEventBus>();
        }
Ejemplo n.º 4
0
        public void Build()
        {
            var cb = new ContainerBuilder();

            // registers all our classes.
            cb.RegisterCqsHandlers(Assembly.GetExecutingAssembly());

            cb.RegisterType <IocCommandBus>().AsImplementedInterfaces().SingleInstance();
            cb.RegisterType <IocEventBus>().AsImplementedInterfaces().SingleInstance();
            cb.RegisterType <IocQueryBus>().AsImplementedInterfaces().SingleInstance();
            cb.RegisterType <IocRequestReplyBus>().AsImplementedInterfaces().SingleInstance();
            cb.Register(x => _adapter).AsImplementedInterfaces().SingleInstance();

            _adapter = new AutofacAdapter(cb.Build());

            CqsBus.CmdBus          = _adapter.Resolve <ICommandBus>();
            CqsBus.QueryBus        = _adapter.Resolve <IQueryBus>();
            CqsBus.RequestReplyBus = _adapter.Resolve <IRequestReplyBus>();
            CqsBus.EventBus        = _adapter.Resolve <IEventBus>();
        }
        public void MultipleImplementationAreRegisteredByTypeAsParameter_ResolvedCollectionContainsAllImplementations()
        {
            var adapter = new AutofacAdapter();

            adapter.RegisterCollection(typeof(ITestDependency), new[] { typeof(TestDependencyA), typeof(TestDependencyB) });
            adapter.BuildAsLifetimeScope();

            var collection = adapter.Resolve <IEnumerable <ITestDependency> >().ToArray();

            collection.Should().Contain(r => r is TestDependencyA);
            collection.Should().Contain(r => r is TestDependencyB);
        }
Ejemplo n.º 6
0
        static void Main(string[] args)
        {
            IContainer ioc = null;
            while (ioc == null)
            {
                Console.WriteLine("Выберите IoC контейнер: 1-MyIoC, 2-Castle Windsor, 3-Unity, 4-Autofac");
                int iocNumber;
                int.TryParse(Console.ReadLine(), out iocNumber);
                switch (iocNumber)
                {
                    case 1:
                        {
                            ioc = new MyIOCContainer();
                            break;
                        }
                    case 2:
                        {
                            ioc = new WindsorAdapter();
                            break;
                        }
                    case 3:
                        {
                            ioc = new UnityAdapter();
                            break;
                        }
                    case 4:
                        {
                            ioc = new AutofacAdapter();
                            break;
                        }
                }
                if (ioc == null)
                {
                    Console.WriteLine("Неверный ввод!!!");
                }
            }
            var logger = new NLogAdapter();
            logger.Trace(String.Format("Выбран IoC : {0}", ioc.GetType()));
            ioc.Register<IList<Person>>(new List<Person>());
            ioc.Register<IDataAccessor<Person>, ListDataAccessor<Person>>();
            var dataAccessor = ioc.Resolve<IDataAccessor<Person>>();
            dataAccessor.Insert(new Person(1, "Петров", "Петр", "Петрович", 20));
            dataAccessor.Insert(new Person(2, "Иванов", "Иван", "Иванович", 25));
            foreach (var person in dataAccessor.GetAll())
            {
                logger.Info(String.Format("В коллекцию добавлен {0}", person.ToString()));
                Console.WriteLine(person.ToString());
            }

            Console.ReadKey();
        }
        public void MultipleImplementationAreRegisteredByInstance_ResolvedCollectionContainsAllImplementations()
        {
            var adapter   = new AutofacAdapter();
            var instanceA = new TestDependencyA();
            var instanceB = new TestDependencyB();

            adapter.RegisterCollection(new ITestDependency[] { instanceA, instanceB });
            adapter.BuildAsLifetimeScope();

            var collection = adapter.Resolve <IEnumerable <ITestDependency> >().ToArray();

            collection.Should().Contain(instanceA);
            collection.Should().Contain(instanceB);
        }