Unverified Commit 9ae05f5b authored by Almouhannad Hafez's avatar Almouhannad Hafez Committed by GitHub

Merge pull request #5 from Almouhannad/B_Add-MediatR-and-CQRS-bases

B add mediat r and cqrs bases
parents ead3c9e3 4c0a6b16
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="MediatR" Version="12.4.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.8" /> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.8"> <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.8">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
......
using API.Options.Database; using API.Options.Database;
using Domain.Repositories.Base;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Persistence.Context; using Persistence.Context;
using Persistence.Repositories.Base;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
...@@ -32,15 +30,29 @@ builder.Services.AddDbContext<ClinicsDbContext>( ...@@ -32,15 +30,29 @@ builder.Services.AddDbContext<ClinicsDbContext>(
// Add services to the container. // Add services to the container.
#region Link repositories #region Link interfaces implemented in persistence
// AddTransient: Created on demand, every time they are requested, not shared across requests or components. // Using Scrutor library
builder.Services.AddTransient(typeof(IRepository<>), typeof(Repositroy<>)); builder
.Services
.Scan(
selector => selector
.FromAssemblies(Persistence.AssemblyReference.Assembly
// Add other assemblies here
)
.AddClasses(false)
.AsImplementedInterfaces()
.WithScopedLifetime()
);
#endregion
#region Add MadiatR
builder.Services.AddMediatR(configuration =>
configuration.RegisterServicesFromAssembly(Application.AssemblyReference.Assembly));
#endregion #endregion
#region Link controllers with presentation layer #region Link controllers with presentation layer
var presentationAssembly = typeof(Presentation.AssemblyReference).Assembly;
builder.Services.AddControllers() builder.Services.AddControllers()
.AddApplicationPart(presentationAssembly); .AddApplicationPart(Presentation.AssemblyReference.Assembly);
#endregion #endregion
......
using MediatR;
namespace Application.Abstractions.CQRS.Commands;
// No response
public interface ICommand : IRequest
{
}
// With response
public interface ICommand<TResponse> : IRequest<TResponse>
{
}
using MediatR;
namespace Application.Abstractions.CQRS.Commands;
// No response
public interface ICommandHandler<TCommand> : IRequestHandler<TCommand>
where TCommand : ICommand
{
}
// With response
public interface ICommandHandler<TCommand, TResponse>
: IRequestHandler<TCommand, TResponse>
where TCommand : ICommand<TResponse>
{
}
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Application.Abstractions.CQRS.Queries
{
internal interface Interface1
{
}
}
using MediatR;
namespace Application.Abstractions.CQRS.Queries;
public interface IQueryHandler<TQuery, TResponse>
: IRequestHandler<TQuery, TResponse>
where TQuery : IQuery<TResponse>
{
}
...@@ -6,6 +6,10 @@ ...@@ -6,6 +6,10 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="MediatR" Version="12.4.0" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Domain\Domain.csproj" /> <ProjectReference Include="..\Domain\Domain.csproj" />
</ItemGroup> </ItemGroup>
......
using System.Reflection;
namespace Application;
public class AssemblyReference
{
public static readonly Assembly Assembly = typeof(AssemblyReference).Assembly;
}
using System.Reflection;
namespace Persistence;
public class AssemblyReference
{
public static readonly Assembly Assembly = typeof(AssemblyReference).Assembly;
}
namespace Presentation using System.Reflection;
namespace Presentation;
public class AssemblyReference
{ {
public class AssemblyReference public static readonly Assembly Assembly = typeof(AssemblyReference).Assembly;
{
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment