Table of Contents

Class ImplementationFactory<T1, TBase>

Namespace
Gamelogic.Extensions
Assembly
Gamelogic.Extensions.dll

A factory class for creating instances of types derived from TBase.

[Version(3, 2, 0)]
public class ImplementationFactory<T1, TBase> : IEnumerable<Func<T1, TBase>>, IEnumerable

Type Parameters

T1

The type of argument1 for factory methods.

TBase

The base type that all created instances must derive from.

Inheritance
ImplementationFactory<T1, TBase>
Implements
IEnumerable<Func<T1, TBase>>
Inherited Members
Extension Methods

Examples

One use case is to make it easier to write unit tests for a set of types that share an interface.

using System;
using System.Collections.Generic;

namespace Gamelogic.Extensions.DocumentationCode
{
    #region Fake classes so these tests are not picked up by the actual test running
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
    class TestFixtureAttribute : Attribute
    {
        // ReSharper disable once UnusedParameter.Local
        public TestFixtureAttribute(Type type) { }
    }
    
    public class TestAttribute : Attribute { }
    
    public class SetUpAttribute : Attribute { }

    public class Assert
    {
        public static void That(object a, object b) { }
    }
    
    public class Is
    {
        public static object EqualTo(object a) => null;
    }
    
    #endregion

    #region Documentation_ImplementationFactory_TestExample

    // Non-generic base class for static members
    public class CollectionTests
    {
        // A factory for each type we want to test for. 
        protected static ImplementationFactory<ICollection<int>> Factories = new ImplementationFactory<ICollection<int>>
        {
            () => new List<int>(),
            () => new HashSet<int>()
        };
    }

    // A fixture for each type we want to test for
    [TestFixture(typeof(List<int>))]
    [TestFixture(typeof(HashSet<int>))]
    public class CollectionTests<TCollection> : CollectionTests
        where TCollection : ICollection<int>
    {
        private TCollection collection;

        [SetUp]
        public void Setup() => collection = CreateCollection();

        [Test]
        public void Add_IncreasesCount()
        {
            collection.Add(4);
            Assert.That(collection.Count, Is.EqualTo(1));
        }

        private TCollection CreateCollection() => Factories.GetInstance<TCollection>();
    }
    #endregion
}

Remarks

TBase is often a common interface or abstract type, and the registered instances of different types that implements the base type. This is useful when you want to choose the implementation based on a type parameter.

Methods

Add<TImplementation>(Func<T1, TImplementation>)

Registers a factory method for creating instances of TImplementation.

GetEnumerator()

Returns an enumerator that iterates through the registered factory methods.

GetInstance<TImplementation>(T1)

Gets an instance of TImplementation using the registered factory method.