public void ResolveAllStartable()
        {
            var registrar = new ContainerRegistrar();
            registrar.RegisterConcrete<Startable1>(Lifetime.Singleton);
            registrar.RegisterConcrete<Startable2>(Lifetime.Singleton);
            var builder = new ContainerBuilder();
            var container = builder.Build(registrar);

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

            Assert.Equal(2, instances.Count());
        }
Example #2
0
        public void Test()
        {
            var registrar = new ContainerRegistrar();

            registrar.RegisterConcrete <Service1>();
            registrar.RegisterConcrete <Service2>();
            registrar.RegisterConcrete <Service3>();

            var builder = new ContainerBuilder();

            Assert.Throws <CircularDependenciesException>(() => builder.Build(registrar));
        }
        public void OneDependencySingleton()
        {
            var registrar = new ContainerRegistrar();
            registrar.RegisterConcrete<MySelf>(Lifetime.Transient);
            registrar.RegisterConcrete<OneDepencency>(Lifetime.Singleton);
            var builder = new ContainerBuilder();
            var container = builder.Build(registrar);

            var instance1 = container.Resolve<OneDepencency>();
            var instance2 = container.Resolve<OneDepencency>();

            Assert.Same(instance1, instance2);
        }
        public void ResolveAllStartable()
        {
            var registrar = new ContainerRegistrar();

            registrar.RegisterConcrete <Startable1>(Lifetime.Singleton);
            registrar.RegisterConcrete <Startable2>(Lifetime.Singleton);
            var builder   = new ContainerBuilder();
            var container = builder.Build(registrar);

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

            Assert.Equal(2, instances.Count());
        }
        public void Test()
        {
            var registrar = new ContainerRegistrar(Lifetime.Transient);
            registrar.RegisterConcrete<Will>();
            registrar.RegisterConcrete<Wont>();
            registrar.RegisterConcrete<Subject>();

            var c = registrar.Build();
            var actual = c.Resolve<Subject>();

            Assert.NotNull(actual);
            Assert.IsAssignableFrom<Subject>(actual);
        }
Example #6
0
        public void Test()
        {
            var registrar = new ContainerRegistrar(Lifetime.Transient);

            registrar.RegisterConcrete <Will>();
            registrar.RegisterConcrete <Wont>();
            registrar.RegisterConcrete <Subject>();

            var c      = registrar.Build();
            var actual = c.Resolve <Subject>();

            Assert.NotNull(actual);
            Assert.IsAssignableFrom <Subject>(actual);
        }
        public void OneDependencySingleton()
        {
            var registrar = new ContainerRegistrar();

            registrar.RegisterConcrete <MySelf>(Lifetime.Transient);
            registrar.RegisterConcrete <OneDepencency>(Lifetime.Singleton);
            var builder   = new ContainerBuilder();
            var container = builder.Build(registrar);

            var instance1 = container.Resolve <OneDepencency>();
            var instance2 = container.Resolve <OneDepencency>();

            Assert.Same(instance1, instance2);
        }
        private Container ConfigureGriffinContainer()
        {
            var registrar = new ContainerRegistrar();

            // Repositories
            registrar.RegisterConcrete<RaceMemoryRepository>(Lifetime.Singleton);
            registrar.RegisterConcrete<UserMemoryRepository>(Lifetime.Singleton);
            registrar.RegisterConcrete<ResultMemoryRepository>(Lifetime.Singleton);
            registrar.RegisterConcrete<ApplicationStateMemoryRepository>(Lifetime.Singleton);

            registrar.RegisterComponents(Lifetime.Default, typeof(RfidTagRegistrationHandler).Assembly);
            registrar.RegisterComponents(Lifetime.Singleton, typeof(DebugUserControlViewModel).Assembly);

            return registrar.Build();
        }
Example #9
0
        static void Main(string[] args)
        {
            var registrar = new ContainerRegistrar();

            registrar.RegisterConcrete <Hello>(Lifetime.Singleton); // will register as the implemented interfaces
            var container = registrar.Build();

            // will get
            var instance1 = container.Resolve <IPrintable>();
            var instance2 = container.Resolve <IInvokable>();

            // Will get same instance
            // since both is implemented by the same class.
            instance1.Print(null);
            instance2.DoSomeWork();

            try
            {
                container.Resolve <Hello>();
            }
            catch (ServiceNotRegisteredException)
            {
                // throws exception as we've implemented
                // non .NET specific interfaces.
                //
                // since we should not depend on concretes.
                // (design choice in griffin.container)
            }

            Console.WriteLine("Press enter to quit");
            Console.ReadLine();
        }
Example #10
0
        static void Main(string[] args)
        {
            var registrar = new ContainerRegistrar();
            registrar.RegisterConcrete<Hello>(Lifetime.Singleton); // will register as the implemented interfaces
            var container = registrar.Build();

            // will get
            var instance1 = container.Resolve<IPrintable>();
            var instance2 = container.Resolve<IInvokable>();

            // Will get same instance
            // since both is implemented by the same class.
            instance1.Print(null);
            instance2.DoSomeWork();

            try
            {
                container.Resolve<Hello>();
            }
            catch(ServiceNotRegisteredException)
            {
                // throws exception as we've implemented
                // non .NET specific interfaces.
                //
                // since we should not depend on concretes.
                // (design choice in griffin.container)
            }

            Console.WriteLine("Press enter to quit");
            Console.ReadLine();
        }
        public void Should_be_able_To_take_an_specified_generic_as_a_dependency()
        {
            var t  = typeof(ISomeDude <>);
            var t2 = typeof(ISomeDude <string>);

            var registrar = new ContainerRegistrar();

            registrar.RegisterType(typeof(IRestrictedGeneric <SomeClass>), typeof(RestrictedGeneric <SomeClass>), Lifetime.Singleton);
            registrar.RegisterConcrete <Word>(Lifetime.Transient);
            registrar.RegisterConcrete <SomeProcessor>(Lifetime.Singleton);
            var container = registrar.Build();

            var actual = container.Resolve <SomeProcessor>();

            Assert.NotNull(actual);
        }
        public void Build(Action <ContainerRegistrar> action, ConfigurationStore configStore)
        {
            var builder = new ContainerRegistrar();

            //need to invoke first to allow plug-ins to override default behavior.
            action(builder);

            builder.RegisterComponents(Lifetime.Scoped, Assembly.GetExecutingAssembly());
            builder.RegisterService(CreateUnitOfWork, Lifetime.Scoped);
            builder.RegisterService(CreateTaskInvoker, Lifetime.Singleton);

            RegisterBuiltInComponents(builder);
            RegisterQueues(builder);

            builder.RegisterService(x => Container, Lifetime.Singleton);
            builder.RegisterService(x => x);
            builder.RegisterConcrete <AnalysisDbContext>();
            builder.RegisterInstance(configStore);
            builder.RegisterType(typeof(IConfiguration <>), typeof(ConfigWrapper <>), Lifetime.Transient);
            builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
            builder.RegisterControllers(Assembly.GetExecutingAssembly());

            builder.RegisterService(x => configStore.Load <BaseConfiguration>());
            var ioc = builder.Build();

            DependencyResolver.SetResolver(new GriffinDependencyResolver(ioc));
            GlobalConfiguration.Configuration.DependencyResolver = new GriffinWebApiDependencyResolver2(ioc);
            Container = new GriffinContainerAdapter(ioc);
        }
        public void TwoChildContainers()
        {
            var registrar = new ContainerRegistrar();

            registrar.RegisterConcrete <MySelf>();
            var builder   = new ContainerBuilder();
            var container = builder.Build(registrar);

            MySelf instance1, instance2;
            var    childContainer1 = container.CreateChildContainer();
            var    childContainer2 = container.CreateChildContainer();

            instance1 = childContainer1.Resolve <MySelf>();
            instance2 = childContainer2.Resolve <MySelf>();
            Assert.False(instance1.IsDisposed);
            Assert.False(instance2.IsDisposed);

            childContainer1.Dispose();

            Assert.True(instance1.IsDisposed);
            Assert.False(instance2.IsDisposed);

            childContainer2.Dispose();

            Assert.True(instance2.IsDisposed);
        }
Example #14
0
        static void Main(string[] args)
        {
            var registrar = new ContainerRegistrar();

            registrar.RegisterConcrete <Hello>(Lifetime.Transient); // <-- transient
            var container = registrar.Build();

            // will get
            var instance1 = container.Resolve <Hello>();

            instance1.World();

            // a new instance
            var instance2 = container.Resolve <Hello>();

            instance2.World();

            // every time = transient
            var instance3 = container.Resolve <Hello>();

            instance3.World();


            Console.WriteLine("Press enter to quit");
            Console.ReadLine();
        }
        /// <summary>
        /// Creates a <see cref="T:System.ServiceModel.ServiceHost"/> for a specified type of service with a specific base address.
        /// </summary>
        /// <param name="serviceType">Specifies the type of service to host.</param>
        /// <param name="baseAddresses">The <see cref="T:System.Array"/> of type <see cref="T:System.Uri"/> that contains the base addresses for the service hosted.</param>
        /// <returns>
        /// A <see cref="T:System.ServiceModel.ServiceHost"/> for the type of service specified with a specific base address.
        /// </returns>
        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
            if (serviceType == null)
            {
                throw new ArgumentNullException("serviceType");
            }

            ServiceBehaviorAttribute attribute;
            var lifetime = GetLifetime(serviceType, out attribute);

            if (_container == null)
            {
                var registrar = new ContainerRegistrar();
                registrar.RegisterConcrete(serviceType, lifetime);
                registrar.RegisterModules(serviceType.Assembly);
                _container = registrar.Build();
            }

            var host = new ServiceLocatorServiceHost(_container, serviceType, baseAddresses);

            if (ConfigurationCallback != null)
            {
                ConfigurationCallback(host);
            }

            return(host);
        }
Example #16
0
        private void RegisterQueues(ContainerRegistrar builder)
        {
            builder.RegisterComponents(Lifetime.Scoped, typeof(QueueProvider).Assembly);

            var queueProviderTypeStr = ConfigurationManager.AppSettings["QueueProviderType"];

            if (string.IsNullOrEmpty(queueProviderTypeStr))
            {
                builder.RegisterConcrete <QueueProvider>(Lifetime.Singleton);
                return;
            }

            var type = Type.GetType(queueProviderTypeStr, true);

            builder.RegisterConcrete(type, Lifetime.Singleton);
        }
Example #17
0
        public void Build(Action <ContainerRegistrar> action, ConfigurationStore configStore)
        {
            var builder = new ContainerRegistrar();

            builder.RegisterComponents(Lifetime.Scoped, Assembly.GetExecutingAssembly());
            builder.RegisterService(CreateConnection, Lifetime.Scoped);
            builder.RegisterService(CreateTaskInvoker, Lifetime.Singleton);
            builder.RegisterInstance(Startup.ConnectionFactory);
            action(builder);

            RegisterBuiltInComponents(builder);
            RegisterQueues(builder);

            builder.RegisterService(x => Container, Lifetime.Singleton);
            builder.RegisterService(x => x);
            builder.RegisterConcrete <AnalysisDbContext>();
            builder.RegisterInstance(configStore);
            builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
            builder.RegisterControllers(Assembly.GetExecutingAssembly());

            var ioc = builder.Build();

            DependencyResolver.SetResolver(new GriffinDependencyResolver(ioc));
            GlobalConfiguration.Configuration.DependencyResolver = new GriffinWebApiDependencyResolver2(ioc);
            Container = new GriffinContainerAdapter(ioc);
        }
        public void TestDecorateHiearchy()
        {
            // register services
            var registrar = new ContainerRegistrar();
            registrar.RegisterConcrete<Decorated1>(Lifetime.Transient);
            registrar.RegisterConcrete<Decorated2>(Lifetime.Transient);
            registrar.RegisterConcrete<Decorated3>(Lifetime.Transient);
            var container = registrar.Build();

            // only log transient services
            var filter = new DelegateDecoratorFilter(ctx => ctx.Lifetime == Lifetime.Transient);
            var decorator = new ExceptionLoggerDecorator(this, filter);
            container.AddDecorator(decorator);

            // exception will be logged.
            var all = container.ResolveAll<IShouldBeDecorated>();
            Assert.True(all.All(x => x.GetType().Name.Contains("Proxy")));
        }
        public void TestSingleton()
        {
            var registrar = new ContainerRegistrar();

            registrar.RegisterInstance <MySelf>(new MySelf());
            registrar.RegisterConcrete <OneDepencency>(Lifetime.Singleton);
            var container = registrar.Build();

            container.Resolve <OneDepencency>();
        }
Example #20
0
        static void Main(string[] args)
        {
            var registrar = new ContainerRegistrar();
            registrar.RegisterConcrete<Hello>(Lifetime.Transient);
            registrar.RegisterConcrete<OurDependency>(Lifetime.Singleton);
            var container = registrar.Build();

            // gets a new OurDependency
            var obj1 = container.Resolve<Hello>();
            obj1.World();

            // New Hello instance, but with the same OurDependency.
            var obj2 = container.Resolve<Hello>();
            obj2.World();

            Console.WriteLine();
            Console.WriteLine("Press enter to quit");
            Console.ReadLine();
        }
        public void TestDelegateFactory()
        {
            var registrar = new ContainerRegistrar();

            registrar.RegisterService <MySelf>(ctnr => new MySelf(), Lifetime.Transient);
            registrar.RegisterConcrete <OneDepencency>(Lifetime.Singleton);
            var container = registrar.Build();

            container.Resolve <OneDepencency>();
        }
        public void Test()
        {
            var registrar = new ContainerRegistrar();
            registrar.RegisterConcrete<Service1>();

            var builder = new ContainerBuilder();
            Assert.Throws<ConcreteDependencyMissingException>(() =>builder.Build(registrar));


        }
        public void NoRegisteredConcretes()
        {
            var registrar = new ContainerRegistrar(Lifetime.Transient);
            registrar.RegisterConcrete<NoInstancesSubject>();

            var c = registrar.Build();
            var actual = c.Resolve<NoInstancesSubject>();

            Assert.NotNull(actual);
            Assert.IsAssignableFrom<NoInstancesSubject>(actual);
        }
        public void InterfaceToConcrete()
        {
            var registrar = new ContainerRegistrar();

            registrar.RegisterType(typeof(ISomeDude <>), typeof(SomeDude <>), Lifetime.Transient);
            registrar.RegisterConcrete <Word>(Lifetime.Transient);
            var container = registrar.Build();

            var actual = container.Resolve <ISomeDude <string> >();

            Assert.NotNull(actual);
        }
        public void Should_be_able_To_resolve_open_generics_which_are_type_restricted()
        {
            var registrar = new ContainerRegistrar();

            registrar.RegisterType(typeof(IRestrictedGeneric <>), typeof(RestrictedGeneric <>), Lifetime.Singleton);
            registrar.RegisterConcrete <Word>(Lifetime.Transient);
            var container = registrar.Build();

            var actual = container.Resolve <IRestrictedGeneric <SomeClass> >();

            Assert.NotNull(actual);
        }
Example #26
0
        public void NoRegisteredConcretes()
        {
            var registrar = new ContainerRegistrar(Lifetime.Transient);

            registrar.RegisterConcrete <NoInstancesSubject>();

            var c      = registrar.Build();
            var actual = c.Resolve <NoInstancesSubject>();

            Assert.NotNull(actual);
            Assert.IsAssignableFrom <NoInstancesSubject>(actual);
        }
        public void RegisterServiceAsInstance()
        {
            var registrar = new ContainerRegistrar(Lifetime.Transient);
            registrar.RegisterInstance<IThinkNot>(new Will());
            registrar.RegisterConcrete<InstanceSubject>();

            var c = registrar.Build();
            var actual = c.Resolve<InstanceSubject>();

            Assert.NotNull(actual);
            Assert.IsAssignableFrom<InstanceSubject>(actual);
        }
Example #28
0
        public void TestDecorateHiearchy()
        {
            // register services
            var registrar = new ContainerRegistrar();

            registrar.RegisterConcrete <Decorated1>(Lifetime.Transient);
            registrar.RegisterConcrete <Decorated2>(Lifetime.Transient);
            registrar.RegisterConcrete <Decorated3>(Lifetime.Transient);
            var container = registrar.Build();

            // only log transient services
            var filter    = new DelegateDecoratorFilter(ctx => ctx.Lifetime == Lifetime.Transient);
            var decorator = new ExceptionLoggerDecorator(this, filter);

            container.AddDecorator(decorator);

            // exception will be logged.
            var all = container.ResolveAll <IShouldBeDecorated>();

            Assert.True(all.All(x => x.GetType().Name.Contains("Proxy")));
        }
        public void Should_be_able_To_resolve_open_generics_in_singleton_scope()
        {
            var registrar = new ContainerRegistrar();

            registrar.RegisterType(typeof(ISomeDude <>), typeof(SomeDude <>), Lifetime.Singleton);
            registrar.RegisterConcrete <Word>(Lifetime.Transient);
            var container = registrar.Build();

            var actual = container.Resolve <ISomeDude <string> >();

            Assert.NotNull(actual);
        }
        public void SimpleRegistrationTransient()
        {
            var registrar = new ContainerRegistrar();
            registrar.RegisterConcrete<MySelf>(Lifetime.Transient);
            var builder = new ContainerBuilder();
            var container = builder.Build(registrar);

            var instance1 = container.Resolve<MySelf>();
            var instance2 = container.Resolve<MySelf>();

            Assert.NotSame(instance1, instance2);
        }
        public void RegisteredSpecific()
        {
            var registrar = new ContainerRegistrar();

            registrar.RegisterType(typeof(ISomeDude <>), typeof(SomeDude <>), Lifetime.Transient);
            registrar.RegisterType(typeof(ISomeDude <int>), typeof(MyClass), Lifetime.Transient);
            registrar.RegisterConcrete <Word>(Lifetime.Transient);
            var container = registrar.Build();

            var actual = container.Resolve <ISomeDude <int> >();

            Assert.IsType <MyClass>(actual);
        }
Example #32
0
        public void RegisterServiceAsInstance()
        {
            var registrar = new ContainerRegistrar(Lifetime.Transient);

            registrar.RegisterInstance <IThinkNot>(new Will());
            registrar.RegisterConcrete <InstanceSubject>();

            var c      = registrar.Build();
            var actual = c.Resolve <InstanceSubject>();

            Assert.NotNull(actual);
            Assert.IsAssignableFrom <InstanceSubject>(actual);
        }
Example #33
0
        static void Main(string[] args)
        {
            var registrar = new ContainerRegistrar();

            registrar.RegisterConcrete <Hello>(Lifetime.Transient);
            registrar.RegisterConcrete <OurDependency>(Lifetime.Singleton);
            var container = registrar.Build();

            // gets a new OurDependency
            var obj1 = container.Resolve <Hello>();

            obj1.World();

            // New Hello instance, but with the same OurDependency.
            var obj2 = container.Resolve <Hello>();

            obj2.World();

            Console.WriteLine();
            Console.WriteLine("Press enter to quit");
            Console.ReadLine();
        }
        public void SimpleRegistrationTransient()
        {
            var registrar = new ContainerRegistrar();

            registrar.RegisterConcrete <MySelf>(Lifetime.Transient);
            var builder   = new ContainerBuilder();
            var container = builder.Build(registrar);

            var instance1 = container.Resolve <MySelf>();
            var instance2 = container.Resolve <MySelf>();

            Assert.NotSame(instance1, instance2);
        }
Example #35
0
        public void Test()
        {
            var reg = new ContainerRegistrar();

            reg.RegisterConcrete <FirstService>(Lifetime.Singleton);
            reg.RegisterConcrete <AnotherFirstService>(Lifetime.Scoped);
            reg.RegisterConcrete <TestWithDependency>(Lifetime.Singleton);
            reg.RegisterConcrete <TestWithDependency2>(Lifetime.Scoped);
            var container = reg.Build();

            var single = container.Resolve <IFirstService>();

            container.Resolve <TestWithDependency>();
            IFirstService scoped;

            using (var scope = container.CreateChildContainer())
            {
                scoped = scope.Resolve <IFirstService>();
                scope.Resolve <TestWithDependency2>();
            }

            Assert.IsType <FirstService>(single);
            Assert.IsType <AnotherFirstService>(scoped);
        }
        public void ChildContainer()
        {
            var registrar = new ContainerRegistrar();
            registrar.RegisterConcrete<MySelf>();
            var builder = new ContainerBuilder();
            var container = builder.Build(registrar);

            MySelf instance;
            using (var childContainer = container.CreateChildContainer())
            {
                instance = childContainer.Resolve<MySelf>();
            }

            Assert.True(instance.IsDisposed);
        }
        public void ChildContainer()
        {
            var registrar = new ContainerRegistrar();

            registrar.RegisterConcrete <MySelf>();
            var builder   = new ContainerBuilder();
            var container = builder.Build(registrar);

            MySelf instance;

            using (var childContainer = container.CreateChildContainer())
            {
                instance = childContainer.Resolve <MySelf>();
            }

            Assert.True(instance.IsDisposed);
        }
        public void TestLoggingDecorator()
        {
            // register services
            var registrar = new ContainerRegistrar();
            registrar.RegisterConcrete<TotalFailure>(Lifetime.Transient);
            var container = registrar.Build();

            // only log transient services
            var filter = new DelegateDecoratorFilter(ctx => ctx.Lifetime == Lifetime.Transient);
            var decorator = new ExceptionLoggerDecorator(this, filter);
            container.AddDecorator(decorator);

            // exception will be logged.
            var tmp = container.Resolve<TotalFailure>();
            Assert.Throws<InvalidOperationException>(() => tmp.Fail("Big!"));
            Assert.IsType<InvalidOperationException>(_exception);
        }
Example #39
0
        public void TestLoggingDecorator()
        {
            // register services
            var registrar = new ContainerRegistrar();

            registrar.RegisterConcrete <TotalFailure>(Lifetime.Transient);
            var container = registrar.Build();

            // only log transient services
            var filter    = new DelegateDecoratorFilter(ctx => ctx.Lifetime == Lifetime.Transient);
            var decorator = new ExceptionLoggerDecorator(this, filter);

            container.AddDecorator(decorator);

            // exception will be logged.
            var tmp = container.Resolve <TotalFailure>();

            Assert.Throws <InvalidOperationException>(() => tmp.Fail("Big!"));
            Assert.IsType <InvalidOperationException>(_exception);
        }
Example #40
0
        static void Main(string[] args)
        {
            var registrar = new ContainerRegistrar();
            registrar.RegisterConcrete<Hello>(Lifetime.Singleton); //<-- singleton
            var container = registrar.Build();

            // Will get
            var instance1 = container.Resolve<Hello>();
            instance1.World();

            // the same instance
            var instance2 = container.Resolve<Hello>();
            instance2.World();

            // every time = singleton
            var instance3 = container.Resolve<Hello>();
            instance3.World();

            Console.WriteLine("Press enter to quit");
            Console.ReadLine();
        }
Example #41
0
        static void Main(string[] args)
        {
            var registrar = new ContainerRegistrar();
            registrar.RegisterConcrete<Hello>(Lifetime.Transient); // <-- transient
            var container = registrar.Build();

            // will get
            var instance1 = container.Resolve<Hello>();
            instance1.World();

            // a new instance
            var instance2 = container.Resolve<Hello>();
            instance2.World();

            // every time = transient
            var instance3 = container.Resolve<Hello>();
            instance3.World();

            Console.WriteLine("Press enter to quit");
            Console.ReadLine();
        }
        /// <summary>
        /// Creates a <see cref="T:System.ServiceModel.ServiceHost"/> for a specified type of service with a specific base address.
        /// </summary>
        /// <param name="serviceType">Specifies the type of service to host.</param>
        /// <param name="baseAddresses">The <see cref="T:System.Array"/> of type <see cref="T:System.Uri"/> that contains the base addresses for the service hosted.</param>
        /// <returns>
        /// A <see cref="T:System.ServiceModel.ServiceHost"/> for the type of service specified with a specific base address.
        /// </returns>
        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
            if (serviceType == null) throw new ArgumentNullException("serviceType");

            ServiceBehaviorAttribute attribute;
            var lifetime = GetLifetime(serviceType, out attribute);

            if (_container == null)
            {
                var registrar = new ContainerRegistrar();
                registrar.RegisterConcrete(serviceType, lifetime);
                registrar.RegisterModules(serviceType.Assembly);
                _container = registrar.Build();
            }

            var host = new ServiceLocatorServiceHost(_container, serviceType, baseAddresses);
            if (ConfigurationCallback != null)
                ConfigurationCallback(host);

            return host;
        }
Example #43
0
        static void Main(string[] args)
        {
            var registrar = new ContainerRegistrar();

            registrar.RegisterConcrete <Hello>(Lifetime.Scoped); //<-- scoped
            var container = registrar.Build();

            try
            {
                // May not resolve scoped objects
                // in the global container.
                var instance1 = container.Resolve <Hello>();
                instance1.World();
            }
            catch (InvalidOperationException)
            {
            }

            using (var scope = container.CreateChildContainer())
            {
                // notice that it get disposed when the scope gets disposed.
                var obj1 = scope.Resolve <Hello>();
                obj1.World();

                // will get same instance
                var obj2 = scope.Resolve <Hello>();
                obj2.World();
            }

            using (var scope = container.CreateChildContainer())
            {
                // Will get a new instance, since it's a new scope.
                var obj1 = scope.Resolve <Hello>();
                obj1.World();
            }


            Console.WriteLine("Press enter to quit");
            Console.ReadLine();
        }
Example #44
0
        static void Main(string[] args)
        {
            var registrar = new ContainerRegistrar();
            registrar.RegisterConcrete<Hello>(Lifetime.Scoped); //<-- scoped
            var container = registrar.Build();

            try
            {
                // May not resolve scoped objects
                // in the global container.
                var instance1 = container.Resolve<Hello>();
                instance1.World();
            }
            catch(InvalidOperationException)
            {
            }

            using (var scope = container.CreateChildContainer())
            {
                // notice that it get disposed when the scope gets disposed.
                var obj1 = scope.Resolve<Hello>();
                obj1.World();

                // will get same instance
                var obj2 = scope.Resolve<Hello>();
                obj2.World();
            }

            using (var scope = container.CreateChildContainer())
            {
                // Will get a new instance, since it's a new scope.
                var obj1 = scope.Resolve<Hello>();
                obj1.World();
            }

            Console.WriteLine("Press enter to quit");
            Console.ReadLine();
        }
Example #45
0
        static void Main(string[] args)
        {
            // Used to configure which classes
            // the container should create
            var registrar = new ContainerRegistrar();

            // transient = new object every time
            registrar.RegisterConcrete<Hello>(Lifetime.Transient);

            // Build the actual container
            // nothing can be done with the registrar after this point.
            // (as changes whill have no effect unless you build a new container)
            var container = registrar.Build();

            // Ask the container after your class.
            var instance = container.Resolve<Hello>();

            // and invoke something on it
            instance.World();

            Console.WriteLine("Press enter to quit");
            Console.ReadLine();
        }
        public void TestDelegateFactory()
        {
            var registrar = new ContainerRegistrar();
            registrar.RegisterService<MySelf>(ctnr => new MySelf(), Lifetime.Transient);
            registrar.RegisterConcrete<OneDepencency>(Lifetime.Singleton);
            var container = registrar.Build();

            container.Resolve<OneDepencency>();
        }
 public void Init()
 {
     _registrar = new ContainerRegistrar();
     _registrar.RegisterConcrete<UserMemoryRepository>(Lifetime.Singleton);
     _registrar.Build();
 }
        public void TestSingleton()
        {
            var registrar = new ContainerRegistrar();
            registrar.RegisterInstance<MySelf>(new MySelf());
            registrar.RegisterConcrete<OneDepencency>(Lifetime.Singleton);
            var container = registrar.Build();

            container.Resolve<OneDepencency>();
        }
        public void TwoChildContainers()
        {
            var registrar = new ContainerRegistrar();
            registrar.RegisterConcrete<MySelf>();
            var builder = new ContainerBuilder();
            var container = builder.Build(registrar);

            MySelf instance1, instance2;
            var childContainer1 = container.CreateChildContainer();
            var childContainer2 = container.CreateChildContainer();
            instance1 = childContainer1.Resolve<MySelf>();
            instance2 = childContainer2.Resolve<MySelf>();
            Assert.False(instance1.IsDisposed);
            Assert.False(instance2.IsDisposed);

            childContainer1.Dispose();

            Assert.True(instance1.IsDisposed);
            Assert.False(instance2.IsDisposed);
            
            childContainer2.Dispose();

            Assert.True(instance2.IsDisposed);
        }