Created
November 1, 2013 10:50
-
-
Save RobThree/7263792 to your computer and use it in GitHub Desktop.
An improvement upon http://stackoverflow.com/a/4100126/215042. This factory can be used as a generic factory. Improvements are, amongst others:
• Generics
• Being able to specify case(in)senstivity when resolving instances
• Constructor overloads; the default will assume the Executing Assembly, other assemblies can be passed using an overload of…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IFood | |
{ | |
bool IsTasty { get; } | |
} | |
public class Hamburger : IFood | |
{ | |
public bool IsTasty { get { return true; } } | |
} | |
public class PeaSoup : IFood | |
{ | |
public bool IsTasty { get { return false; } } | |
} | |
public interface IAnimal | |
{ | |
bool IsPet { get; } | |
} | |
public class Cat : IAnimal | |
{ | |
public bool IsPet { get { return true; } } | |
} | |
public class Elephant : IAnimal | |
{ | |
public bool IsPet { get { return false; } } | |
} | |
public interface IVehicle | |
{ | |
bool CanFly { get; } | |
string Location { get; } | |
} | |
public class Boeing747 : IVehicle | |
{ | |
public bool CanFly { get { return true; } } | |
public string Location { get; private set; } | |
public Boeing747(string location) | |
{ | |
this.Location = location; | |
} | |
} | |
public class Beetle : IVehicle | |
{ | |
public bool CanFly { get { return false; } } | |
public string Location { get; private set; } | |
public Beetle(string location) | |
{ | |
this.Location = location; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Reflection; | |
public class ObjectFactory<T> | |
where T : class | |
{ | |
private readonly Dictionary<string, Type> _foundTypes; | |
public ObjectFactory() | |
: this(Assembly.GetExecutingAssembly()) { } | |
public ObjectFactory(params Assembly[] assemblies) | |
: this(StringComparer.OrdinalIgnoreCase, assemblies) { } | |
public ObjectFactory(IEqualityComparer<string> comparer, params Assembly[] assemblies) | |
{ | |
_foundTypes = assemblies.SelectMany(a => a.GetTypes().Where(t => typeof(T).IsAssignableFrom(t) && !t.IsAbstract && !t.IsInterface)) | |
.ToDictionary(t => t.Name, comparer); | |
} | |
public T CreateInstance(string name, params object[] args) | |
{ | |
if (string.IsNullOrEmpty(name)) | |
throw new ArgumentNullException("name"); | |
Type type; | |
if (!_foundTypes.TryGetValue(name, out type)) | |
throw new ArgumentException("Failed to find type named '" + name + "' in ObjectFactory of type '" + typeof(T).Name + "'."); | |
return (T)Activator.CreateInstance(type, args); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var animalfactory = new ObjectFactory<IAnimal>(); | |
var foodfactory = new ObjectFactory<IFood>(); | |
var vehiclefactory = new ObjectFactory<IVehicle>(); | |
var a1 = animalfactory.CreateInstance("cat"); | |
var a2 = animalfactory.CreateInstance("elephant"); | |
var f1 = foodfactory.CreateInstance("peasoup"); | |
var f2 = foodfactory.CreateInstance("hamburger"); | |
var v1 = vehiclefactory.CreateInstance("beetle", "Amsterdam"); | |
var v2 = vehiclefactory.CreateInstance("boeing747", "London"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment