public static void Run() { IImplementation concreteImplementation = new ConcreteImplementation(); Abstraction abstraction = new Abstraction(concreteImplementation); abstraction.Feature1(); }
/// <summary> /// Adds an object to the end of the collection. /// </summary> /// <typeparam name="T">The Type to specify as the interface type in the new DependencyRegistration object to add.</typeparam> /// <param name="concreteImplementation">The ConcreteImplementation instance that represents the concrete implementation that implements the type of interface specified by the generic T type parameter.</param> public void Add <T>(ConcreteImplementation concreteImplementation) { base.Add(new DependencyRegistration(typeof(T), new List <ConcreteImplementation>() { concreteImplementation })); }
public void TestBridge() { IImplementation implementer = new ConcreteImplementation(); var abstraction = new RefinedAbstraction(implementer); var result = abstraction.Operation(); Assert.AreEqual(result, "Test"); }
/// <summary> /// Registers all types found in the assembly of the Type specified by the generic type parameter T where matches of implemented interfaces are found. /// </summary> /// <typeparam name="T">A type from the assembly to register all types with found implemented interfaces from.</typeparam> public void RegisterAllConcreteImplementationsInAssemblyOf <T>() where T : class { Assembly assembly = typeof(T).Assembly; IEnumerable <Type> concreteImplementations = assembly.GetTypes().Where(t => t.IsClass && t.GetInterfaces().Count() > 0); foreach (Type concreteImplementationType in concreteImplementations) { IEnumerable <Type> interfaceTypes = concreteImplementationType.GetInterfaces(); foreach (Type interfaceType in interfaceTypes) { ConcreteImplementation concreteImplementation = new ConcreteImplementation(concreteImplementationType, null); if (concreteImplementations != null && concreteImplementations.Any()) { registeredDependencies.Add(interfaceType, concreteImplementation); } } } }
/// <summary> /// Returns the concrete implementation of the interface type represented by the generic type parameter T. /// </summary> /// <typeparam name="T">The type of object to instantiate and return.</typeparam> /// <returns>The type of object represented by the generic type parameter T.</returns> public T Resolve <T>() where T : class { DependencyRegistration registration = registeredDependencies.GetRegistration <T>(); if (registration == null) { return(null); } ConcreteImplementation concreteImplementation = registration.ConcreteImplementations.First(); if (concreteImplementation.ConstructorParameters == null) { return(Activator.CreateInstance(concreteImplementation.Type) as T); } else { return(Activator.CreateInstance(concreteImplementation.Type, concreteImplementation.ConstructorParameters) as T); } }