SimonG96 / LightweightIocContainer

A lightweight IOC Container that is powerful enough to do all the things you need it to do.
MIT License
4 stars 2 forks source link

Allow registration of multiple interfaces for one type #33

Closed SimonG96 closed 4 years ago

SimonG96 commented 4 years ago

Allow the registration of multiple interfaces for one registered type, e.g.:

The interface

public interface IFoo : IBar { }

is implemented by

public class Foo : IFoo { }

Users may want to get a IBar or a IFoo and both times need the implementing type Foo.
The registration should look something like this:

container.Register<IBar, IFoo, Foo>();

For registrations with Lifestyle.Transient this could be done by calling IIocContainer.Register<>() multiple times:

container.Register<IBar, Foo>();
container.Register<IFoo, Foo>();

What can be done for registrations with Lifestyle.Singleton? Every Resolve<>() call has to return the same instance of the implemented type.

SimonG96 commented 4 years ago

For Lifestyle.Singleton:
Add the registered Type to the _singletons-list, and not the interface. This way there can be a check if there is already an instance of the type that has to be resolved.

SimonG96 commented 4 years ago

Add MultipleRegistrations for up to five interfaces: