Skip to content

Conversation

sauravbhattacharya001
Copy link
Contributor

  • Remove the Autofac ContainerBuilder configuration
  • Set up DI in the Program.cs top-level file
  • Clean up Autofac in packages, documentation, and other helper classes.

@sauravbhattacharya001
Copy link
Contributor Author

sauravbhattacharya001 commented Mar 2, 2024

Fixes #694

Co-authored-by: Kyle McMaster <KyleMcMaster@users.noreply.github.com>
Copy link

@KamRon-67 KamRon-67 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My way is not the only way, our main goal is to keep the Program.cs file clean.

Parts that could grow should have their own section.

}
}

builder.Services.AddScoped(typeof(IRepository<>), typeof(EfRepository<>));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should clean this up.

I wrote a blog post on this topic.

In Program.cs builder is in scope wherever you use it.

Comment on lines +62 to +86
var mediatrOpenTypes = new[]
{
containerBuilder.RegisterModule(new DefaultCoreModule());
containerBuilder.RegisterModule(new AutofacInfrastructureModule(builder.Environment.IsDevelopment()));
});
typeof(IRequestHandler<,>),
typeof(IRequestExceptionHandler<,,>),
typeof(IRequestExceptionAction<,>),
typeof(INotificationHandler<>),
};

foreach (var assembly in assemblies)
{
foreach (var openInterfaceType in mediatrOpenTypes)
{
if (assembly != null)
{
assembly.GetTypes()
.Where(t => t.IsClass && !t.IsAbstract && t.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == openInterfaceType))
.ToList()
.ForEach(implementationType =>
{
var serviceType = implementationType.GetInterfaces().First(i => i.IsGenericType && i.GetGenericTypeDefinition() == openInterfaceType);
builder.Services.AddScoped(serviceType, implementationType);
});
}
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this loop be removed in favor of using MediatR's own DI registration method?

builder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssemblies(assemblies));

@ardalis ardalis merged commit 94305da into ardalis:main Mar 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants