I'm trying to configure Mediatr with Autofac. The documentation shows how to configure it, but I don't understand how the ServiceFactory registration works.
The registration is as follows:
builder.Register<ServiceFactory>(ctx =>
{
var c = ctx.Resolve<IComponentContext>();
return t => c.Resolve(t);
});
And ServiceFactory is a delegate:
/// <summary>
/// Factory method used to resolve all services. For multiple instances, it will resolve against <see cref="IEnumerable{T}" />
/// </summary>
/// <param name="serviceType">Type of service to resolve</param>
/// <returns>An instance of type <paramref name="serviceType" /></returns>
public delegate object ServiceFactory(Type serviceType);
My understanding is that when resolving ServiceFactory, Autofac will resolve the anonymous function:
t=>c.Resolve(t)
but I don't understand why IComponentContext is resolved from ctx, given that ctx is already an IComponentContext.
So what difference would it make to register it this way:
builder.Register<ServiceFactory>(ctx =>
{
return t => ctx.Resolve(t);
});