cqrs.
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
using FluentValidation;
|
||||
using PoweredSoft.CQRS.Abstractions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Demo.Commands
|
||||
{
|
||||
public class CreatePersonCommand
|
||||
{
|
||||
public string FirstName { get; set; }
|
||||
public string LastName { get; set; }
|
||||
}
|
||||
|
||||
public class CreatePersonCommandValidator : AbstractValidator<CreatePersonCommand>
|
||||
{
|
||||
public CreatePersonCommandValidator()
|
||||
{
|
||||
RuleFor(t => t.FirstName).NotEmpty();
|
||||
RuleFor(t => t.LastName).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class CreatePersonCommandHandler : ICommandHandler<CreatePersonCommand>
|
||||
{
|
||||
public Task HandleAsync(CreatePersonCommand command, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using FluentValidation;
|
||||
using PoweredSoft.CQRS.Abstractions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Demo.Commands
|
||||
{
|
||||
public class EchoCommand
|
||||
{
|
||||
public string Message { get; set; }
|
||||
}
|
||||
|
||||
public class EchoCommandValidator : AbstractValidator<EchoCommand>
|
||||
{
|
||||
public EchoCommandValidator()
|
||||
{
|
||||
RuleFor(t => t.Message).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class EchoCommandHandler : ICommandHandler<EchoCommand, string>
|
||||
{
|
||||
public Task<string> HandleAsync(EchoCommand command, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return Task.FromResult(command.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="FluentValidation.AspNetCore" Version="9.5.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="5.6.3" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="5.6.3" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="5.6.3" />
|
||||
|
||||
+16
-1
@@ -1,4 +1,7 @@
|
||||
using Demo.Commands;
|
||||
using Demo.Queries;
|
||||
using FluentValidation;
|
||||
using FluentValidation.AspNetCore;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.HttpsPolicy;
|
||||
@@ -30,15 +33,27 @@ namespace Demo
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
AddQueries(services);
|
||||
AddCommands(services);
|
||||
|
||||
services.AddPoweredSoftCQRS();
|
||||
services
|
||||
.AddControllers()
|
||||
.AddPoweredSoftQueryController();
|
||||
.AddPoweredSoftQueryController()
|
||||
.AddPoweredSoftCommandController()
|
||||
.AddFluentValidation();
|
||||
|
||||
services.AddSwaggerGen();
|
||||
}
|
||||
|
||||
private void AddCommands(IServiceCollection services)
|
||||
{
|
||||
services.AddCommand<CreatePersonCommand, CreatePersonCommandHandler>();
|
||||
services.AddTransient<IValidator<CreatePersonCommand>, CreatePersonCommandValidator>();
|
||||
|
||||
services.AddCommand<EchoCommand, string, EchoCommandHandler>();
|
||||
services.AddTransient<IValidator<EchoCommand>, EchoCommandValidator>();
|
||||
}
|
||||
|
||||
private void AddQueries(IServiceCollection services)
|
||||
{
|
||||
services.AddQuery<PersonQuery, IQueryable<Person>, PersonQueryHandler>();
|
||||
|
||||
Reference in New Issue
Block a user