Skip to content
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
5 changes: 1 addition & 4 deletions Hestia.Access/Hestia.Access.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MediatR" Version="12.4.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Hestia.Domain\Hestia.Domain.csproj" />
<ProjectReference Include="..\Hestia.Mediator\Hestia.Mediator.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MediatR;
using Hestia.Mediator.Infrastructure.Messaging;
using Hestia.Mediator.Infrastructure.Types;

namespace Hestia.Access.Requests.Authentication.Commands.CreateTokenLog;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using MediatR;
using Hestia.Mediator.Infrastructure.Messaging;

namespace Hestia.Access.Requests.Product.Commands.CreateProduct;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using MediatR;
using Hestia.Mediator.Infrastructure.Messaging;

namespace Hestia.Access.Requests.Product.Commands.DeleteProduct;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using MediatR;
using Hestia.Mediator.Infrastructure.Messaging;

namespace Hestia.Access.Requests.Product.Commands.UpdateProduct;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using MediatR;
using Hestia.Mediator.Infrastructure.Messaging;

namespace Hestia.Access.Requests.Product.Queries.GetExisting;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using MediatR;
using Hestia.Mediator.Infrastructure.Messaging;

namespace Hestia.Access.Requests.Product.Queries.GetExisting;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using MediatR;
using Hestia.Mediator.Infrastructure.Messaging;

namespace Hestia.Access.Requests.Product.Queries.GetExisting;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using MediatR;
using Hestia.Mediator.Infrastructure.Messaging;

namespace Hestia.Access.Requests.Product.Queries.GetExisting;

Expand Down
3 changes: 2 additions & 1 deletion Hestia.Access/Requests/Shared/ExecuteSaveChangesAsync.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MediatR;
using Hestia.Mediator.Infrastructure.Messaging;
using Hestia.Mediator.Infrastructure.Types;

namespace Hestia.Access.Requests.Shared;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Hestia.Domain.Models.Authentication;
using MediatR;
using Hestia.Mediator.Infrastructure.Messaging;
using Microsoft.AspNetCore.Identity;

namespace Hestia.Access.Requests.User.Commands.CreateUser;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using MediatR;
using Hestia.Mediator.Infrastructure.Messaging;

namespace Hestia.Access.Requests.User.Commands.CreateUser;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Hestia.Domain.Models.Authentication;
using MediatR;
using Hestia.Mediator.Infrastructure.Messaging;

namespace Hestia.Access.Requests.User.Queries.UserExists;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Hestia.Domain.Models.Authentication;
using MediatR;
using Hestia.Mediator.Infrastructure.Messaging;

namespace Hestia.Access.Requests.User.Queries.ValidateUserLogin;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@
using Hestia.Api.Configuration.Application;
using Hestia.Api.Tests.Shared;
using Hestia.Application.Interfaces.Authentication;
using Hestia.Application.Interfaces.Infrastructure;
using Hestia.Application.Interfaces.Product;
using Hestia.Application.Interfaces.Response;
using Hestia.Domain.Models.Authentication;
using Hestia.Mediator.Infrastructure;
using Hestia.Mediator.Infrastructure.Layers;
using Hestia.Persistence.Contexts;
using MediatR;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
Expand All @@ -33,7 +34,10 @@ public void ConfigureApplicationServices_ShouldRegisterExpectedServices()
services.AddSingleton<IWebHostEnvironment>(new MockWebHostEnvironment { EnvironmentName = Environments.Development });
services.AddMemoryCache();
services.AddDataProtection();
services.AddScoped<HestiaContext>();
services.AddDbContext<HestiaContext>(options =>
{
options.UseInMemoryDatabase("TestDb");
});

// Add Identity services
services.AddIdentityCore<ApplicationUser>(options => { })
Expand Down Expand Up @@ -67,10 +71,6 @@ public void ConfigureApplicationServices_ShouldRegisterExpectedServices()
var mediator = serviceProvider.GetService<IMediator>();
Assert.NotNull(mediator);

// Check if AutoMapper is registered
var mapper = serviceProvider.GetService<AutoMapper.IMapper>();
Assert.NotNull(mapper);

// Check if FluentValidation validators are registered
var validators = serviceProvider.GetServices<IValidator>();
Assert.NotNull(validators);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;
Expand Down Expand Up @@ -35,7 +36,10 @@ public void ConfigureAuthentication_ShouldAddAuthenticationServices()
{
// Arrange
var builder = WebApplication.CreateBuilder();
builder.Services.AddScoped<HestiaContext>();
builder.Services.AddDbContext<HestiaContext>(options =>
{
options.UseInMemoryDatabase("TestDb");
});
builder.Environment.EnvironmentName = "Development";
builder.Configuration.AddJsonFile("appsettings.json");
builder.Configuration.AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Hestia.Background.Interfaces;
using Hestia.Background.Tasks;
using Hestia.Persistence.Contexts;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
Expand Down Expand Up @@ -47,8 +48,15 @@ public static class ServiceCollectionExtensions
{
public static IServiceCollection ConfigureInfrastructuresServices(this IServiceCollection services)
{
services.AddScoped<HestiaContext>();
services.AddScoped<RheaContext>();
services.AddDbContext<HestiaContext>(options =>
{
options.UseInMemoryDatabase("TestDb");
});

services.AddDbContext<RheaContext>(options =>
{
options.UseInMemoryDatabase("TestDb");
});

services.AddMemoryCache();
services.ConfigureBackgroundRunners();
Expand Down
19 changes: 13 additions & 6 deletions Hestia.Api.Tests/Hestia.Api.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="9.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="coverlet.collector" Version="6.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="9.0.8" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.8" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="9.0.8" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
61 changes: 45 additions & 16 deletions Hestia.Api/Configuration/Application/ConfigureServices.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
using FluentValidation;
using Hestia.Application.Handlers.Validation;
using Hestia.Application.Interfaces.Authentication;
using Hestia.Application.Interfaces.Infrastructure;
using Hestia.Application.Interfaces.Product;
using Hestia.Application.Interfaces.Response;
using Hestia.Application.Services;
using Hestia.Application.Services.Authentication;
using Hestia.Application.Services.Product;
using MediatR;
using Hestia.Mediator.Infrastructure;
using Hestia.Mediator.Infrastructure.Layers;
using Hestia.Mediator.Infrastructure.Messaging;
using Hestia.Mediator.Infrastructure.Pipeline;
using Hestia.Mediator.Services;
using System.Reflection;
using System.Text.Json.Serialization;

Expand All @@ -22,19 +25,12 @@ public static IServiceCollection ConfigureApplicationServices(this IServiceColle
services.AddApiServices();
services.AddApplicationServices();

// MediatR
services.AddMediatR();

// AutoMapper
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
// Mediator pattern
services.AddMediator();

// FluentValidation validators
services.AddFluentValidationValidators();

// MediatR layers
services.AddTransient<IAccessLayer, MediatrAdapter>();
services.AddTransient<ICoreLayer, MediatrAdapter>();

services.AddControllers().AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
Expand All @@ -43,16 +39,49 @@ public static IServiceCollection ConfigureApplicationServices(this IServiceColle
return services;
}

public static IServiceCollection AddMediatR(this IServiceCollection services)
public static IServiceCollection AddMediator(this IServiceCollection services)
{
services.AddMediatR(cfg =>
{
cfg.RegisterServicesFromAssemblies(AppDomain.CurrentDomain.GetAssemblies());
});
// Mediator pattern service
services.AddSingleton<IMediator, MediatorService>();

// Mediator pattern layers
services.AddTransient<IAccessLayer, MediatorAdapter>();
services.AddTransient<ICoreLayer, MediatorAdapter>();

services.AddRequestHandlers();

return services;
}

private static void AddRequestHandlers(this IServiceCollection services)
{
var handlerInterfaceType = typeof(IRequestHandler<,>);

var assemblies = Directory
.GetFiles(AppContext.BaseDirectory, "*.dll", SearchOption.TopDirectoryOnly)
.Where(file =>
{
string name = Path.GetFileName(file);
// Adjust filter to match your solution's assemblies
return name.StartsWith("Hestia.", StringComparison.OrdinalIgnoreCase);
})
.Select(Assembly.LoadFrom)
.ToList();

var handlerTypes = assemblies
.SelectMany(a => a.GetTypes())
.Where(t => !t.IsAbstract && !t.IsInterface)
.SelectMany(t => t.GetInterfaces(), (type, iface) => new { type, iface })
.Where(x => x.iface.IsGenericType &&
x.iface.GetGenericTypeDefinition() == handlerInterfaceType)
.ToList();

foreach (var handler in handlerTypes)
{
services.AddTransient(handler.iface, handler.type);
}
}

public static IServiceCollection AddFluentValidationValidators(this IServiceCollection services)
{
services.AddValidatorsFromAssembly(Assembly.Load("Hestia.Domain"));
Expand Down
14 changes: 12 additions & 2 deletions Hestia.Api/Configuration/Infrastructure/ConfigureServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Hestia.Background.Interfaces;
using Hestia.Background.Tasks;
using Hestia.Persistence.Contexts;
using Microsoft.EntityFrameworkCore;

namespace Hestia.Api.Configuration.Infrastructure;

Expand All @@ -27,8 +28,17 @@ private static IServiceCollection ConfigureBackgroundRunners(this IServiceCollec

private static IServiceCollection ConfigureDbContexts(this IServiceCollection services)
{
services.AddScoped<HestiaContext>();
services.AddScoped<RheaContext>();
services.AddDbContext<HestiaContext>((serviceProvider, options) =>
{
var configuration = serviceProvider.GetRequiredService<IConfiguration>();
options.UseNpgsql(configuration.GetConnectionString("AuthServer"));
});

services.AddDbContext<RheaContext>((serviceProvider, options) =>
{
var configuration = serviceProvider.GetRequiredService<IConfiguration>();
options.UseNpgsql(configuration.GetConnectionString("DataServer"));
});

return services;
}
Expand Down
2 changes: 1 addition & 1 deletion Hestia.Api/Controllers/AuthenticationController.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Hestia.Application.Handlers.Authentication.Commands.Login;
using Hestia.Application.Handlers.Authentication.Commands.Register;
using Hestia.Application.Interfaces.Infrastructure;
using Hestia.Application.Models.Authentication.Inbound;
using Hestia.Application.Models.Authentication.Outbound;
using Hestia.Application.Models.Shared;
using Hestia.Mediator.Infrastructure.Layers;
using Microsoft.AspNetCore.Mvc;

namespace Hestia.Api.Controllers;
Expand Down
2 changes: 1 addition & 1 deletion Hestia.Api/Controllers/ProductController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
using Hestia.Application.Handlers.Product.Commands.DeleteProduct;
using Hestia.Application.Handlers.Product.Commands.UpdateProduct;
using Hestia.Application.Handlers.Product.Queries.GetProduct;
using Hestia.Application.Interfaces.Infrastructure;
using Hestia.Application.Models.Shared;
using Hestia.Domain.Models.Product.Inbound.CreateProduct;
using Hestia.Domain.Models.Product.Inbound.GetProduct;
using Hestia.Domain.Models.Product.Inbound.UpdateProduct;
using Hestia.Mediator.Infrastructure.Layers;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

Expand Down
20 changes: 11 additions & 9 deletions Hestia.Api/Hestia.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,27 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.11.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="9.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="9.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.0">
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="12.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.8" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="9.0.8" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="9.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.8">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.22.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.1.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="7.1.0" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="9.0.4" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="9.0.4" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Hestia.Application\Hestia.Application.csproj" />
<ProjectReference Include="..\Hestia.Background\Hestia.Background.csproj" />
<ProjectReference Include="..\Hestia.Domain\Hestia.Domain.csproj" />
<ProjectReference Include="..\Hestia.Mediator\Hestia.Mediator.csproj" />
<ProjectReference Include="..\Hestia.Persistence\Hestia.Persistence.csproj" />
</ItemGroup>

Expand Down
Loading
Loading