Skip to content

gkurbesov/TinyFactory

Repository files navigation

TinyFactory

Small and simple factory. TinyFactory is ready to store and instantiate simple classes.

This factory can create new instances of classes or store their references to them.

TinyFactory knows how to inject dependencies through the constructor

Build status Nuget

Quick start

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

About

Small and simple DI container for C#

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages