Ejemplo n.º 1
0
        public async Task Push_And_Prepare_Model(string stringValue)
        {
            var container = new Container();
            var app       = new Application();

            ZeroApp
            .On(app)
            .WithContainer(DryIocZeroContainer.Build(container))
            .RegisterShell(() => new FirstShell())
            .Start();

            var firstViewModel  = container.Resolve <FirstViewModel>();
            var secondViewModel = container.Resolve <SecondViewModel>();

            var secondViewModelPageBeforePush          = secondViewModel.CurrentPage;
            var secondViewModelPreviousModelBeforePush = secondViewModel.PreviousModel;

            Assert.IsNull(secondViewModelPageBeforePush);
            Assert.IsNull(secondViewModelPreviousModelBeforePush);
            Assert.IsNull(secondViewModel.SecondStringProperty);

            await firstViewModel.Push <SecondPage>(stringValue);

            var secondViewModelPageAfterPush          = secondViewModel.CurrentPage;
            var secondViewModelPreviousModelAfterPush = secondViewModel.PreviousModel;

            Assert.IsNotNull(secondViewModelPageAfterPush);
            Assert.IsNotNull(secondViewModelPreviousModelAfterPush);
            Assert.AreEqual(secondViewModelPageAfterPush.GetType(), typeof(SecondPage));
            Assert.AreEqual(secondViewModelPreviousModelAfterPush.GetType(), typeof(FirstViewModel));
            Assert.AreEqual(firstViewModel, secondViewModel.PreviousModel);
            Assert.IsNotNull(secondViewModel.SecondStringProperty);
            Assert.AreEqual(stringValue, secondViewModel.SecondStringProperty);
        }
Ejemplo n.º 2
0
        public void test_lazy_resolve_and_resolutionscope_reuse()
        {
            var container = new Container();

            container.Register <Component>(Reuse.InResolutionScope);
            container.Register <CreationContext>(Reuse.InResolutionScope);
            container.Register <Factory>(Reuse.Transient);

            var object1  = new object();
            var factory1 = container.Resolve <Factory>();

            factory1.Parameter = object1;

            var object2  = new object();
            var factory2 = container.Resolve <Factory>();

            factory2.Parameter = object2;


            var component1 = factory1.CreateComponent();
            var component2 = factory2.CreateComponent();

            Assert.AreSame(component1.Parameter, object1);
            Assert.AreNotSame(component1.Parameter, component2.Parameter);
        }
Ejemplo n.º 3
0
        public void ShellPagedViewModel_Markup_Returns_ActualViewModel()
        {
            var container = new Container();
            var app       = new Application();

            ZeroApp
            .On(app)
            .WithContainer(DryIocZeroContainer.Build(container))
            .RegisterShell(() => new FirstShell())
            .Start();

            var firstPage = container.Resolve <FirstPage>();

            var markup = new ShellPagedViewModelMarkup()
            {
                ViewModel = typeof(FirstViewModel),
                Page      = firstPage
            };

            var provider = new XamlServiceProvider();

            var vmValue = (FirstViewModel)markup.ProvideValue(provider);

            Assert.AreEqual(typeof(FirstViewModel), vmValue.GetType());
            Assert.AreEqual(vmValue, container.Resolve <FirstViewModel>());
        }
Ejemplo n.º 4
0
        public void SingletonRegister_Success()
        {
            ITestCase testCase = new TestCaseA();

            var c = new Container();
            c = (Container)testCase.SingletonRegister(c);

            var obj1 = c.Resolve<ITestA>();
            var obj2 = c.Resolve<ITestA>();

            Assert.AreEqual(obj1, obj2);
            Helper.Check(obj1, true);
            Helper.Check(obj2, true);
        }
Ejemplo n.º 5
0
        public void TransientRegister_Success()
        {
            ITestCase testCase = new TestCaseA();

            var c = new Container();
            c = (Container)testCase.TransientRegister(c);

            var obj1 = c.Resolve<ITestA>();
            var obj2 = c.Resolve<ITestA>();

            Assert.AreNotEqual(obj1, obj2);
            Helper.Check(obj1, false);
            Helper.Check(obj2, false);
        }
Ejemplo n.º 6
0
        public void DryIoc()
        {
            var container = new DryIoc.Container();

            foreach (var type in _types)
            {
                container.Register(type, type);
            }
            int length = 0;

            if (Scenario == ResolveScenario.ResolveOne)
            {
                length = 1;
            }
            else if (Scenario == ResolveScenario.ResolveHalf)
            {
                length = _types.Length / 2;
            }
            else if (Scenario == ResolveScenario.ResolveAll)
            {
                length = _types.Length;
            }

            for (var i = 0; i < length; i++)
            {
                container.Resolve(_types[i]);
            }

            container.Dispose();
        }
Ejemplo n.º 7
0
        static void Main(string[] args)
        {
            var container = new DryIoc.Container();

            container.Register <IMySuperService, MySuperServiceImpl>(Reuse.Transient);
            container.Register <IMyWorker, MyWorkerImpl>();



            var worker = container.Resolve <IMyWorker>();

            worker.Work();

            var worker2 = container.Resolve <IMyWorker>();

            worker2.Work();

            System.Console.ReadLine();
        }
Ejemplo n.º 8
0
        static void Main(string[] args)
        {
            Container container = new Container();
            container.Register<ICalculo, Soma>();
            container.Register<ICalculo, Subtracao>();
            container.Register<Calculadora>(Reuse.Singleton);

            Calculadora calculadora = container.Resolve<Calculadora>();
            calculadora.EfetuarCalculos(500, 200);

            IEnumerable<ICalculo> calculos = container.Resolve<IEnumerable<ICalculo>>();
            foreach (var calculo in calculos)
            {
                var result = calculo.Calcular(100, 50);
                Console.WriteLine($"Resultado: {result}");
            }

            Console.Read();
        }
Ejemplo n.º 9
0
        public void container_can_resolve_personlistviewmodel()
        {
            var ass = Assembly.GetAssembly(typeof(PersonListViewModel));
            var container = new Container();
            container.Register(typeof (IRepository<Person>), typeof (TestPersonRepository));

            container = container.RegisterViewModels(ass);

            var personListVm = container.Resolve<IListViewModel<Person>>();
            Assert.IsNotNull(personListVm, "PersonListViewModel could not be resolved");
        }
Ejemplo n.º 10
0
        private static IMediator BuildMediator()
        {
            var container = new Container();

            container.RegisterDelegate<SingleInstanceFactory>(r => serviceType => r.Resolve(serviceType));
            container.RegisterDelegate<MultiInstanceFactory>(r => serviceType => r.ResolveMany(serviceType));
            container.RegisterInstance(Console.Out);

            container.RegisterMany(new[] { typeof(IMediator).GetAssembly(), typeof(Ping).GetAssembly() }, type => type.GetTypeInfo().IsInterface);

            return container.Resolve<IMediator>();
        }
Ejemplo n.º 11
0
        public void Test_ViewModels_ShellService_And_MessagingCenter_Are_Registered_On_Startup()
        {
            var container = new Container();
            var app       = new Application();

            ZeroApp
            .On(app)
            .WithContainer(DryIocZeroContainer.Build(container))
            .RegisterShell(() => new FirstShell())
            .Start();

            var firstViewModel  = container.Resolve <FirstViewModel>();
            var secondViewModel = container.Resolve <SecondViewModel>();

            var shellService    = container.Resolve <IShellService>();
            var messagingCenter = container.Resolve <IMessagingCenter>();

            Assert.NotNull(firstViewModel);
            Assert.NotNull(secondViewModel);
            Assert.NotNull(shellService);
            Assert.NotNull(messagingCenter);
        }
Ejemplo n.º 12
0
        public IGame BuildGame()
        {
            var container = new Container();

            new AlphaCivPackage().Load(container);

            new FixedCities().SetUpCities(() => new DryIocCityBuilder(container));
            new ThreeUnitsWithoutActions().SetUpUnits(() => new DryIocUnitBuilder(container));

            var game = container.Resolve<IGame>();
            game.ContainerName = "DryIoc/AlphaCiv";

            return game;
        }
Ejemplo n.º 13
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.AddApplicationInsightsTelemetry(Configuration);
            services.AddMvc();

            // DryIOC
            // http://stackoverflow.com/questions/36179819/dryioc-asp-net-5-web-api
            var container = new DryIoc.Container().WithDependencyInjectionAdapter(services);

            container.Register <ICustomerRepository, CustomerRepository>(Reuse.Singleton);
            var serviceProvider = container.Resolve <IServiceProvider>();

            return(serviceProvider);
        }
Ejemplo n.º 14
0
        public async Task Pop_And_ReversePrepare_Model(string stringValue)
        {
            var container = new Container();
            var app       = new Application();

            ZeroApp
            .On(app)
            .WithContainer(DryIocZeroContainer.Build(container))
            .RegisterShell(() => new FirstShell())
            .Start();

            var firstViewModel  = container.Resolve <FirstViewModel>();
            var secondViewModel = container.Resolve <SecondViewModel>();

            Assert.AreEqual(firstViewModel.CurrentPage.GetType(), typeof(FirstPage));
            Assert.IsNull(secondViewModel.CurrentPage);

            Assert.AreEqual(firstViewModel.CurrentPage.Navigation.NavigationStack.Count, 1);

            await firstViewModel.Push <SecondPage>();

            Assert.AreEqual(firstViewModel.CurrentPage.Navigation.NavigationStack.Count, 2);

            Assert.AreEqual(firstViewModel.CurrentPage.GetType(), typeof(FirstPage));
            Assert.AreEqual(secondViewModel.CurrentPage.GetType(), typeof(SecondPage));

            await secondViewModel.Pop(stringValue);

            Assert.AreEqual(firstViewModel.CurrentPage.Navigation.NavigationStack.Count, 1);

            Assert.AreEqual(firstViewModel.CurrentPage.GetType(), typeof(FirstPage));
            Assert.AreEqual(secondViewModel.CurrentPage.GetType(), typeof(SecondPage));

            Assert.NotNull(firstViewModel.FirstStringProperty);
            Assert.AreEqual(firstViewModel.FirstStringProperty, stringValue);
        }
Ejemplo n.º 15
0
        internal static IBus Build(Config configuration)
        {
            var logger = configuration.Logger;
            logger.Debug("Constructing bus...");

            _container = _container ?? new Container();

            _container.Register(configuration.Logger.GetType(), ifAlreadyRegistered: IfAlreadyRegistered.Keep);
            _container.Register(configuration.Serializer.GetType(), ifAlreadyRegistered: IfAlreadyRegistered.Keep);
            _container.Register(configuration.Compressor.GetType(), ifAlreadyRegistered: IfAlreadyRegistered.Keep);
            _container.Register(configuration.Transport.GetType(), Reuse.Singleton, ifAlreadyRegistered: IfAlreadyRegistered.Keep);

            _container.RegisterMany(configuration.CommandPipeline.GetCommandHandlerTypes(), Reuse.Singleton, ifAlreadyRegistered: IfAlreadyRegistered.Keep);
            _container.RegisterMany(configuration.CommandPipeline.GetCommandTypes(), ifAlreadyRegistered: IfAlreadyRegistered.Keep);
            _container.RegisterMany(configuration.CommandPipeline.GetPreHookTypes(), ifAlreadyRegistered: IfAlreadyRegistered.Keep);
            _container.RegisterMany(configuration.CommandPipeline.GetPostHookTypes(), ifAlreadyRegistered: IfAlreadyRegistered.Keep);
            _container.RegisterMany(configuration.CommandPipeline.GetErrorHookTypes(), ifAlreadyRegistered: IfAlreadyRegistered.Keep);

            _container.RegisterMany(configuration.EventPipeline.GetCompetingEventHandlerTypes(), Reuse.Singleton, ifAlreadyRegistered: IfAlreadyRegistered.Keep);
            _container.RegisterMany(configuration.EventPipeline.GetMulticastEventHandlerTypes(), Reuse.Singleton, ifAlreadyRegistered: IfAlreadyRegistered.Keep);
            _container.RegisterMany(configuration.EventPipeline.GetEventTypes(), ifAlreadyRegistered: IfAlreadyRegistered.Keep);
            _container.RegisterMany(configuration.EventPipeline.GetPreHookTypes(), ifAlreadyRegistered: IfAlreadyRegistered.Keep);
            _container.RegisterMany(configuration.EventPipeline.GetPostHookTypes(), ifAlreadyRegistered: IfAlreadyRegistered.Keep);
            _container.RegisterMany(configuration.EventPipeline.GetErrorHookTypes(), ifAlreadyRegistered: IfAlreadyRegistered.Keep);

            _container.RegisterMany(configuration.RequestResponsePipeline.GetRequestHandlerTypes(), Reuse.Singleton, ifAlreadyRegistered: IfAlreadyRegistered.Keep);
            _container.RegisterMany(configuration.RequestResponsePipeline.GetMulticastRequestHandlerTypes(), Reuse.Singleton, ifAlreadyRegistered: IfAlreadyRegistered.Keep);
            _container.RegisterMany(configuration.RequestResponsePipeline.GetRequestTypes(), ifAlreadyRegistered: IfAlreadyRegistered.Keep);
            _container.RegisterMany(configuration.RequestResponsePipeline.GetResponseTypes(), ifAlreadyRegistered: IfAlreadyRegistered.Keep);
            _container.RegisterMany(configuration.RequestResponsePipeline.GetPreHookTypes(), ifAlreadyRegistered: IfAlreadyRegistered.Keep);
            _container.RegisterMany(configuration.RequestResponsePipeline.GetPostHookTypes(), ifAlreadyRegistered: IfAlreadyRegistered.Keep);
            _container.RegisterMany(configuration.RequestResponsePipeline.GetErrorHookTypes(), ifAlreadyRegistered: IfAlreadyRegistered.Keep);

            _container.Register<IPipeline, CommandPipeline>(Reuse.Singleton, ifAlreadyRegistered: IfAlreadyRegistered.Keep);
            _container.Register<IPipeline, EventPipeline>(Reuse.Singleton, ifAlreadyRegistered: IfAlreadyRegistered.Keep);
            _container.Register<IPipeline, RequestResponsePipeline>(Reuse.Singleton, ifAlreadyRegistered: IfAlreadyRegistered.Keep);

            _container.Register<IBus, Bus>(Reuse.Singleton, ifAlreadyRegistered: IfAlreadyRegistered.Keep);

            return _container.Resolve<IBus>();
        }
Ejemplo n.º 16
0
        static public void Main(string[] args)
        {
            var _benchmark = new Benchmark(() => new Action(() => new Calculator()));

            _benchmark.Add("SimpleInjector", () =>
            {
                var _container = new SimpleInjector.Container();
                _container.Register <ICalculator, Calculator>(SimpleInjector.Lifestyle.Transient);
                return(() => _container.GetInstance <ICalculator>());
            });
            //TODO : change to test new Puresharp DI recast
            _benchmark.Add("Puresharp", () =>
            {
                var _container = new Puresharp.Composition.Container();
                _container.Add <ICalculator>(() => new Calculator(), Puresharp.Composition.Lifetime.Volatile);
                return(() => _container.Enumerable <ICalculator>());
            });
            //TODO : change to test MEF2
            _benchmark.Add("MEF", () =>
            {
                var _container = new System.Composition.Hosting.ContainerConfiguration().WithAssembly(typeof(ICalculator).Assembly).CreateContainer();
                return(() => _container.GetExport <ICalculator>());
            });
            _benchmark.Add("Castle Windsor", () =>
            {
                var _container = new WindsorContainer();
                _container.Register(Castle.MicroKernel.Registration.Component.For <ICalculator>().ImplementedBy <Calculator>());
                return(() => _container.Resolve <ICalculator>());
            });
            _benchmark.Add("Unity", () =>
            {
                var _container = new UnityContainer();
                _container.RegisterType <ICalculator, Calculator>();
                return(() => _container.Resolve <ICalculator>());
            });
            _benchmark.Add("StuctureMap", () =>
            {
                var _container = new StructureMap.Container(_Builder => _Builder.For <ICalculator>().Use <Calculator>());
                return(() => _container.GetInstance <ICalculator>());
            });
            _benchmark.Add("DryIoc", () =>
            {
                var _container = new DryIoc.Container();
                _container.Register <ICalculator, Calculator>();
                return(() => _container.Resolve <ICalculator>());
            });
            _benchmark.Add("Autofac", () =>
            {
                var _builder = new Autofac.ContainerBuilder();
                _builder.RegisterType <Calculator>().As <ICalculator>();
                var _container = _builder.Build(Autofac.Builder.ContainerBuildOptions.None);
                return(() => _container.Resolve <ICalculator>());
            });
            _benchmark.Add("Ninject", () =>
            {
                var _container = new Ninject.StandardKernel();
                _container.Bind <ICalculator>().To <Calculator>();
                return(() => _container.Get <ICalculator>());
            });
            _benchmark.Add("Abioc", () =>
            {
                var _setup = new Abioc.Registration.RegistrationSetup();
                _setup.Register <ICalculator, Calculator>();
                var _container = Abioc.ContainerConstruction.Construct(_setup, typeof(ICalculator).Assembly);
                return(() => _container.GetService <ICalculator>());
            });
            _benchmark.Add("Grace", () =>
            {
                var _container = new Grace.DependencyInjection.DependencyInjectionContainer();
                _container.Configure(c => c.Export <Calculator>().As <ICalculator>());
                return(() => _container.Locate <ICalculator>());
            });
            _benchmark.Run(Console.WriteLine);
        }
Ejemplo n.º 17
0
 //[Benchmark]
 public IUserService DryIoC()
 {
     return(dryIocContainer.Resolve <IUserService>());
 }
 public object DryIoc()
 {
     return(dryIocContainer.Resolve(typeof(ITransient1)));
 }
Ejemplo n.º 19
0
 public T Resolve <T>()
     where T : class
 => _Container.Resolve <T>().NotNull();
Ejemplo n.º 20
0
 internal T Resolve <T>() => _container.Resolve <T>();
Ejemplo n.º 21
0
        static void Test()
        {
            var dry = new DryIoc.Container();

            dry.Register <IMetricSubmitter, EnrichMetricsDecorator>(
                setup: new DecoratorSetup(z =>
            {
                return(z.Parent.ImplementationType == typeof(SingleWorker));
            }, order: 0, useDecorateeReuse: false));
            dry.Register <IMetricSubmitter, DefaultMetricSubmitter>(Reuse.Singleton);
            dry.Register <SingleWorker>(Reuse.Singleton);
            dry.Register <ScopedWorker>(Reuse.Singleton);
            var worker = dry.Resolve <SingleWorker>();

            Console.WriteLine(worker);
            var worker2 = dry.Resolve <ScopedWorker>();

            Console.WriteLine(worker2);


            var container = new global::SimpleInjector.Container();

            container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();

            container.ForConsumer <BConsumer>()
            .RegisterSingleton <AInterface, BImplementation>();

            container.ForConsumer <AConsumer>()
            .Register <AInterface, AImplementation>(Lifestyle.Singleton);

            container.RegisterSingleton <AConsumer>();

            container.RegisterDecorator(
                typeof(AInterface), typeof(AInterfaceDecorator), Lifestyle.Singleton,
                z =>
            {
                return(true);
            });
            container.RegisterDecorator(
                typeof(AInterface), typeof(BInterfaceDecorator), Lifestyle.Singleton,
                z =>
            {
                return(true);
            });


            container.Register <SingleWorker>(Lifestyle.Singleton);
            container.Register <ScopedWorker>(Lifestyle.Scoped);

            container.RegisterDecorator <IMetricSubmitter, EnrichMetricsDecorator>();
            container.RegisterConditional <IMetricSubmitter, ProxyMetricSubmitter>(
                Lifestyle.Singleton,
                z => z.Consumer.ImplementationType == typeof(SingleWorker));



            container.Verify();

            container.GetInstance <SingleWorker>();
            using (AsyncScopedLifestyle.BeginScope(container))
            {
                container.GetInstance <ScopedWorker>();
            }

            container.GetInstance <AConsumer>();
            container.GetInstance <AConsumer>();
            container.GetInstance <BConsumer>();
            container.GetInstance <BConsumer>();
        }
Ejemplo n.º 22
0
 public IBus ResolveBus() => _container.Resolve <IBus>();