Skip to content

Abstract factories

Simon Gockner edited this page Jul 8, 2019 · 1 revision

Abstract factories are interfaces, that are implemented by the LightweightIocContainer. Using these factories is the recommended way to resolve any instances from the IocContainer.

public interface IFooFactory
{
    // This is the main structure of every abstract factory
}

Create() methods

Every abstract factory has to have a Create() method with the return value you want to resolve:

IFoo Create();

If the constructor of the type you want to resolve has any arguments that can't be resolved by the IocContainer, you'll have to pass them as arguments to your Create() method:

IFoo Create(string name);

Internally the Create() methods call IocContainer.Resolve<>():

IFoo foo = container.Resolve<IFoo>(name);

ClearMultitonInstance<>() methods

There is the possibility to add a method to the abstract factory that allows to clear Multiton instances. This method has to be a generic method to specify the type that is registered as a Multiton and whose instances should be deleted:

void ClearMultitonInstance<IBar>();

This method for example would clear all created Multiton instances of type IBar.

Internally the ClearMultitonInstance<>() methods call IocContainer.ClearMultitonInstances<>():

container.ClearMultitonInstances<IBar>();