Пример #1
0
        public void ShouldBeAbleToCreateSingletonsThatDependOnOtherSingletons()
        {
            var map = new DependencyMap();

            map.AddSingletonService(typeof(IVehicle), typeof(Vehicle));
            map.AddSingletonService(typeof(IPerson), typeof(Person));

            var compiler       = map.ContainerCompiler;
            var outputAssembly = compiler.Compile("MicroContainer", "Hiro.Containers", "Hiro.CompiledContainers", map);

            outputAssembly.Write("singletonOutputAssembly.dll");

            var container = map.CreateContainer();
            var vehicle   = container.GetInstance <IVehicle>();

            Assert.IsNotNull(vehicle);

            var person = container.GetInstance <IPerson>();

            Assert.IsNotNull(person);
            for (var i = 0; i < 1000; i++)
            {
                var currentInstance = container.GetInstance <IVehicle>();
                Assert.AreSame(vehicle, currentInstance);

                var driver = currentInstance.Driver;
                Assert.AreSame(driver, person);
            }
        }
Пример #2
0
        public void ShouldBeAbleToCreateSingletonsThatDependOnOtherSingletons()
        {
            var map = new DependencyMap();
            map.AddSingletonService(typeof(IVehicle), typeof(Vehicle));
            map.AddSingletonService(typeof(IPerson), typeof(Person));

            var compiler = map.ContainerCompiler;
            var outputAssembly = compiler.Compile("MicroContainer", "Hiro.Containers", "Hiro.CompiledContainers", map);
            outputAssembly.Write("singletonOutputAssembly.dll");

            var container = map.CreateContainer();
            var vehicle = container.GetInstance<IVehicle>();
            Assert.IsNotNull(vehicle);

            var person = container.GetInstance<IPerson>();
            Assert.IsNotNull(person);
            for (var i = 0; i < 1000; i++)
            {
                var currentInstance = container.GetInstance<IVehicle>();
                Assert.AreSame(vehicle, currentInstance);

                var driver = currentInstance.Driver;
                Assert.AreSame(driver, person);
            }
        }
 private static void RegisterComplex(DependencyMap map)
 {
     map.AddSingletonService <IFirstService, FirstService>();
     map.AddSingletonService <ISecondService, SecondService>();
     map.AddSingletonService <IThirdService, ThirdService>();
     map.AddService <ISubObjectOne, SubObjectOne>();
     map.AddService <ISubObjectTwo, SubObjectTwo>();
     map.AddService <ISubObjectThree, SubObjectThree>();
     map.AddService <IComplex, Complex>();
 }
 private static void RegisterComplex(DependencyMap map)
 {
     map.AddSingletonService<IFirstService, FirstService>();
     map.AddSingletonService<ISecondService, SecondService>();
     map.AddSingletonService<IThirdService, ThirdService>();
     map.AddService<ISubObjectOne, SubObjectOne>();
     map.AddService<ISubObjectTwo, SubObjectTwo>();
     map.AddService<ISubObjectThree, SubObjectThree>();
     map.AddService<IComplex, Complex>();
 }
 private static void RegisterStandard(DependencyMap map)
 {
     map.AddSingletonService<ISingleton1, Singleton1>();
     map.AddSingletonService<ISingleton2, Singleton2>();
     map.AddSingletonService<ISingleton3, Singleton3>();
     map.AddService<ITransient1, Transient1>();
     map.AddService<ITransient2, Transient2>();
     map.AddService<ITransient3, Transient3>();
     map.AddService<ICombined1, Combined1>();
     map.AddService<ICombined2, Combined2>();
     map.AddService<ICombined3, Combined3>();
 }
Пример #6
0
 private static void RegisterStandard(DependencyMap map)
 {
     map.AddSingletonService <ISingleton1, Singleton1>();
     map.AddSingletonService <ISingleton2, Singleton2>();
     map.AddSingletonService <ISingleton3, Singleton3>();
     map.AddService <ITransient1, Transient1>();
     map.AddService <ITransient2, Transient2>();
     map.AddService <ITransient3, Transient3>();
     map.AddService <ICombined1, Combined1>();
     map.AddService <ICombined2, Combined2>();
     map.AddService <ICombined3, Combined3>();
 }
Пример #7
0
        private static void RegisterPropertyInjection(DependencyMap map)
        {
            map.AddSingletonService <IServiceA, ServiceA>();
            map.AddSingletonService <IServiceB, ServiceB>();
            map.AddSingletonService <IServiceC, ServiceC>();
            map.AddService(new Func <IMicroContainer, ISubObjectA>(
                               microContainer => new SubObjectA {
                ServiceA = microContainer.GetInstance <IServiceA>()
            }));
            map.AddService(new Func <IMicroContainer, ISubObjectB>(
                               microContainer => new SubObjectB {
                ServiceB = microContainer.GetInstance <IServiceB>()
            }));
            map.AddService(new Func <IMicroContainer, ISubObjectC>(
                               microContainer => new SubObjectC {
                ServiceC = microContainer.GetInstance <IServiceC>()
            }));

            // HACK: We must wrap the delegate explicitly in a Func<T, TResult> or else resolving will fail.
            map.AddService(new Func <IMicroContainer, IComplexPropertyObject1>(
                               microContainer => new ComplexPropertyObject1
            {
                ServiceA   = microContainer.GetInstance <IServiceA>(),
                ServiceB   = microContainer.GetInstance <IServiceB>(),
                ServiceC   = microContainer.GetInstance <IServiceC>(),
                SubObjectA = microContainer.GetInstance <ISubObjectA>(),
                SubObjectB = microContainer.GetInstance <ISubObjectB>(),
                SubObjectC = microContainer.GetInstance <ISubObjectC>()
            }));

            map.AddService(new Func <IMicroContainer, IComplexPropertyObject2>(
                               microContainer => new ComplexPropertyObject2
            {
                ServiceA   = microContainer.GetInstance <IServiceA>(),
                ServiceB   = microContainer.GetInstance <IServiceB>(),
                ServiceC   = microContainer.GetInstance <IServiceC>(),
                SubObjectA = microContainer.GetInstance <ISubObjectA>(),
                SubObjectB = microContainer.GetInstance <ISubObjectB>(),
                SubObjectC = microContainer.GetInstance <ISubObjectC>()
            }));

            map.AddService(new Func <IMicroContainer, IComplexPropertyObject3>(
                               microContainer => new ComplexPropertyObject3
            {
                ServiceA   = microContainer.GetInstance <IServiceA>(),
                ServiceB   = microContainer.GetInstance <IServiceB>(),
                ServiceC   = microContainer.GetInstance <IServiceC>(),
                SubObjectA = microContainer.GetInstance <ISubObjectA>(),
                SubObjectB = microContainer.GetInstance <ISubObjectB>(),
                SubObjectC = microContainer.GetInstance <ISubObjectC>()
            }));
        }
Пример #8
0
        public void ShouldBeAbleToRegisterNamedSingletonServicesWithDependencyMapUsingGenerics()
        {
            var serviceName   = "MyService";
            var dependencyMap = new DependencyMap();

            dependencyMap.AddSingletonService <IVehicle, Vehicle>(serviceName);
            Assert.IsTrue(dependencyMap.Contains(typeof(IVehicle), serviceName));
        }
Пример #9
0
        public void ShouldBeAbleToRegisterAnonymousSingletonServicesWithDependencyMap()
        {
            var dependencyMap = new DependencyMap();

            dependencyMap.AddSingletonService <IVehicle, Vehicle>();

            Assert.IsTrue(dependencyMap.Contains(new Dependency(typeof(IVehicle))));
            Assert.IsTrue(dependencyMap.Contains(typeof(IVehicle)));
        }
 /// <summary>
 /// Registers the specified service type.
 /// </summary>
 /// <param name="serviceType">Type of the service.</param>
 public void Register(Type serviceType)
 {
     if (IsDefaultLifetime())
     {
         _builder.AddService(serviceType, serviceType);
     }
     else
     {
         _builder.AddSingletonService(serviceType, serviceType);
     }
 }
Пример #11
0
        public override void Prepare()
        {
            var map = new DependencyMap();

            map.AddSingletonService<ISingleton, Singleton>();
            map.AddService<ITransient, Transient>();
            map.AddService<ICombined, Combined>();

            this.container = map.CreateContainer();
        }
Пример #12
0
        public void Prepare()
        {
            var map = new DependencyMap();

            map.AddSingletonService <ISingleton, Singleton>();
            map.AddService <ITransient, Transient>();
            map.AddService <ICombined, Combined>();

            this.container = map.CreateContainer();
        }
Пример #13
0
        public void ShouldBeAbleToInstantiateGenericSingletonTypes()
        {
            var map = new DependencyMap();
            map.AddSingletonService(typeof(IList<>), typeof(List<>));

            var container = map.CreateContainer();
            var list = container.GetInstance<IList<int>>();
            Assert.IsNotNull(list);

            var otherList = container.GetInstance<IList<int>>();
            Assert.AreSame(list, otherList);
        }
Пример #14
0
        public void ShouldBeAbleToAddNamedServiceToMapUsingExtensionMethod()
        {
            var map = new DependencyMap();

            map.AddSingletonService("SomePerson", typeof(IPerson), typeof(Person));

            var container = map.CreateContainer();
            var result    = container.GetInstance(typeof(IPerson), "SomePerson");

            Assert.IsNotNull(result);
            Assert.IsTrue(result is Person);
        }
Пример #15
0
        public Hiro()
        {
            var map = new DependencyMap();
            map.AddSingletonService<Game, Game>();
            map.AddService<Player, Player>();
            map.AddService<Gun, Gun>();
            map.AddService<Bullet, Bullet>();

            Func<IMicroContainer, Bullet> createBullet = c => c.GetInstance<Bullet>();
            Func<IMicroContainer, Func<Bullet>> createBulletFunctor = c => () => createBullet(c);
            map.AddService(createBulletFunctor);

            container = map.CreateContainer();
        }
Пример #16
0
        public void ShouldBeAbleToInjectGenericConstructor()
        {
            var map = new DependencyMap();
            map.AddService<UnitOfWorkScopeBase<UserUnitOfWork>,
            SimpleUnitOfWorkScope<UserUnitOfWork>>();

            map.AddSingletonService<LightSpeedContext<UserUnitOfWork>,
            LightSpeedContext<UserUnitOfWork>>();

            var container = map.CreateContainer();
            var service = container.GetInstance<UnitOfWorkScopeBase<UserUnitOfWork>>();

            Assert.IsNotNull(service);
        }
Пример #17
0
        public void ShouldBeAbleToInstantiateGenericSingletonTypes()
        {
            var map = new DependencyMap();

            map.AddSingletonService(typeof(IList <>), typeof(List <>));

            var container = map.CreateContainer();
            var list      = container.GetInstance <IList <int> >();

            Assert.IsNotNull(list);

            var otherList = container.GetInstance <IList <int> >();

            Assert.AreSame(list, otherList);
        }
Пример #18
0
        public void ShouldBeAbleToReturnTheSameSingletonInstance()
        {
            var map = new DependencyMap();

            map.AddSingletonService(typeof(IPerson), typeof(Person));

            var container = map.CreateContainer();
            var result    = container.GetInstance(typeof(IPerson), null);

            for (var i = 0; i < 100; i++)
            {
                var currentResult = container.GetInstance(typeof(IPerson), null);
                Assert.AreSame(result, currentResult);
            }
        }
Пример #19
0
        public void ShouldBeAbleToInjectGenericConstructor()
        {
            var map = new DependencyMap();

            map.AddService <UnitOfWorkScopeBase <UserUnitOfWork>,
                            SimpleUnitOfWorkScope <UserUnitOfWork> >();

            map.AddSingletonService <LightSpeedContext <UserUnitOfWork>,
                                     LightSpeedContext <UserUnitOfWork> >();

            var container = map.CreateContainer();
            var service   = container.GetInstance <UnitOfWorkScopeBase <UserUnitOfWork> >();

            Assert.IsNotNull(service);
        }
Пример #20
0
        public Hiro()
        {
            var map = new DependencyMap();

            map.AddSingletonService <Game, Game>();
            map.AddService <Player, Player>();
            map.AddService <Gun, Gun>();
            map.AddService <Bullet, Bullet>();

            Func <IMicroContainer, Bullet>         createBullet        = c => c.GetInstance <Bullet>();
            Func <IMicroContainer, Func <Bullet> > createBulletFunctor = c => () => createBullet(c);

            map.AddService(createBulletFunctor);

            container = map.CreateContainer();
        }
Пример #21
0
        public void ShouldCallInitializeOnSingletonTypeThatImplementsIInitializeOnceAndOnlyOnce()
        {
            var map = new DependencyMap();

            map.AddSingletonService <IInitialize, SampleInitialize>();

            var container = map.CreateContainer();
            var result    = (SampleInitialize)container.GetInstance <IInitialize>();

            for (var i = 0; i < 100; i++)
            {
                result = (SampleInitialize)container.GetInstance <IInitialize>();
            }

            Assert.AreSame(container, result.Container);
            Assert.IsTrue(result.NumberOfTimesInitialized == 1);
        }
Пример #22
0
        public void ShouldCallCacheOnCachedServiceType()
        {
            var map                  = new DependencyMap();
            var dependency           = new Dependency(typeof(IFoo));
            var implementation       = new TransientType(typeof(Foo), map, new ConstructorResolver());
            var cachedImplementation = new CachedInstantiation(implementation);

            map.AddSingletonService <ICache, MockCache>();
            map.AddService(dependency, cachedImplementation);

            // Compile the container
            var container = map.CreateContainer();

            // Grab the service instance
            var firstResult  = container.GetInstance <IFoo>();
            var secondResult = container.GetInstance <IFoo>();

            Assert.IsNotNull(firstResult);
            Assert.AreSame(firstResult, secondResult);
        }
Пример #23
0
        public void ShouldCallCacheOnCachedServiceType()
        {
            var map = new DependencyMap();
            var dependency = new Dependency(typeof(IFoo));
            var implementation = new TransientType(typeof(Foo), map, new ConstructorResolver());
            var cachedImplementation = new CachedInstantiation(implementation);

            map.AddSingletonService<ICache, MockCache>();
            map.AddService(dependency, cachedImplementation);

            // Compile the container
            var container = map.CreateContainer();

            // Grab the service instance
            var firstResult = container.GetInstance<IFoo>();
            var secondResult = container.GetInstance<IFoo>();

            Assert.IsNotNull(firstResult);
            Assert.AreSame(firstResult, secondResult);
        }
Пример #24
0
        public void ShouldBeAbleToResolvePlayerInstanceWithoutEncounteringACLRLimitationError()
        {
            var map = new DependencyMap();

            map.AddSingletonService <Game, Game>();
            map.AddService <Player, Player>();
            map.AddService <Gun, Gun>();
            map.AddService <Bullet, Bullet>();

            Func <IMicroContainer, Bullet>         createBullet        = c => c.GetInstance <Bullet>();
            Func <IMicroContainer, Func <Bullet> > createBulletFunctor = c => () => createBullet(c);

            map.AddService(createBulletFunctor);

            var container = map.CreateContainer();

            Assert.IsNotNull(container);

            var player = container.GetInstance <Player>();

            Assert.IsNotNull(player);

            player.Shoot();
        }
Пример #25
0
 private static void RegisterStandard(DependencyMap map)
 {
     map.AddSingletonService<ISingleton, Singleton>();
     map.AddService<ITransient, Transient>();
     map.AddService<ICombined, Combined>();
 }
Пример #26
0
        public void ShouldBeAbleToRegisterAnonymousSingletonServicesWithDependencyMap()
        {
            var dependencyMap = new DependencyMap();
            dependencyMap.AddSingletonService<IVehicle, Vehicle>();

            Assert.IsTrue(dependencyMap.Contains(new Dependency(typeof(IVehicle))));
            Assert.IsTrue(dependencyMap.Contains(typeof(IVehicle)));
        }
 private static void RegisterPropertyInjection(DependencyMap map)
 {
     map.AddSingletonService<IServiceA, ServiceA>();
     map.AddSingletonService<IServiceB, ServiceB>();
     map.AddSingletonService<IServiceC, ServiceC>();
     map.AddService(new Func<IMicroContainer, ISubObjectA>(
         microContainer => new SubObjectA { ServiceA = microContainer.GetInstance<IServiceA>() }));
     map.AddService(new Func<IMicroContainer, ISubObjectB>(
         microContainer => new SubObjectB { ServiceB = microContainer.GetInstance<IServiceB>() }));
     map.AddService(new Func<IMicroContainer, ISubObjectC>(
         microContainer => new SubObjectC { ServiceC = microContainer.GetInstance<IServiceC>() }));
     map.AddService(new Func<IMicroContainer, IComplexPropertyObject>(
         microContainer => new ComplexPropertyObject
                               {
                                   ServiceA = microContainer.GetInstance<IServiceA>(),
                                   ServiceB = microContainer.GetInstance<IServiceB>(),
                                   ServiceC = microContainer.GetInstance<IServiceC>(),
                                   SubObjectA = microContainer.GetInstance<ISubObjectA>(),
                                   SubObjectB = microContainer.GetInstance<ISubObjectB>(),
                                   SubObjectC = microContainer.GetInstance<ISubObjectC>()
                               }));
 }
 /// <summary>
 /// Registers the generic service type as a singleton type.
 /// </summary>
 /// <param name="serviceType">The service type that will be registered.</param>
 /// <param name="concreteType">The generic concrete type that will implement the generic service type.</param>
 /// <param name="map">The dependency map that contains all the dependencies.</param>
 protected override void Register(Type serviceType, Type concreteType, DependencyMap map)
 {
     map.AddSingletonService(serviceType, concreteType);
 }
Пример #29
0
 private static void RegisterStandard(DependencyMap map)
 {
     map.AddSingletonService <ISingleton, Singleton>();
     map.AddService <ITransient, Transient>();
     map.AddService <ICombined, Combined>();
 }
Пример #30
0
 public void ShouldBeAbleToRegisterNamedSingletonServicesWithDependencyMapUsingGenerics()
 {
     var serviceName = "MyService";
     var dependencyMap = new DependencyMap();
     dependencyMap.AddSingletonService<IVehicle, Vehicle>(serviceName);
     Assert.IsTrue(dependencyMap.Contains(typeof(IVehicle), serviceName));
 }
Пример #31
0
        private static void RegisterPropertyInjection(DependencyMap map)
        {
            map.AddSingletonService<IServiceA, ServiceA>();
            map.AddSingletonService<IServiceB, ServiceB>();
            map.AddSingletonService<IServiceC, ServiceC>();
            map.AddService(new Func<IMicroContainer, ISubObjectA>(
                microContainer => new SubObjectA { ServiceA = microContainer.GetInstance<IServiceA>() }));
            map.AddService(new Func<IMicroContainer, ISubObjectB>(
                microContainer => new SubObjectB { ServiceB = microContainer.GetInstance<IServiceB>() }));
            map.AddService(new Func<IMicroContainer, ISubObjectC>(
                microContainer => new SubObjectC { ServiceC = microContainer.GetInstance<IServiceC>() }));
            
            // HACK: We must wrap the delegate explicitly in a Func<T, TResult> or else resolving will fail.
            map.AddService(new Func<IMicroContainer, IComplexPropertyObject1>(
                microContainer => new ComplexPropertyObject1
                {
                    ServiceA = microContainer.GetInstance<IServiceA>(),
                    ServiceB = microContainer.GetInstance<IServiceB>(),
                    ServiceC = microContainer.GetInstance<IServiceC>(),
                    SubObjectA = microContainer.GetInstance<ISubObjectA>(),
                    SubObjectB = microContainer.GetInstance<ISubObjectB>(),
                    SubObjectC = microContainer.GetInstance<ISubObjectC>()
                }));

            map.AddService(new Func<IMicroContainer, IComplexPropertyObject2>(
                microContainer => new ComplexPropertyObject2
                {
                    ServiceA = microContainer.GetInstance<IServiceA>(),
                    ServiceB = microContainer.GetInstance<IServiceB>(),
                    ServiceC = microContainer.GetInstance<IServiceC>(),
                    SubObjectA = microContainer.GetInstance<ISubObjectA>(),
                    SubObjectB = microContainer.GetInstance<ISubObjectB>(),
                    SubObjectC = microContainer.GetInstance<ISubObjectC>()
                }));

            map.AddService(new Func<IMicroContainer, IComplexPropertyObject3>(
                microContainer => new ComplexPropertyObject3
                {
                    ServiceA = microContainer.GetInstance<IServiceA>(),
                    ServiceB = microContainer.GetInstance<IServiceB>(),
                    ServiceC = microContainer.GetInstance<IServiceC>(),
                    SubObjectA = microContainer.GetInstance<ISubObjectA>(),
                    SubObjectB = microContainer.GetInstance<ISubObjectB>(),
                    SubObjectC = microContainer.GetInstance<ISubObjectC>()
                }));
        }
Пример #32
0
        public void ShouldBeAbleToResolvePlayerInstanceWithoutEncounteringACLRLimitationError()
        {
            var map = new DependencyMap();
            map.AddSingletonService<Game, Game>();
            map.AddService<Player, Player>();
            map.AddService<Gun, Gun>();
            map.AddService<Bullet, Bullet>();

            Func<IMicroContainer, Bullet> createBullet = c => c.GetInstance<Bullet>();
            Func<IMicroContainer, Func<Bullet>> createBulletFunctor = c => () => createBullet(c);
            map.AddService(createBulletFunctor);

            var container = map.CreateContainer();
            Assert.IsNotNull(container);

            var player = container.GetInstance<Player>();
            Assert.IsNotNull(player);

            player.Shoot();
        }