Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TinyIoC: Register multiple interfaces on a single instance

Autofac allows resolving multiple interfaces to the same instance very easily with .AsImplementedInterfaces() or chained .As<>() calls together with .SingleInstance(). Can this also be done with TinyIoC? I've only found how to register multiple implementations of the same interface, but there is no way of chaining registrations or the like.

From my understanding this is a quite important feature for an IoC container, isn't it?

like image 209
TeaDrivenDev Avatar asked Jun 27 '12 00:06

TeaDrivenDev


1 Answers

If I'm understanding correctly you have something like

public class MyThing : IFoo, IBar
{
}

And you want the following to return the same instance as each other:

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

If so, it's possible, but it's a bit ugly:

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

You could probably wrap that into some nicer syntax if you wanted to, but that factory delegate is effectively what will be happening under the hood.

like image 89
Steven Robbins Avatar answered Sep 29 '22 02:09

Steven Robbins