Factory icon indicating copy to clipboard operation
Factory copied to clipboard

Add Factory.Create<T>()

Open RoryDungan opened this issue 8 years ago • 2 comments

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>();

RoryDungan avatar Jan 23 '18 04:01 RoryDungan

Hmm. I thought it supported that already?!

ashleydavis avatar Jan 23 '18 06:01 ashleydavis

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.

RoryDungan avatar Jan 23 '18 06:01 RoryDungan