public void ShouldLoadServicesUsingTheGivenServiceLoaderAndAssemblyLoaderAndServiceResolverInstances() { var serviceLoader = new Mock <IServiceLoader>(); var resolver = new Mock <IDefaultServiceResolver>(); var typeLoader = new Mock <ITypeLoader>(); var assembly = typeof(IPerson).Assembly; var assemblies = new Assembly[] { assembly }; var serviceList = new List <IServiceInfo>(); IEnumerable <IServiceInfo> services = serviceList; var defaultService = new ServiceInfo(typeof(IVehicle), typeof(Vehicle), "Vehicle"); serviceList.Add(new ServiceInfo(typeof(IVehicle), typeof(Truck), "Truck")); serviceList.Add(defaultService); typeLoader.Expect(l => l.LoadTypes(It.IsAny <Assembly>())).Returns(new System.Type[0]); resolver.Expect(r => r.GetDefaultService(It.IsAny <System.Type>(), It.IsAny <IEnumerable <IServiceInfo> >())).Returns(defaultService); serviceLoader.Expect(s => s.Load(assembly)).Returns(services); var loader = new DependencyMapLoader(typeLoader.Object, serviceLoader.Object, resolver.Object); DependencyMap map = loader.LoadFrom(assemblies); typeLoader.VerifyAll(); resolver.VerifyAll(); serviceLoader.VerifyAll(); // Make sure the services are loaded into the dependency map Assert.IsTrue(map.Contains(new Dependency(typeof(IVehicle), "Vehicle"))); Assert.IsTrue(map.Contains(new Dependency(typeof(IVehicle), "Truck"))); Assert.IsTrue(map.Contains(new Dependency(typeof(IVehicle)))); }
public void ShouldBeAbleToRegisterAnonymousServicesWithDependencyMapUsingGenerics() { var dependencyMap = new DependencyMap(); dependencyMap.AddService<IVehicle, Vehicle>(); Assert.IsTrue(dependencyMap.Contains(new Dependency(typeof(IVehicle)))); Assert.IsTrue(dependencyMap.Contains(typeof(IVehicle))); }
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))); }
public void ShouldBeAbleToReturnAllAvailableImplementationsForADependency() { var numberOfImplementations = 5; var implementations = Enumerable.Range(0, numberOfImplementations) .Select(_ => A.Fake <IImplementation>()).ToArray(); var dependency = new Dependency(typeof(IEnumerable <string>)); var map = new DependencyMap(); for (var i = 0; i < numberOfImplementations; i++) { var implementation = implementations[i]; map.AddService(dependency, implementation); } Assert.True(map.Contains(dependency)); Assert.Contains(dependency, map.Dependencies); var results = map.GetImplementations(dependency, false).ToArray(); for (var i = 0; i < numberOfImplementations; i++) { Assert.Contains(implementations[i], results); } }
public void ShouldBeAbleToAddMultipleServices() { var numberOfDependencies = 5; var implementations = Enumerable.Range(0, numberOfDependencies) .Select(_ => A.Fake <IImplementation>()).ToArray(); var dependencies = Enumerable.Range(0, numberOfDependencies) .Select(_ => A.Fake <IDependency>()).ToArray(); var map = new DependencyMap(); for (var i = 0; i < numberOfDependencies; i++) { var dependency = dependencies[i]; var implementation = implementations[i]; map.AddService(dependency, implementation); } for (var i = 0; i < numberOfDependencies; i++) { var dependency = dependencies[i]; Assert.True(map.Contains(dependency)); Assert.Contains(dependency, map.Dependencies); } }
public void ShouldBeAbleToRegisterNamedServicesWithDependencyMapUsingGenerics() { var serviceName = "MyService"; var dependencyMap = new DependencyMap(); dependencyMap.AddService <IVehicle, Vehicle>(serviceName); Assert.IsTrue(dependencyMap.Contains(typeof(IVehicle), serviceName)); }
public void ShouldBeAbleToAddItemsToDependencyMap() { var ctor = typeof(Vehicle).GetConstructor(new Type[0]); var dependency = new Dependency(typeof(IVehicle), string.Empty); var constructorImplementation = new ConstructorCall(ctor); var dependencyMap = new DependencyMap(); dependencyMap.AddService(dependency, constructorImplementation); Assert.IsTrue(dependencyMap.Contains(dependency)); }
public void ShouldLoadSampleModuleFromGivenDirectory() { var map = new DependencyMap(); var loader = new ModuleLoader(map); var targetDirectory = Path.GetDirectoryName(typeof(SampleModule).Assembly.Location); loader.LoadModulesFrom(targetDirectory, "*.dll"); Assert.IsTrue(map.Contains(typeof(IList <int>))); }
public void ShouldBeAbleToAddItemsToDependencyMap() { var ctor = typeof(Vehicle).GetConstructor(new Type[0]); var dependency = new Dependency(typeof(IVehicle), string.Empty); var constructorImplementation = new ConstructorCall(ctor); var dependencyMap = new DependencyMap(); dependencyMap.AddService(dependency, constructorImplementation); Assert.IsTrue(dependencyMap.Contains(dependency)); }
public void ShouldLoadSampleModule() { var map = new DependencyMap(); var sampleModule = new SampleModule(); var loader = new ModuleLoader(map); loader.LoadModule(sampleModule); Assert.IsTrue(sampleModule.Invoked); Assert.IsTrue(map.Contains(typeof(IList <int>))); }
/// <summary> /// Adds a named service to the dependency map. /// </summary> /// <remarks>This service will be created once per web session.</remarks> /// <param name="map">The target dependency map.</param> /// <param name="serviceName">The service name.</param> /// <param name="serviceType">The service type.</param> /// <param name="implementingType">The implementing type.</param> public static void AddPerSessionService(this DependencyMap map, string serviceName, System.Type serviceType, System.Type implementingType) { // Add the SessionCache by default if (!map.Contains(typeof(ICache))) { map.AddService <ICache, SessionCache>(); } // Add the HttpReferenceTracker by default if (!map.Contains(typeof(IHttpReferenceTracker))) { map.AddService <IHttpReferenceTracker, HttpReferenceTracker>(); } // The cached instantiation class will use the cache in the container // to cache service instances var dependency = new Dependency(serviceType, serviceName); var implementation = new CachedInstantiation(new TransientType(implementingType, map, new ConstructorResolver())); map.AddService(dependency, implementation); }
public void ShouldBeAbleToReturnAllAvailableImplementationsForADependencyExceptForTheIncompleteDependencies() { var numberOfImplementations = 5; var numberOfIncompleteImplementations = 3; IEnumerable <IImplementation> CreateImplementations(int numberOfImplementationsToCreate, bool isCompleted) { var incompleteDependencies = isCompleted ? Enumerable.Empty <IDependency>() : new[] { A.Fake <IDependency>() }; return(Enumerable.Range(0, numberOfImplementationsToCreate) .Select(_ => { var currentImplementation = A.Fake <IImplementation>(); A.CallTo(() => currentImplementation.GetMissingDependencies(A <IDependencyMap> .Ignored)) .Returns(incompleteDependencies); return currentImplementation; }).ToArray()); } var implementations = new List <IImplementation>(); implementations.AddRange(CreateImplementations(numberOfImplementations, true)); var incompleteImplementations = CreateImplementations(numberOfIncompleteImplementations, false) .ToArray(); implementations.AddRange(incompleteImplementations); var dependency = new Dependency(typeof(IEnumerable <string>)); var map = new DependencyMap(); foreach (var implementation in implementations) { map.AddService(dependency, implementation); } Assert.True(map.Contains(dependency)); Assert.Contains(dependency, map.Dependencies); var results = map.GetImplementations(dependency, false).ToArray(); for (var i = 0; i < numberOfImplementations; i++) { Assert.Contains(implementations[i], results); } foreach (var implementation in incompleteImplementations) { Assert.DoesNotContain(implementation, results); } }
public void ShouldBeAbleToAddService() { var dependency = new Dependency(typeof(IList <string>)); var implementation = A.Fake <IImplementation>(); var map = new DependencyMap(); map.AddService(dependency, implementation); Assert.True(map.Contains(dependency)); Assert.Contains(dependency, map.Dependencies); }
public void ShouldBeAbleToRegisterNamedServicesWithDependencyMap() { var serviceName = "MyService"; var dependencyMap = new DependencyMap(); dependencyMap.AddService<IVehicle, Vehicle>(serviceName); Assert.IsTrue(dependencyMap.Contains(typeof(IVehicle), serviceName)); }
// enumerate /// <summary> /// Determines whether this instance has registered. /// </summary> /// <typeparam name="TService">The type of the service.</typeparam> /// <returns> /// <c>true</c> if this instance has registered; otherwise, <c>false</c>. /// </returns> public bool HasRegistered <TService>() { return(_builder.Contains(typeof(TService))); }