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)))); }
/// <summary> /// Compiles the target IOC container. /// </summary> /// <returns>Returns <c>true</c> if the operation succeeded. Otherwise, it will return <c>false</c>.</returns> public override bool Execute() { bool result = true; try { string targetPath = GetTargetPath(TargetAssemblies); targetPath = string.IsNullOrEmpty(targetPath) ? Environment.CurrentDirectory : targetPath; targetPath = Path.GetDirectoryName(targetPath); var targetFiles = Path.GetFileName(TargetAssemblies); var loader = new DependencyMapLoader(); var dependencyMap = loader.LoadFrom(targetPath, targetFiles); var typeName = "MicroContainer" ?? TypeName; var namespaceName = "Hiro.Containers" ?? NamespaceName; var assemblyName = "Hiro.CompiledContainers" ?? AssemblyName; var compiler = new ContainerCompiler(); var compiledAssembly = compiler.Compile(typeName, namespaceName, assemblyName, dependencyMap); Console.WriteLine("Compiling {0}", OutputAssemblyFileName); compiledAssembly.Write(OutputAssemblyFileName); } catch (Exception ex) { result = false; Log.LogError(string.Format("Exception thrown: {0}", ex)); } return(result); }
public void ShouldUseDefaultAnonymousFactoryToCreateTypesIfFactoryTypeIsAvailable() { var loader = new DependencyMapLoader(); var map = loader.LoadFromBaseDirectory("SampleAssembly.dll"); var container = map.CreateContainer(); var result = container.GetInstance <object>(); Assert.AreEqual(42, result); }
public void ShouldLoadAllImplementationsAndInterfaces() { var assembly = typeof(IDBConnection).Assembly; var loader = new DependencyMapLoader(); var map = loader.LoadFrom(assembly); var container = map.CreateContainer(); var testRepo = container.GetInstance <ITestRepo>(); Assert.IsNotNull(testRepo); }
public void ShouldLoadAndCreateContainerFromBaseDirectory() { var loader = new DependencyMapLoader(); var map = loader.LoadFromBaseDirectory("*.UnitTests.dll"); var container = map.CreateContainer(); Assert.IsNotNull(container); var vehicle = container.GetInstance <IVehicle>(); Assert.IsNotNull(vehicle); }
public void ShouldLoadAndCreateContainerFromSingleAssembly() { var loader = new DependencyMapLoader(); var map = loader.LoadFrom(typeof(IPerson).Assembly); var container = map.CreateContainer(); Assert.IsNotNull(container); var vehicle = container.GetInstance <IVehicle>(); Assert.IsNotNull(vehicle); }
public void ShouldBeAbleToAutomaticallyInjectBaseGenericInterfaceTypes() { var loader = new DependencyMapLoader(); var map = loader.LoadFromBaseDirectory("Sample*.dll"); map.AddService <IFoo <int>, SampleGenericImplementation>(); var dependencies = map.Dependencies; var container = map.CreateContainer(); Assert.IsTrue(container.Contains(typeof(IBaz <int>), "SampleGenericImplementation")); Assert.IsTrue(container.Contains(typeof(IFoo <int>), "SampleGenericImplementation")); }
public void ShouldBeAbleToFilterLoadedServicesUsingASinglePredicate() { Predicate <IServiceInfo> serviceFilter = service => service.ServiceType != typeof(IVehicle); var loader = new DependencyMapLoader(); loader.ServiceFilter = serviceFilter; var map = loader.LoadFrom(AppDomain.CurrentDomain.BaseDirectory, "*.UnitTests.dll"); var container = map.CreateContainer(); Assert.IsNotNull(container); // There should be no IVehicle container instances // in this container since the IVehicle service has been filtered // out of the container var vehicle = container.GetInstance <IVehicle>(); Assert.IsNull(vehicle); }
public void ShouldBeAbleToLoadEnumerableServicesByDefault() { var loader = new DependencyMapLoader(); loader.ServiceFilter = info => !string.IsNullOrEmpty(info.ServiceName) && info.ServiceName.StartsWith("Baz") && info.ServiceType == typeof(IBaz <int>) || info.ServiceType == typeof(IFizz); var map = loader.LoadFrom(typeof(IFoo <>).Assembly); var container = map.CreateContainer(); Assert.IsNotNull(container); 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]); }