LightFactory is one of the simplest in use, fastest and most flexible Dependency Injection frameworks!

LightFactory is extremely fast!

To invoke your object-generator delegate LightFactory used only 1 method call and check only 1 if-condition. Its make LightFactory one of the most high-performance Dependency Injection framework ever!

It's very simple to use

You can provide dependency injection using just 4 methods and their overloads.

Next example demonstrate how is simple to use LightFactory:
public class Program
{
    /// <summary>
    /// STEP 1: Create you own factory which will inherit FactoryBase and provide type registration in InitTypes method.
    /// </summary>
    class SamplerFactory : FactoryBase
    {
        /// <summary>
        /// STEP 2: Register new instance of you factory class with FactoryBase.RegisterFactory(...) call
        /// </summary>
        public override void InitFactory()
        {
            Define<ICollection<int>, int[]>(arr => new List<int>(arr));
        }
    }

    public static void Main()
    {
        /// <summary>
        /// STEP 3: Register SampleFactory instance before use it
        /// </summary>
        FactoryBase.RegisterFactory(new SamplerFactory());

        /// <summary>
        /// STEP 4: Use Factory<T>.Create() method and its overloads to create new instances.
        /// </summary>
        ICollection colletion = Factory<ICollection>.Create(new int[] { 1, 2, 3 });
    }
}

It's flexible and compact

It's give you ability to set any instantiation logic which you need.
Also its produce small amount of hidden generic-implementation members

New in version 1.2

Type mapping with attributes

With new MetaBaseFactory you can define types substitutes via Overrides attribute, i.e.:

public interface IPerson
{
	string Name { get; }
}

// map IPerson to Person
[Overrides(typeof(IPerson))]
class Person: IPerson
{
	public string Name { get { return "Smith"; } }
}

public class Program
{
	static void Main(string[] args)
	{
		FactoryBase.RegisterFactory(new MetaBasedFactory());

		var person = Factory<IPerson>.Create();
		Console.WriteLine(person.Name);   //output: Smith
	}
}

How to get exception on trying instantiate unregistered type

Since v1.2 you can choose how will LightFactory handle objects creation of undefined types. Common option is to return default(T) in Factory.Create<>() method. But you can add reference to LightFactory.dll from "Throwable" folder(from binaries) and it will generate exception.

LightFactory is available on NuGet

Last edited Feb 22, 2012 at 3:07 PM by seoriy, version 14