factoryfactory

Yet another IOC container for .NET, designed from the ground up to support the ASP.NET Core DI abstractions.

Introduction

FactoryFactory is a new IOC container, intended either as a drop-in replacement for the default container in ASP.NET Core, or as a standalone container for use in other projects.

Getting started

To get started, add FactoryFactory to your application from NuGet:

Install-Package FactoryFactory -Pre

A basic “Hello World” application might look something like this:

using System;
using FactoryFactory;
using Microsoft.Extensions.DependencyInjection;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var container = Configuration.CreateContainer(m => {
                // Define your services here like this:
                m.Define<IClock>().As<Clock>().Singleton();
            }))
            {
                // Program will resolve to itself implicitly.
                container.GetService<Program>().Run();
            }
        }

        private readonly IClock _clock;

        public Program(IClock clock)
        {
            _clock = clock;
        }

        public void Run()
        {
            Console.WriteLine($"Hello world, the time is {_clock.UtcNow}");
            Console.ReadLine();
        }
    }

    public interface IClock
    {
        DateTime UtcNow { get; }
    }

    public class Clock: IClock
    {
        public DateTime UtcNow => DateTime.UtcNow;
    }
}

There are more complex approaches that involve separately creating modules and a Configuration instance, but for many scenarios, the above simple approach should suffice.


Project maintained by jammycakes • Hosted on GitHub Pages — Theme by mattgraham (with modifications)