Skip to content

Commit

Permalink
New DependencyInjections for improving clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
novru authored and novru committed May 17, 2024
1 parent eda9da2 commit 66b0e60
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 48 deletions.
4 changes: 0 additions & 4 deletions Api/Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,4 @@
<ProjectReference Include="..\Infrastructure\Persistence\Persistence.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Extensions\" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,54 +1,15 @@
using Application.Models.Configurations;
using Application.Repositories;
using Application.Repositories.BookRepository;
using Application.Repositories.CommentRepository;
using Application.Repositories.CourseRepository;
using Application.Repositories.UserRepository;
using Application.Services;
using Infrastructure.Services;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using Persistence.Repositories;
using Persistence.Repositories.BookRepository;
using Persistence.Repositories.CommentRepository;
using Persistence.Repositories.CourseRepository;
using Persistence.Repositories.UserRepository;
using System.Text;

namespace Api.Extensions;

public static class Extension
public static class SwaggerExtensions
{
public static IServiceCollection AddServices(this IServiceCollection services)
{
services.AddScoped<IUserService, UserService>();
services.AddScoped<IHostService, HostService>();
services.AddScoped<IAuthService, AuthService>();

services.AddScoped<IPassHashService, PassHashService>();
services.AddScoped<IJwtService, JwtService>();
services.AddScoped<IBlobService, BlobService>();

return services;
}

public static IServiceCollection AddRepositories(this IServiceCollection services)
{
services.AddScoped<IReadBookRepository, ReadBookRepository>();
services.AddScoped<IWriteBookRepository, WriteBookRepository>();
services.AddScoped<IReadUserRepository, ReadUserRepository>();
services.AddScoped<IWriteUserRepository, WriteUserRepository>();
services.AddScoped<IWriteCourseRepository, WriteCourseRepository>();
services.AddScoped<IReadCourseRepository, ReadCourseRepository>();
services.AddScoped<IReadCommentRepository, ReadCommentRepository>();
services.AddScoped<IWriteCommentRepository, WriteCommentRepository>();

services.AddScoped<IUnitOfWork, UnitOfWork>();

return services;
}

public static IServiceCollection AddSwagger(this IServiceCollection services)
{
services.AddEndpointsApiExplorer();
Expand Down
12 changes: 8 additions & 4 deletions Api/Program.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
using Api.Extensions;
using Api.GlobalException;
using Application.Models.Configurations;
using Infrastructure.DependencyInjection;
using Microsoft.EntityFrameworkCore;
using Persistence.Context;
using Persistence.DependencyInjection;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddServices();
builder.Services.AddRepositories();

builder.Services
.AddInfrastructureServices()
.AddPersistenceRepositories();


builder.Services.AddAuthenticationAndAuthorization(builder.Configuration);
builder.Services.AddSwagger();
builder.Services.AddExceptionHandler<GlobalExceptionHandler>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Application.Services;
using Infrastructure.Services;
using Microsoft.Extensions.DependencyInjection;
using System.Runtime.CompilerServices;

namespace Infrastructure.DependencyInjection;

public static class InfrastructureDependencyInjection
{
public static IServiceCollection AddInfrastructureServices(this IServiceCollection services)
{
services.AddScoped<IUserService, UserService>();

services.AddScoped<IHostService, HostService>();

services.AddScoped<IAuthService, AuthService>();

// helper services

services.AddScoped<IPassHashService, PassHashService>();

services.AddScoped<IJwtService, JwtService>();

services.AddScoped<IBlobService, BlobService>();

return services;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Application.Repositories.BookRepository;
using Application.Repositories.CommentRepository;
using Application.Repositories.CourseRepository;
using Application.Repositories.UserRepository;
using Application.Repositories;
using Microsoft.Extensions.DependencyInjection;
using Persistence.Repositories.BookRepository;
using Persistence.Repositories.CommentRepository;
using Persistence.Repositories.CourseRepository;
using Persistence.Repositories.UserRepository;
using Persistence.Repositories;

namespace Persistence.DependencyInjection;

public static class PersistenceDependencyInjection
{
public static IServiceCollection AddPersistenceRepositories(this IServiceCollection services)
{
services.AddScoped<IReadBookRepository, ReadBookRepository>();
services.AddScoped<IWriteBookRepository, WriteBookRepository>();

services.AddScoped<IReadUserRepository, ReadUserRepository>();
services.AddScoped<IWriteUserRepository, WriteUserRepository>();

services.AddScoped<IWriteCourseRepository, WriteCourseRepository>();
services.AddScoped<IReadCourseRepository, ReadCourseRepository>();

services.AddScoped<IReadCommentRepository, ReadCommentRepository>();
services.AddScoped<IWriteCommentRepository, WriteCommentRepository>();

services.AddScoped<IUnitOfWork, UnitOfWork>();

return services;
}

}

0 comments on commit 66b0e60

Please sign in to comment.