Skip to content
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

chore: moved duplicate extension code to a shared extension #32

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

<ItemGroup>
<PackageReference Include="Dapr.AspNetCore" Version="1.13.1" />
<PackageReference Include="FluentValidation.AspNetCore" Version="11.3.0" />
<PackageReference Include="MicroElements.Swashbuckle.FluentValidation" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.6">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using BookStore.Services.AuthorApi.Persistence;
using BookStore.Services.AuthorApi.Services;
using BookStore.Services.AuthorApi.Shared.Authors;
using FluentValidation;
using FluentValidation.AspNetCore;
using MicroElements.Swashbuckle.FluentValidation.AspNetCore;
using Microsoft.EntityFrameworkCore;
using BookStore.Services.Shared.Extensions;

namespace BookStore.Services.AuthorApi.Api.Extensions;

Expand All @@ -28,57 +25,11 @@ IConfiguration configuration
.AddDaprClient();

services
.AddDbServices(
configuration
);

return services;
}

private static IServiceCollection AddSwaggerServices(
this IServiceCollection services
)
{
services
.AddEndpointsApiExplorer();

services
.AddSwaggerGen(options =>
{
options.CustomSchemaIds(type => type.DeclaringType is null ? $"{type.Name}" : $"{type.DeclaringType?.Name}.{type.Name}");
})
.AddFluentValidationRulesToSwagger();

return services;
}
.AddFluentValidationServices<AuthorDto.Mutate, AuthorDto.Mutate.Validator>();

private static IServiceCollection AddFluentValidationServices(
this IServiceCollection services
)
{
services
.AddValidatorsFromAssemblyContaining<AuthorDto.Mutate.Validator>();

services
.AddFluentValidationAutoValidation();

return services;
}

private static IServiceCollection AddDbServices(
this IServiceCollection services,
IConfiguration configuration
)
{
services
.AddDbContext<AuthorDbContext>(options =>
{
options
.UseSqlServer(
configuration
.GetConnectionString("database")
);
}
.AddDbServices<AuthorDbContext>(
configuration
);

return services;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using BookStore.Services.AuthorApi.Persistence;
using BookStore.Services.Shared.Middleware;
using Microsoft.EntityFrameworkCore;
using BookStore.Services.Shared.Extensions;

namespace BookStore.Services.AuthorApi.Api.Extensions;

Expand Down Expand Up @@ -54,37 +54,4 @@ this WebApplication app

return app;
}

private static WebApplication AddDevelopmentMiddleWare(
this WebApplication app
)
{
app
.UseSwagger();

app
.UseSwaggerUI();

return app;
}

private static WebApplication AddCustomMiddleware(
this WebApplication app
)
{
app
.UseMiddleware<ExceptionMiddleware>();

return app;
}

private static WebApplication AddAuthMiddleWare(
this WebApplication app
)
{
app
.UseAuthorization();

return app;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

<ItemGroup>
<PackageReference Include="Dapr.AspNetCore" Version="1.13.1" />
<PackageReference Include="FluentValidation.AspNetCore" Version="11.3.0" />
<PackageReference Include="MicroElements.Swashbuckle.FluentValidation" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.6">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using BookStore.Services.BookApi.Persistence;
using BookStore.Services.BookApi.Services;
using BookStore.Services.BookApi.Shared.Books;
using FluentValidation;
using FluentValidation.AspNetCore;
using MicroElements.Swashbuckle.FluentValidation.AspNetCore;
using Microsoft.EntityFrameworkCore;
using BookStore.Services.Shared.Extensions;

namespace BookStore.Services.BookApi.Api.Extensions;

Expand All @@ -28,57 +25,11 @@ IConfiguration configuration
.AddRestServices();

services
.AddDbServices(
configuration
);

return services;
}

private static IServiceCollection AddSwaggerServices(
this IServiceCollection services
)
{
services
.AddEndpointsApiExplorer();

services
.AddSwaggerGen(options =>
{
options.CustomSchemaIds(type => type.DeclaringType is null ? $"{type.Name}" : $"{type.DeclaringType?.Name}.{type.Name}");
})
.AddFluentValidationRulesToSwagger();

return services;
}
.AddFluentValidationServices<BookDto.Mutate, BookDto.Mutate.Validator>();

private static IServiceCollection AddFluentValidationServices(
this IServiceCollection services
)
{
services
.AddValidatorsFromAssemblyContaining<BookDto.Mutate.Validator>();

services
.AddFluentValidationAutoValidation();

return services;
}

private static IServiceCollection AddDbServices(
this IServiceCollection services,
IConfiguration configuration
)
{
services
.AddDbContext<BookDbContext>(options =>
{
options
.UseSqlServer(
configuration
.GetConnectionString("database")
);
}
.AddDbServices<BookDbContext>(
configuration
);

return services;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using BookStore.Services.BookApi.Persistence;
using BookStore.Services.Shared.Middleware;
using Microsoft.EntityFrameworkCore;
using BookStore.Services.Shared.Extensions;

namespace BookStore.Services.BookApi.Api.Extensions;

Expand Down Expand Up @@ -60,37 +60,4 @@ this WebApplication app

return app;
}

private static WebApplication AddDevelopmentMiddleWare(
this WebApplication app
)
{
app
.UseSwagger();

app
.UseSwaggerUI();

return app;
}

private static WebApplication AddCustomMiddleware(
this WebApplication app
)
{
app
.UseMiddleware<ExceptionMiddleware>();

return app;
}

private static WebApplication AddAuthMiddleWare(
this WebApplication app
)
{
app
.UseAuthorization();

return app;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentValidation.AspNetCore" Version="11.3.0" />
<PackageReference Include="MicroElements.Swashbuckle.FluentValidation" Version="6.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.6" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="6.6.2" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.6.2" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using FluentValidation;
using FluentValidation.AspNetCore;
using MicroElements.Swashbuckle.FluentValidation.AspNetCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace BookStore.Services.Shared.Extensions;

public static class IServiceCollectionSharedExtensions
{
public static IServiceCollection AddSwaggerServices(
this IServiceCollection services
)
{
services
.AddEndpointsApiExplorer();

services
.AddSwaggerGen(options =>
{
options.CustomSchemaIds(type =>
type.DeclaringType is null
? $"{type.Name}"
: $"{type.DeclaringType?.Name}.{type.Name}");
}
)
.AddFluentValidationRulesToSwagger();

return services;
}

public static IServiceCollection AddFluentValidationServices<T, U>(
this IServiceCollection services
) where U : AbstractValidator<T>
{
services
.AddValidatorsFromAssemblyContaining<U>();

services
.AddFluentValidationAutoValidation();

return services;
}

public static IServiceCollection AddDbServices<T>(
this IServiceCollection services,
IConfiguration configuration
) where T : DbContext
{
services
.AddDbContext<T>(options =>
{
options
.UseSqlServer(
configuration
.GetConnectionString("database")
);
}
);

return services;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using BookStore.Services.Shared.Middleware;
using Microsoft.AspNetCore.Builder;

namespace BookStore.Services.Shared.Extensions;

public static class WebApplicationSharedExtensions
{
public static WebApplication AddDevelopmentMiddleWare(
this WebApplication app
)
{
app
.UseSwagger();

app
.UseSwaggerUI();

return app;
}

public static WebApplication AddCustomMiddleware(
this WebApplication app
)
{
app
.UseMiddleware<ExceptionMiddleware>();

return app;
}

public static WebApplication AddAuthMiddleWare(
this WebApplication app
)
{
app
.UseAuthorization();

return app;
}
}
Loading