Skip to content

Commit

Permalink
refactor: ♻️ some refactoring for adding modules
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdihadeli committed Jun 13, 2023
1 parent 8b3a6e7 commit c6d0045
Show file tree
Hide file tree
Showing 32 changed files with 146 additions and 58 deletions.
4 changes: 0 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -410,10 +410,6 @@ TODO

For running and debugging this application we could use our preferred Dev Environment, for example `Visual Studio`, `VsCode` Or `Rider` for me, it's Rider, So just open the [Vertical.Slice.Template.sln](./Vertical.Slice.Template.sln) solution file in the IDE and run, debug your application.

For testing apis I used [REST Client](https://marketplace.visualstudio.com/items?itemName=humao.rest-client) plugin of VSCode its related file scenarios are available in [\_httpclients](_httpclients) folder. also after running api you have access to `swagger open api` for all microservices in `/swagger` route path.

In this application I use a `fake email sender` with name of [ethereal](https://ethereal.email/) as a SMTP provider for sending email. after sending email by the application you can see the list of sent emails in [ethereal messages panel](https://ethereal.email/messages). My temp username and password is available inner the all of [appsettings file](./src/Services/Customers/ECommerce.Services.Customers.Api/appsettings.json).

### Using PM2

For ruining all microservices and control on their running mode we could use [PM2](https://pm2.keymetrics.io/) tools. for installing `pm2` on our system globally we should use this command:
Expand Down
1 change: 1 addition & 0 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Ardalis.Specification.EntityFrameworkCore" Version="7.0.0" />
<PackageVersion Include="AspNetCore.HealthChecks.Network" Version="6.0.4" />
<PackageVersion Include="AspNetCore.HealthChecks.Prometheus.Metrics" Version="6.0.2" />
<PackageVersion Include="AspNetCore.HealthChecks.Publisher.Prometheus" Version="6.0.2" />
Expand Down
17 changes: 5 additions & 12 deletions src/Vertical.Slice.Template.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
using Serilog;
using Serilog.Events;
using Vertical.Slice.Template;
using Vertical.Slice.Template.Shared.Extensions.WebApplicationBuilderExtensions;
using Vertical.Slice.Template.Shared.Extensions.WebApplicationExtensions;
using Vertical.Slice.Template.Shared;
using Vertical.Slice.Template.Shared.Core.Extensions.ServiceCollectionsExtensions;
using Vertical.Slice.Template.Shared.Swagger;
using Vertical.Slice.Template.Shared.Web.Extensions.ServiceCollection;
using Vertical.Slice.Template.Shared.Web.Minimal.Extensions;

// https://github.com/serilog/serilog-aspnetcore#two-stage-initialization
Expand Down Expand Up @@ -40,11 +39,7 @@
}
);

builder.AddInfrastructures();

builder.AddStorage();

builder.AddModulesServices();
builder.AddCatalogsServices();

var app = builder.Build();

Expand All @@ -57,9 +52,9 @@
);
}

await app.UseInfrastructure();
await app.UseCatalogs();

await app.ConfigureModules();
app.MapCatalogsEndpoints();

app.MapModulesEndpoints();

Expand All @@ -71,8 +66,6 @@
}
// #endif

app.MapGet("/", () => "Vertical.Slice.Template Api.").ExcludeFromDescription();

await app.RunAsync();
}
catch (Exception ex)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using Ardalis.Specification;

namespace Vertical.Slice.Template.Shared.Abstractions.Ef.Repository;

public interface IReadRepository<T> : IReadRepositoryBase<T>
where T : class { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using Ardalis.Specification;

namespace Vertical.Slice.Template.Shared.Abstractions.Ef.Repository;

// from Ardalis.Specification
public interface IRepository<T> : IRepositoryBase<T>
where T : class { }
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,11 @@ public interface IHttpCommand<TRequest>
IMapper Mapper { get; init; }
CancellationToken CancellationToken { get; init; }
}

public interface IHttpCommand
{
HttpContext HttpContext { get; init; }
IMediator Mediator { get; init; }
IMapper Mapper { get; init; }
CancellationToken CancellationToken { get; init; }
}
14 changes: 14 additions & 0 deletions src/Vertical.Slice.Template.Shared/Core/Ef/EfRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Ardalis.Specification.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Vertical.Slice.Template.Shared.Abstractions.Ef.Repository;

namespace Vertical.Slice.Template.Shared.Core.Ef;

// inherit from Ardalis.Specification type
public class EfRepository<T, TContext> : RepositoryBase<T>, IReadRepository<T>, IRepository<T>
where T : class
where TContext : DbContext
{
public EfRepository(TContext dbContext)
: base(dbContext) { }
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Reflection;
using Sieve.Services;
using Vertical.Slice.Template.Shared.Abstractions.Core.Domain.Events;
using Vertical.Slice.Template.Shared.Abstractions.Ef.Repository;
using Vertical.Slice.Template.Shared.Core.Domain.Events;
using Vertical.Slice.Template.Shared.Core.Ef;
using Vertical.Slice.Template.Shared.Core.Paging;

namespace Vertical.Slice.Template.Shared.Core.Extensions.ServiceCollectionsExtensions;
Expand All @@ -18,7 +20,6 @@ public static IServiceCollection AddCore(this IServiceCollection services, param

services.AddScoped<ISieveProcessor, ApplicationSieveProcessor>();
services.ScanAndRegisterDbExecutors(scanAssemblies);

return services;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Reflection;
using Microsoft.Extensions.DependencyInjection.Extensions;

namespace Vertical.Slice.Template.Shared.Web.Extensions.ServiceCollection;
namespace Vertical.Slice.Template.Shared.Core.Extensions.ServiceCollectionsExtensions;

public static partial class ServiceCollectionExtensions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Runtime.CompilerServices;
using Microsoft.Extensions.Options;

namespace Vertical.Slice.Template.Shared.Web.Extensions.ServiceCollection;
namespace Vertical.Slice.Template.Shared.Core.Extensions.ServiceCollectionsExtensions;

public static partial class ServiceCollectionExtensions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
using Vertical.Slice.Template.Shared.Abstractions.Core.Domain.Events;
using Vertical.Slice.Template.Shared.Abstractions.Ef;
using Vertical.Slice.Template.Shared.Core.Extensions;
using Vertical.Slice.Template.Shared.Core.Extensions.ServiceCollectionsExtensions;
using Vertical.Slice.Template.Shared.EF.Interceptors;
using Vertical.Slice.Template.Shared.Web.Extensions.ServiceCollection;

namespace Vertical.Slice.Template.Shared.EF.Extensions;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
using Polly;
using Polly.CircuitBreaker;
using Polly.Timeout;
using Vertical.Slice.Template.Shared.Core.Extensions.ServiceCollectionsExtensions;
using Vertical.Slice.Template.Shared.Resiliency.Options;
using Vertical.Slice.Template.Shared.Web.Extensions.ServiceCollection;

namespace Vertical.Slice.Template.Shared.Resiliency.Extensions;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Ardalis.Specification.EntityFrameworkCore" />
<PackageReference Include="Asp.Versioning.Http" />
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" />
<PackageReference Include="AutoMapper" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Microsoft.AspNetCore.Builder;
using Vertical.Slice.Template.Shared.Web.Extensions.ServiceCollection;
using Vertical.Slice.Template.Shared.Core.Extensions.ServiceCollectionsExtensions;

namespace Vertical.Slice.Template.Shared.Web.Extensions;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Asp.Versioning;
using Microsoft.AspNetCore.Builder;

namespace Vertical.Slice.Template.Shared.Extensions.WebApplicationBuilderExtensions;
namespace Vertical.Slice.Template.Shared.Web.Extensions.WebApplicationBuilderExtensions;

public static partial class WebApplicationBuilderExtensions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Vertical.Slice.Template.Shared.Abstractions.Web;
using Vertical.Slice.Template.Shared.Core.Exceptions;

namespace Vertical.Slice.Template.Shared;
namespace Vertical.Slice.Template.Shared.Web.ProblemDetail;

internal sealed class DefaultProblemDetailMapper : IProblemDetailMapper
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.AspNetCore.Builder;

namespace Vertical.Slice.Template.Shared.Web.Middlewares.CaptureExceptionMiddleware;
namespace Vertical.Slice.Template.Shared.Web.ProblemDetail.Middlewares.CaptureExceptionMiddleware;

public static class CaptureExceptionMiddlewareExtensions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;

namespace Vertical.Slice.Template.Shared.Web.Middlewares.CaptureExceptionMiddleware;
namespace Vertical.Slice.Template.Shared.Web.ProblemDetail.Middlewares.CaptureExceptionMiddleware;

// https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/write
public class CaptureExceptionMiddlewareImp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
using Microsoft.AspNetCore.Http;
using Scrutor;
using Vertical.Slice.Template.Shared.Abstractions.Web;
using Vertical.Slice.Template.Shared.Core.Extensions.ServiceCollectionsExtensions;
using Vertical.Slice.Template.Shared.Core.Reflection;
using Vertical.Slice.Template.Shared.Web.Extensions.ServiceCollection;

namespace Vertical.Slice.Template.Shared.Web.ProblemDetail;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Vertical.Slice.Template.Shared.Data;
using Vertical.Slice.Template.Shared.EF;

namespace Vertical.Slice.Template.Products.Data;
namespace Vertical.Slice.Template.Products.Data.Configurations;

public class ProductEntityTypeConfigurations : IEntityTypeConfiguration<Product>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Sieve.Services;
using Vertical.Slice.Template.Products.ReadModel;

namespace Vertical.Slice.Template.Products.Data;
namespace Vertical.Slice.Template.Products.Data.Configurations;

public class SieveProductReadConfigurations : ISieveConfiguration
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ > Handle([AsParameters] CreateProductRequestParameters requestParameters)
// https://learn.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis/parameter-binding#parameter-binding-for-argument-lists-with-asparameters
// https://learn.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis/parameter-binding#binding-precedence
internal record CreateProductRequestParameters(
[FromBody] CreateProductRequest Request,
[FromBody] CreateProductRequest? Request,
HttpContext HttpContext,
IMediator Mediator,
IMapper Mapper,
CancellationToken CancellationToken
) : IHttpCommand<CreateProductRequest>;
) : IHttpCommand<CreateProductRequest?>;

internal record CreateProductResponse(Guid Id);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using AutoMapper;
using FluentValidation;
using Microsoft.EntityFrameworkCore;
using Sieve.Services;
using Vertical.Slice.Template.Products.Dtos.v1;
using Vertical.Slice.Template.Products.Models;
Expand Down Expand Up @@ -98,11 +99,9 @@ public void Register(IServiceCollection services)
services.AddTransient<GetProductsExecutor>(sp =>
{
var context = sp.GetRequiredService<CatalogsDbContext>();
var mapper = sp.GetRequiredService<IMapper>();
IQueryable<Product> Query(CancellationToken cancellationToken)
{
var collection = context.Products;
var collection = context.Products.AsNoTracking();
return collection;
}
Expand Down
11 changes: 6 additions & 5 deletions src/Vertical.Slice.Template/Products/ProductConfigurations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,27 @@
using Vertical.Slice.Template.Products.Features.CreatingProduct.v1;
using Vertical.Slice.Template.Products.Features.GettingProductById.v1;
using Vertical.Slice.Template.Products.Features.GettingProductsByPage.v1;
using Vertical.Slice.Template.Shared;
using Vertical.Slice.Template.Shared.Abstractions.Web;

namespace Vertical.Slice.Template.Products;

internal class ProductConfigurations : IModuleConfiguration
internal static class ProductConfigurations
{
public const string Tag = "Products";
public const string ProductsPrefixUri = "api/v{version:apiVersion}/catalogs/products";
public const string ProductsPrefixUri = $"{CatalogsConfigurations.CatalogsPrefixUri}/products";

public WebApplicationBuilder AddModuleServices(WebApplicationBuilder builder)
public static WebApplicationBuilder AddProductsServices(this WebApplicationBuilder builder)
{
return builder;
}

public Task<WebApplication> ConfigureModule(WebApplication app)
public static Task<WebApplication> UseProducts(this WebApplication app)
{
return Task.FromResult(app);
}

public IEndpointRouteBuilder MapEndpoints(IEndpointRouteBuilder endpoints)
public static IEndpointRouteBuilder MapProductsEndpoints(this IEndpointRouteBuilder endpoints)
{
var products = endpoints.NewVersionedApi(Tag);
var productsV1 = products.MapGroup(ProductsPrefixUri).HasDeprecatedApiVersion(0.9).HasApiVersion(1.0);
Expand Down
47 changes: 47 additions & 0 deletions src/Vertical.Slice.Template/Shared/CatalogsConfigurations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Vertical.Slice.Template.Products;
using Vertical.Slice.Template.Shared.Extensions.WebApplicationBuilderExtensions;
using Vertical.Slice.Template.Shared.Extensions.WebApplicationExtensions;

namespace Vertical.Slice.Template.Shared;

public static class CatalogsConfigurations
{
public const string CatalogsPrefixUri = "api/v{version:apiVersion}/catalogs";

public static WebApplicationBuilder AddCatalogsServices(this WebApplicationBuilder builder)
{
// Shared
builder.AddInfrastructures();
builder.AddStorage();

// Modules
builder.AddProductsServices();

return builder;
}

public static async Task<WebApplication> UseCatalogs(this WebApplication app)
{
// Shared
await app.UseInfrastructure();

// Modules
await app.UseProducts();

return app;
}

public static IEndpointRouteBuilder MapCatalogsEndpoints(this IEndpointRouteBuilder endpoints)
{
// Shared
endpoints.MapGet("/", () => "Catalogs Api.").ExcludeFromDescription();

// Modules
endpoints.MapProductsEndpoints();

return endpoints;
}
}
10 changes: 10 additions & 0 deletions src/Vertical.Slice.Template/Shared/Data/GenericRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Vertical.Slice.Template.Shared.Core.Ef;

namespace Vertical.Slice.Template.Shared.Data;

public class GenericRepository<T> : EfRepository<T, CatalogsDbContext>
where T : class
{
public GenericRepository(CatalogsDbContext dbContext)
: base(dbContext) { }
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
using CorrelationId.DependencyInjection;
using FluentValidation;
using MediatR;
using Microsoft.AspNetCore.Builder;
using Vertical.Slice.Template.Shared.Abstractions.Ef.Repository;
using Vertical.Slice.Template.Shared.Cache;
using Vertical.Slice.Template.Shared.Cache.Behaviours;
using Vertical.Slice.Template.Shared.Core.Extensions.ServiceCollectionsExtensions;
using Vertical.Slice.Template.Shared.Data;
using Vertical.Slice.Template.Shared.EF;
using Vertical.Slice.Template.Shared.Logging;
using Vertical.Slice.Template.Shared.Resiliency;
using Vertical.Slice.Template.Shared.Swagger;
using Vertical.Slice.Template.Shared.Validation;
using Vertical.Slice.Template.Shared.Validation.Extensions;
using Vertical.Slice.Template.Shared.Web.Extensions;
using Vertical.Slice.Template.Shared.Web.Extensions.WebApplicationBuilderExtensions;

namespace Vertical.Slice.Template.Shared.Extensions.WebApplicationBuilderExtensions;

Expand Down Expand Up @@ -64,6 +65,9 @@ public static WebApplicationBuilder AddInfrastructures(this WebApplicationBuilde

builder.Services.AddCustomValidators(typeof(CatalogsMetadata).Assembly);

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

return builder;
}
}

0 comments on commit c6d0045

Please sign in to comment.