TinyIoC: register multiple interfaces in one instance

Autofac makes it easy to resolve multiple interfaces for the same instance using .AsImplementedInterfaces () or .As <> () chains calls along with .SingleInstance (). Can this also be done with TinyIoC? I just found how to register multiple implementations of the same interface, but there is no way to link registrations, etc.

As far as I understand, this is a pretty important feature for an IoC container, right?

+7
source share
1 answer

If I understand correctly, you have something like

public class MyThing : IFoo, IBar { } 

And you want the following to return the same instance:

 Resolve<IFoo>(); Resolve<IBar>(); 

If yes, maybe, but it's a little ugly:

 container.Register<IFoo, MyThing>(); container.Register<IBar>((c,p) => c.Resolve<IFoo>() as IBar); 

Perhaps you could wrap this in some prettier syntax if you want, but the factory delegate is what will happen under the hood.

+9
source

All Articles