This factory can create new instances of classes or store their references to them.
TinyFactory knows how to inject dependencies through the constructor
Create your a class factory and extend from TinyFactory
:
public class Factory : TinyFactoryService
{
private static Factory _instace;
private static object locker = new object();
private Factory() : base() { }
protected override void ConfigureFactory(IFactoryCollection collection)
{
collection.AddSingleton<IRepository, UserRepository>();
collection.AddTransient<IRepositoryConfig, Config>();
}
public static Factory GetFactory()
{
lock(locker)
{
if (_instace == null) _instace = new Factory();
return _instace;
}
}
public static T Resolve<T>() where T: class => GetFactory().Get<T>();
}
And using this class in your code:
var repo = Factory.Resolve<IRepository>();
See more in demo project