c# - Autofac composite pattern -
i noticed quite need implement composite pattern. example:
interface iservice { ... } class service1 : iservice { ... } class service2 : iservice { ... } class compositeservice : iservice { public compositeservice(ienumerable<iservice> services) { ... } ... }
i want register compositeservice iservice in container , have dependencies injected.
(looks similar decorator decorating set of services instead of one)
what's best way in autofac?
how ideal solution (for c#)?
update:
my current registration is:
builder.registertype<service1>().named<iservice>("impl"); builder.registertype<service2>().named<iservice>("impl"); builder.register(c => new compositeservice(c.resolve<ienumerable<iservice>>("impl"))) .as<iservice>();
it similar decorators hand in http://nblumhardt.com/2011/01/decorator-support-in-autofac-2-4
can improved?
i haven't implemented or thought through fully, best syntax achieve is:
builder .registercomposite<iservice>((c, elements) => new compositeservice(elements)) .withelementsnamed("impl");
the elements
parameter registration function of type ienumerable<iservice>
, encapsulate c.resolve<ienumerable<iservice>>("impl")
.
now how write it...
Comments
Post a Comment