TinyIoC: Register multiple interfaces on a single instance

给你一囗甜甜゛ 提交于 2020-01-02 01:30:08

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/11217884/tinyioc-register-multiple-interfaces-on-a-single-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!