Warm tip: This article is reproduced from serverfault.com, please click

Autofac register IEnumerable and individuals to resolve them all

发布于 2021-10-20 20:30:27

We all know you can register multiple instances and resolve them all with Autofac

builder.RegisterType<Foo1>().As<IFoo>();    
builder.RegisterInstance(new Foo2("something")).As<IFoo>();    
builder.RegisterInstance(new Foo2("another")).As<IFoo>();

//and finally
var foos = ctx.Resolve<IEnumerable<IFoo>>();

Now I had to register some foos as follow:

builder.Register(ctx =>{
   var factory = ctx.Resolve<IFooFactory>();
   var allTheFoos = config.FooConfigs.Select(fooConfig => factory.CreateFoo(fooConfig));
   return allTheFoos;
}).As<IEnumerable<IFoo>>();

This still works, but I think it is not the correct way. In addition to this, I still want to add an additional registration of IFoo

builder.RegisterInstance(new Foo2("this one")).As<IFoo>();    

If I now resolve the IEnumerable, I get the collection explicitly registered as IEnumerable, but I need it to also contain the Foo2("this one");

//contains allTheFoos, but not Foo2("this one");
var foos = ctx.Resolve<IEnumerable<IFoo>>();

The registrations itself can not be merged since they are in different modules for a reason. I think I need to register the foos created with the factory individually, but this need to be inside the Register method (or equivalent) since I needs to be able to first resolve the factory itself. I am hoping on something like:

builder.Register(ctx => /* generate foos*/).AsMultiple<IFoo>();

EDIT: Based upon the answer

public class BotRegistrationSource : IRegistrationSource
{
    private readonly IConfiguration _config;

    public BotRegistrationSource(IConfiguration config)
    {
        _config = config;
    }

    public IEnumerable<IComponentRegistration> RegistrationsFor(Service service,
        Func<Service, IEnumerable<ServiceRegistration>> registrationAccessor)
    {
        var swt = service as IServiceWithType;
        if (!(swt?.ServiceType.IsAssignableTo<IBot>() ?? false))
        {
            return Enumerable.Empty<IComponentRegistration>();
        }

        return  _config.GetSection("Bots")
            .GetChildren()
            .Select(c =>
                new ComponentRegistration(
                    Guid.NewGuid(),
                    new DelegateActivator(swt.ServiceType, (componentContext, _) =>
                    {
                        var botFactory = componentContext.Resolve<IBotFactory>();
                        var config = botFactory.CreateConfig();
                        c.Bind(config);
                        var bot = botFactory.Create(config);
                        bot.Enable();
                        return bot;
                    }),
                    new CurrentScopeLifetime(),
                    InstanceSharing.None,
                    InstanceOwnership.OwnedByLifetimeScope,
                    new[] { service },
                    new Dictionary<string, object>()));
    }

    public bool IsAdapterForIndividualComponents => false;
}
Questioner
Stijn Van Antwerpen
Viewed
0
Travis Illig 2021-10-21 21:43:03

I think what you're going to be looking at is a registration source. Registration sources are a way to do exactly what you want - provide one or more components to the container when a service is requested. The documentation has an example.