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); } }
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>(); }
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>(); }
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>() })); }
public void ShouldBeAbleToRegisterNamedSingletonServicesWithDependencyMapUsingGenerics() { var serviceName = "MyService"; var dependencyMap = new DependencyMap(); dependencyMap.AddSingletonService <IVehicle, Vehicle>(serviceName); Assert.IsTrue(dependencyMap.Contains(typeof(IVehicle), serviceName)); }
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); } }
public override void Prepare() { var map = new DependencyMap(); map.AddSingletonService<ISingleton, Singleton>(); map.AddService<ITransient, Transient>(); map.AddService<ICombined, Combined>(); this.container = map.CreateContainer(); }
public void Prepare() { var map = new DependencyMap(); map.AddSingletonService <ISingleton, Singleton>(); map.AddService <ITransient, Transient>(); map.AddService <ICombined, Combined>(); this.container = map.CreateContainer(); }
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); }
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); }
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(); }
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); }
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); }
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); } }
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); }
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(); }
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); }
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); }
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); }
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(); }
private static void RegisterStandard(DependencyMap map) { map.AddSingletonService<ISingleton, Singleton>(); map.AddService<ITransient, Transient>(); map.AddService<ICombined, Combined>(); }
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); }
private static void RegisterStandard(DependencyMap map) { map.AddSingletonService <ISingleton, Singleton>(); map.AddService <ITransient, Transient>(); map.AddService <ICombined, Combined>(); }
public void ShouldBeAbleToRegisterNamedSingletonServicesWithDependencyMapUsingGenerics() { var serviceName = "MyService"; var dependencyMap = new DependencyMap(); dependencyMap.AddSingletonService<IVehicle, Vehicle>(serviceName); Assert.IsTrue(dependencyMap.Contains(typeof(IVehicle), serviceName)); }
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>() })); }
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(); }