Factory
Factory copied to clipboard
Add Factory.Create<T>()
Sometimes we want to create a class without an interface so that its dependencies can be resolved. Currently there are a few ways to do this but none is ideal:
// Factory.Create(Type) returns object, so needs to be casted to the correct type
var newObj = (SomeObject)Factory.Create(typeof(SomeObject));
// We can also use the string name, but this can cause issues with refactoring later
// This is less of an issue in C#6 because of 'nameof', but that isn't available in C# 4
var newObj = Factory.Create<SomeObject>("SomeObject");
// Creating from interface works with no arguments, but means we need to implement
// an interface
var newObj = Factory.CreateInterface<ISomeObject>();
In theory, this would work and be nicer to write, but does not exist in the current version of the library:
var newObj = Factory.Create<SomeObject>();
Hmm. I thought it supported that already?!
Yeah doesn't look like it. I'm happy to add it but thought I ought to check here to see if there was a reason it had been left out.