Dynamic aggregate service implementation generation for Autofac via Castle DynamicProxy.
Please file issues and pull requests for this package in this repository rather than in the Autofac core repo.
Once you've added a reference to the Autofac.Extras.AggregateService package, you can start by creating your aggregate service interface. The idea is that, instead of injecting several individual services into a consumer, you have a single aggregate that gets injected, where each property is one of the dependencies:
public interface IMyAggregateService
{
IFirstService FirstService { get; }
ISecondService SecondService { get; }
}Update your consumer to take in the aggregate:
public class SomeController
{
private readonly IMyAggregateService _aggregateService;
public SomeController(IMyAggregateService aggregateService)
{
_aggregateService = aggregateService;
}
}Finally, make sure you register the individual dependencies, the aggregate service interface, and your consumer.
var builder = new ContainerBuilder();
builder.RegisterAggregateService<IMyAggregateService>();
builder.Register(/*...*/).As<IFirstService>();
builder.Register(/*...*/).As<ISecondService>();
builder.RegisterType<SomeController>();
var container = builder.Build();When you resolve the consumer, the aggregate service will be injected and you can use the properties on that. This allows you to add new dependencies to the interface without changing all of your consumers.
Need help with Autofac? We have a documentation site as well as API documentation. We're ready to answer your questions on Stack Overflow or check out the discussion forum.