Autofac composite pattern

核能气质少年 提交于 2019-11-30 14:07:43

I haven't implemented this or even thought it through fully, but the best syntax I could achieve is:

builder
.RegisterComposite<IService>((c, elements) => new CompositeService(elements))
.WithElementsNamed("impl");

The elements parameter to the registration function would be of type IEnumerable<IService> and encapsulate the c.Resolve<IEnumerable<IService>>("impl").

Now how to write it...

You could try named or keyed registrations. A named registration simply gets a string name, to differentiate it from other registrations of the same interface. Similarly, a key uses some value type, for instance an enum, to discriminate between multiple registrations. Your CompositeService would likely be the default reference, registered by type with no other special info needed. You'll need some method to resolve the other IService dependencies and pass them to the constructor; a factory method for CompositeService may work.

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