rid00z/FreshMvvm

[IoC] Constructor Injection: IEnumerable of services

Closed this issue · 1 comments

It is not possible at the moment to request all registered services of one type. At least during my tests. Although according to TinyIoc this is possible.

Want I want to do:

// In the container initialization
FreshIOC.Container.Register<IModule, ImplementationOne>();
FreshIOC.Container.Register<IModule, ImplementationTwo>();
FreshIOC.Container.Register<IModule, ImplementationThree>();
FreshIOC.Container.Register<IService, SomeService>();

// The class 
public class SomeService
{
    public SomeService(IEnumerable<IModule> modules)
    {
        // modules contains instances of ImplementationOne, ImplementationTwo and ImplementationThree
    }
    // ...
}

You can use RegisterMultiple (see TinyIoC wiki).

Multiple implementations of the same baseclass/interface can be registered using RegisterMultiple. This can be useful if you have a marker interface/shared base class and you want to register multiple implementations to retrieve later using ResolveAll or using an IEnumerable dependency:

container.RegisterMultiple<IMyMarkerInterface>(new[] { typeof(MyImplementationClass1), typeof(MyImplementationClass2) });

But you must use the underlying container, IFreshIOC does not give you access to all features of TinyIoC.

FreshTinyIoCContainer.Current.RegisterMultiple<>()