-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Migrate Autofac DI -> Native DI in Asp.Net Core #701
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cf20b0f
ASP.NET Core Native DI - init
sauravbhattacharya001 cd69ad8
Fix assembly interface mapping
sauravbhattacharya001 64768fd
merge
sauravbhattacharya001 79f0aee
fix DI to match tests
sauravbhattacharya001 6ee76d1
fix whitespace
sauravbhattacharya001 918d08c
clean autofac
sauravbhattacharya001 22b8108
Merge branch 'main' into main
sauravbhattacharya001 d9f79e0
Update README.md as per CR
sauravbhattacharya001 e22f638
Merge branch 'main' into main
ardalis File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
134 changes: 0 additions & 134 deletions
134
src/Clean.Architecture.Infrastructure/AutofacInfrastructureModule.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,20 @@ | ||
using Ardalis.GuardClauses; | ||
using System.Reflection; | ||
using Ardalis.GuardClauses; | ||
using Ardalis.ListStartupServices; | ||
using Autofac; | ||
using Autofac.Extensions.DependencyInjection; | ||
using Clean.Architecture.Core; | ||
using Ardalis.SharedKernel; | ||
using Clean.Architecture.Core.ContributorAggregate; | ||
using Clean.Architecture.Core.Interfaces; | ||
using Clean.Architecture.Core.Services; | ||
using Clean.Architecture.Infrastructure; | ||
using Clean.Architecture.Infrastructure.Data; | ||
using Clean.Architecture.Infrastructure.Data.Queries; | ||
using Clean.Architecture.Infrastructure.Email; | ||
using Clean.Architecture.UseCases.Contributors.Create; | ||
using Clean.Architecture.UseCases.Contributors.List; | ||
using FastEndpoints; | ||
using FastEndpoints.Swagger; | ||
using MediatR; | ||
using MediatR.Pipeline; | ||
using Serilog; | ||
|
||
var logger = Serilog.Log.Logger = new LoggerConfiguration() | ||
|
@@ -18,8 +26,6 @@ | |
|
||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory()); | ||
|
||
builder.Host.UseSerilog((_, config) => config.ReadFrom.Configuration(builder.Configuration)); | ||
|
||
builder.Services.Configure<CookiePolicyOptions>(options => | ||
|
@@ -47,12 +53,55 @@ | |
config.Path = "/listservices"; | ||
}); | ||
|
||
var assemblies = new[] | ||
{ | ||
Assembly.GetAssembly(typeof(Contributor)), | ||
Assembly.GetAssembly(typeof(CreateContributorCommand)) | ||
}; | ||
|
||
builder.Host.ConfigureContainer<ContainerBuilder>(containerBuilder => | ||
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); | ||
}); | ||
} | ||
} | ||
} | ||
|
||
builder.Services.AddScoped(typeof(IRepository<>), typeof(EfRepository<>)); | ||
builder.Services.AddScoped(typeof(IReadRepository<>), typeof(EfRepository<>)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
builder.Services.AddScoped<IListContributorsQueryService, ListContributorsQueryService>(); | ||
builder.Services.AddScoped<IMediator, Mediator>(); | ||
builder.Services.AddScoped(typeof(IPipelineBehavior<,>), typeof(LoggingBehavior<,>)); | ||
builder.Services.AddScoped<IDomainEventDispatcher, MediatRDomainEventDispatcher>(); | ||
builder.Services.AddScoped<IListContributorsQueryService, ListContributorsQueryService>(); | ||
builder.Services.AddScoped<IDeleteContributorService, DeleteContributorService>(); | ||
|
||
if (builder.Environment.IsDevelopment()) | ||
{ | ||
builder.Services.AddScoped<IEmailSender, FakeEmailSender>(); | ||
} | ||
else | ||
{ | ||
builder.Services.AddScoped<IEmailSender, SmtpEmailSender>(); | ||
} | ||
|
||
var app = builder.Build(); | ||
|
||
|
@@ -83,7 +132,7 @@ static void SeedDatabase(WebApplication app) | |
try | ||
{ | ||
var context = services.GetRequiredService<AppDbContext>(); | ||
// context.Database.Migrate(); | ||
// context.Database.Migrate(); | ||
context.Database.EnsureCreated(); | ||
SeedData.Initialize(services); | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?