public void ShouldBeAbleToCreateTheSameContainerTypeFromASingleDependencyMap() { var map = new DependencyMap(); map.AddService(typeof(IPerson), typeof(Person)); var firstContainer = map.CreateContainer(); var secondContainer = map.CreateContainer(); Assert.AreEqual(firstContainer.GetType(), secondContainer.GetType()); }
public override void PrepareBasic() { var map = new DependencyMap(); RegisterBasic(map); this.container = map.CreateContainer(); }
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); } }
/// <summary> /// Initializes a new instance of the <see cref="HiroServiceRegistrar"/> class. /// </summary> /// <param name="parent">The parent.</param> /// <param name="builder">The builder.</param> /// <param name="containerBuilder">The container builder.</param> public HiroServiceRegistrar(HiroServiceLocator parent, DependencyMap builder, out Func <IMicroContainer> containerBuilder) { _parent = parent; _builder = builder; containerBuilder = (() => _container = _builder.CreateContainer()); LifetimeForRegisters = ServiceRegistrarLifetime.Transient; }
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 ShouldBeAbleToAddDeferredServiceToContainer() { var map = new DependencyMap(); map.AddDeferredService(typeof(IPerson)); map.Injector = new PropertyInjector(); map.AddService(typeof(Truck), typeof(Truck)); var mockPerson = new Mock <IPerson>(); var container = map.CreateContainer(); container.AddService(mockPerson.Object); // The container must instantiate the mock person type var person = container.GetInstance <IPerson>(); Assert.AreSame(mockPerson.Object, person); Assert.IsNotNull(person); // Make sure the person instance is injected into // the target property var truck = container.GetInstance <Truck>(); Assert.IsNotNull(truck); Assert.AreSame(truck.Driver, mockPerson.Object); }
public override void Prepare() { var map = new DependencyMap(); RegisterBasic(map); RegisterPropertyInjection(map); this.container = map.CreateContainer(); }
public void ShouldBeAbleToInstantiateGenericTypes() { var map = new DependencyMap(); map.AddService(typeof(IList<>), typeof(List<>)); var container = map.CreateContainer(); var list = container.GetInstance<IList<int>>(); Assert.IsNotNull(list); }
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 override void Prepare() { var map = new DependencyMap(); RegisterDummies(map); RegisterStandard(map); RegisterComplex(map); 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 ShouldBeAbleToUseGenericsToGetAnonymousServiceInstances() { var map = new DependencyMap(); map.AddService(typeof(IPerson), typeof(Person)); var container = map.CreateContainer(); IPerson person = container.GetInstance<IPerson>(); Assert.IsNotNull(person); }
public void ShouldCallContainerPluginOnceContainerIsInstantiated() { var map = new DependencyMap(); map.AddService <IContainerPlugin, SamplePlugin>(); var container = map.CreateContainer(); Assert.IsTrue(SamplePlugin.HasBeenCalled); }
public void ShouldNotCauseStackOverflowExceptionWhenCallingContainsOnTheNextContainerAndNextContainerIsSelf() { var map = new DependencyMap(); var container = map.CreateContainer(); container.NextContainer = container; var result = container.Contains(typeof(int), "abcdefg"); Assert.IsFalse(result); }
public void ShouldBeAbleToUseGenericsToGetAnonymousServiceInstances() { var map = new DependencyMap(); map.AddService(typeof(IPerson), typeof(Person)); var container = map.CreateContainer(); IPerson person = container.GetInstance <IPerson>(); Assert.IsNotNull(person); }
public void ShouldBeAbleToInstantiateGenericTypes() { var map = new DependencyMap(); map.AddService(typeof(IList <>), typeof(List <>)); var container = map.CreateContainer(); var list = container.GetInstance <IList <int> >(); Assert.IsNotNull(list); }
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 void ShouldNotCauseStackOverflowExceptionWhenCallingGetAllInstancesOnTheNextContainerAndNextContainerIsSelf() { var map = new DependencyMap(); var container = map.CreateContainer(); container.NextContainer = container; var result = container.GetAllInstances(typeof(int)); var resultList = new List <object>(result); Assert.IsTrue(resultList.Count == 0); }
public void ShouldCallInitializeOnTransientTypeThatImplementsIInitializeWhenCallingGetAllInstances() { var map = new DependencyMap(); map.AddService <IInitialize, SampleInitialize>(); var container = map.CreateContainer(); var result = (SampleInitialize)container.GetAllInstances(typeof(IInitialize)).Cast <IInitialize>().First(); Assert.AreSame(container, result.Container); Assert.IsTrue(result.NumberOfTimesInitialized == 1); }
public void ShouldBeAbleToCompileContainerFromDependencyMap() { var map = new DependencyMap(); map.AddService(typeof(IPerson), typeof(Person)); var container = map.CreateContainer(); var result = container.GetInstance(typeof(IPerson), null); Assert.IsNotNull(result); Assert.IsTrue(result is Person); }
public void ShouldBeAbleToAssignTheNextContainerToACompiledContainer() { var map = new DependencyMap(); var container = map.CreateContainer(); Assert.IsNotNull(container); var mockContainer = new Mock <IMicroContainer>(); container.NextContainer = mockContainer.Object; Assert.AreSame(container.NextContainer, mockContainer.Object); }
public void ShouldBeAbleToAddTypedFunctorToDependencyMap() { var expectedInstance = new Foo(); Func<IMicroContainer, IFoo> functor = c => expectedInstance; var map = new DependencyMap(); map.AddService(functor); var container = map.CreateContainer(); var foo = container.GetInstance<IFoo>(); Assert.AreSame(expectedInstance, foo); }
public void ShouldIntroduceContainerInstanceToTypesThatHaveTheMicroContainerDependencyInTheirConstructors() { var map = new DependencyMap(); map.AddService(typeof(SampleContainerAwareType), typeof(SampleContainerAwareType)); var container = map.CreateContainer(); var instance = container.GetInstance <SampleContainerAwareType>(); Assert.IsNotNull(instance); Assert.IsNotNull(instance.Container); Assert.AreSame(container, instance.Container); }
public void ShouldThrowServiceNotFoundExceptionIfDeferredServiceIsNotAvailableAtRuntime() { var map = new DependencyMap(); map.AddDeferredService(typeof(IPerson)); var container = map.CreateContainer(); // The exception should be thrown on this line of code var person = container.GetInstance <IPerson>(); // This line of code should never be executed person.ToString(); }
public void ShouldBeAbleToAddExistingServiceInstanceToContainer() { var map = new DependencyMap(); var person = new Person(); // Create a blank container var container = map.CreateContainer(); container.AddService<IPerson>(null, person); // The container should return the added instance var result = container.GetInstance<IPerson>(); Assert.AreSame(person, result); }
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 ShouldBeAbleToCombineDependencyMaps() { var firstMap = new DependencyMap(); var secondMap = new DependencyMap(); firstMap.AddService(typeof(IPerson), typeof(Person)); secondMap.AddService(typeof(IVehicle), typeof(Vehicle)); DependencyMap combinedMap = firstMap + secondMap; var container = combinedMap.CreateContainer(); Assert.IsNotNull(container.GetInstance <IVehicle>()); Assert.IsNotNull(container.GetInstance <IPerson>()); }
public void ShouldBeAbleToAddTypedFunctorToDependencyMap() { var expectedInstance = new Foo(); Func <IMicroContainer, IFoo> functor = c => expectedInstance; var map = new DependencyMap(); map.AddService(functor); var container = map.CreateContainer(); var foo = container.GetInstance <IFoo>(); Assert.AreSame(expectedInstance, foo); }
public void ShouldBeAbleToAddNamedUntypedFunctorToDependencyMap() { var expectedInstance = new Foo(); Func<IMicroContainer, object> functor = c => expectedInstance; var serviceType = typeof(IFoo); var map = new DependencyMap(); map.AddService("myFoo", serviceType, functor); var container = map.CreateContainer(); var foo = container.GetInstance<IFoo>("myFoo"); Assert.IsNotNull(foo); Assert.AreSame(expectedInstance, foo); }
public void ShouldUseFirstNamedServiceInstanceIfNoDefaultServiceIsAvailable() { var map = new DependencyMap(); map.AddService <Garage, Garage>(); map.AddService <IVehicle, Truck>("Truck"); var container = map.CreateContainer(); var garage = container.GetInstance <Garage>(); Assert.IsNotNull(garage.Vehicle); Assert.IsInstanceOfType(typeof(Truck), garage.Vehicle); }
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 void ShouldBeAbleToInstantiateNamedGenericType() { var map = new DependencyMap(); map.AddService("List", typeof(IEnumerable<>), typeof(List<>)); map.AddService("Queue", typeof(IEnumerable<>), typeof(Queue<>)); var container = map.CreateContainer(); var list = container.GetInstance<IEnumerable<int>>("List"); Assert.IsNotNull(list); var queue = container.GetInstance<IEnumerable<int>>("Queue"); Assert.IsNotNull(queue); Assert.IsFalse(queue.GetType() != list.GetType()); }
public void ShouldInjectPropertyIfDependencyMapHasAPropertyInjectorAssignedToTheInjectorProperty() { var map = new DependencyMap(); map.Injector = new PropertyInjector(); map.AddService<IVehicle, Vehicle>(); map.AddService<IPerson, Person>(); var container = map.CreateContainer(); var result = (IVehicle)container.GetInstance(typeof(IVehicle), null); Assert.IsNotNull(result); Assert.IsNotNull(result.Driver); Assert.IsTrue(result.Driver is Person); }
public void ShouldBeAbleToAddExistingServiceInstanceToContainer() { var map = new DependencyMap(); var person = new Person(); // Create a blank container var container = map.CreateContainer(); container.AddService <IPerson>(null, person); // The container should return the added instance var result = container.GetInstance <IPerson>(); Assert.AreSame(person, result); }
public void ShouldUseConstructorParameterNameToInjectNamedServiceInstanceIfTheNamedServiceExists() { var map = new DependencyMap(); map.AddService <IVehicle, Vehicle>("Vehicle"); map.AddService <IVehicle, Truck>("Truck"); map.AddService <Garage, Garage>(); var container = map.CreateContainer(); var garage = container.GetInstance <Garage>(); Assert.IsNotNull(garage.Vehicle); Assert.IsInstanceOfType(typeof(Vehicle), garage.Vehicle); }
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 ShouldBeAbleToAddNamedUntypedFunctorToDependencyMap() { var expectedInstance = new Foo(); Func <IMicroContainer, object> functor = c => expectedInstance; var serviceType = typeof(IFoo); var map = new DependencyMap(); map.AddService("myFoo", serviceType, functor); var container = map.CreateContainer(); var foo = container.GetInstance <IFoo>("myFoo"); Assert.IsNotNull(foo); Assert.AreSame(expectedInstance, foo); }
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 ShouldInjectPropertyIfDependencyMapHasAPropertyInjectorAssignedToTheInjectorProperty() { var map = new DependencyMap(); map.Injector = new PropertyInjector(); map.AddService <IVehicle, Vehicle>(); map.AddService <IPerson, Person>(); var container = map.CreateContainer(); var result = (IVehicle)container.GetInstance(typeof(IVehicle), null); Assert.IsNotNull(result); Assert.IsNotNull(result.Driver); Assert.IsTrue(result.Driver is Person); }
public void ShouldInjectDefaultServiceImplementationIntoTargetProperty() { var map = new DependencyMap(); var dependency = new Dependency(typeof(IVehicle)); var injector = new PropertyInjectionCall(new TransientType(typeof(Vehicle), map, new ConstructorResolver())); map.AddService(dependency, injector); map.AddService(typeof(IPerson), typeof(Person)); var container = map.CreateContainer(); var result = (IVehicle)container.GetInstance(typeof(IVehicle), null); Assert.IsNotNull(result); Assert.IsNotNull(result.Driver); Assert.IsTrue(result.Driver is Person); }
public void ShouldBeAbleToInstantiateNamedGenericType() { var map = new DependencyMap(); map.AddService("List", typeof(IEnumerable <>), typeof(List <>)); map.AddService("Queue", typeof(IEnumerable <>), typeof(Queue <>)); var container = map.CreateContainer(); var list = container.GetInstance <IEnumerable <int> >("List"); Assert.IsNotNull(list); var queue = container.GetInstance <IEnumerable <int> >("Queue"); Assert.IsNotNull(queue); Assert.IsFalse(queue.GetType() != list.GetType()); }
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 ShouldBeAbleToGetAllEnumerableInstancesOfAGivenService() { var map = new DependencyMap(); map.AddService("Baz1", typeof(IBaz<int>), typeof(Baz1)); map.AddService("Baz2", typeof(IBaz<int>), typeof(Baz2)); map.AddService("Baz3", typeof(IBaz<int>), typeof(Baz3)); map.AddService(typeof(IFizz), typeof(SampleClassWithEnumerableBazDependency)); // Make the IEnumerable<IBazz<int>> service explictly resolvable map.AddAsEnumerableService(typeof(IBaz<int>)); var container = map.CreateContainer(); var fizz = container.GetInstance<IFizz>(); Assert.IsNotNull(fizz); Assert.AreEqual(3, fizz.Services.Count()); var services = fizz.Services.ToArray(); Assert.IsInstanceOfType(typeof(Baz1), services[0]); Assert.IsInstanceOfType(typeof(Baz2), services[1]); Assert.IsInstanceOfType(typeof(Baz3), services[2]); }
public void ShouldBeAbleToAddDeferredServiceToContainer() { var map = new DependencyMap(); map.AddDeferredService(typeof(IPerson)); map.Injector = new PropertyInjector(); map.AddService(typeof(Truck), typeof(Truck)); var mockPerson = new Mock<IPerson>(); var container = map.CreateContainer(); container.AddService(mockPerson.Object); // The container must instantiate the mock person type var person = container.GetInstance<IPerson>(); Assert.AreSame(mockPerson.Object, person); Assert.IsNotNull(person); // Make sure the person instance is injected into // the target property var truck = container.GetInstance<Truck>(); Assert.IsNotNull(truck); Assert.AreSame(truck.Driver, mockPerson.Object); }
public void ShouldThrowServiceNotFoundExceptionIfDeferredServiceIsNotAvailableAtRuntime() { var map = new DependencyMap(); map.AddDeferredService(typeof(IPerson)); var container = map.CreateContainer(); // The exception should be thrown on this line of code var person = container.GetInstance<IPerson>(); // This line of code should never be executed person.ToString(); }
public void should_create_either_both_empty_or_both_equivalent_and_not_null() { var map1 = new DependencyMapLoader().LoadFrom(typeof(ISomeService).Assembly); var container1 = map1.CreateContainer(); var service1 = container1.GetInstance<ISomeService>(); var map2 = new DependencyMap(); map2.AddService<ISomeService,SomeService>(); var container2 = map2.CreateContainer(); var service2 = container2.GetInstance<ISomeService>(); Assert.IsFalse(service2==null||service1==null,"At least one service is null 1:{0} 2:{1}",service1,service2); Assert.AreEqual(service1.GetType(),service2.GetType(),"types are not the same"); }
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(); }
/// <summary> /// Attempts to get an instance of the given <paramref name="serviceType"/>. /// </summary> /// <param name="serviceType">The service type.</param> /// <param name="key">The service name.</param> /// <returns>An object instance that matches the given service type. This method will return null if that service isn't available.</returns> public object GetInstance(Type serviceType, string key) { if (!Contains(serviceType, key)) return null; // Compile the container that will be used // to satisfy the dependencies for the service type if (_baseContainer == null) BuildBaseContainer(); // Use the existing container, if possible if (!_containerMap.ContainsKey(serviceType)) { // Create a new container that constructs the service type // and redirects all other calls to the base container var map = new DependencyMap(); var availableDependencies = _serviceMap.Keys; foreach (var dependency in availableDependencies) { // The base container will handle all the other dependencies map.AddDeferredService(dependency.ServiceName, dependency.ServiceType); } // Add the service type itself var arguments = serviceType.GetGenericArguments(); var concreteType = _genericTypeImplementation.MakeGenericType(arguments); Register(serviceType, concreteType, map); // Compile the container var container = map.CreateContainer(); // Defer the other calls to the base container container.NextContainer = _baseContainer; _containerMap[serviceType] = container; } var result = _containerMap[serviceType].GetInstance(serviceType, key); if (result == null && NextContainer != this && NextContainer != null) return NextContainer.GetInstance(serviceType, key); return result; }
private void BuildBaseContainer() { _serviceMap = _dependencyContainer.GetServiceMap(); var map = new DependencyMap(); foreach (var dependency in _serviceMap.Keys) { map.AddService(dependency, _serviceMap[dependency]); } _baseContainer = map.CreateContainer(); }