Составной шаблон Autofac

I noticed I quite often need to implement composite pattern. For example:

interface IService { ... }
class Service1 : IService { ... }
class Service2 : IService { ... }
class CompositeService : IService
{
    public CompositeService(IEnumerable services) { ... }
    ...
}

I want to register CompositeService as IService in container and have dependencies injected.

(looks somewhat similar to Decorator but decorating set of services instead of only one)

What's the best way to do it in autofac?

How would ideal solution look like (for C#)?

Update:

My current registration is:

builder.RegisterType().Named("impl");
builder.RegisterType().Named("impl");

builder.Register(c => new CompositeService(c.Resolve>("impl")))
    .As();

It is similar to Decorators by Hand in http://nblumhardt.com/2011/01/decorator-support-in-autofac-2-4

Can it be improved?

6
задан Konstantin Spirin 24 February 2011 в 03:38
поделиться