Skip to content

Commit

Permalink
Added validation example
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmwebb-lv committed May 8, 2024
1 parent 026feee commit d0a0630
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Examples.Validation.FluentValidation
{
internal static class ConfigurationContainer
{
public static IConfiguration Configuration { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\Src\RCommon.FluentValidation\RCommon.FluentValidation.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using RCommon.ApplicationServices.ExecutionResults;
using RCommon.ApplicationServices.Validation;

namespace Examples.Validation.FluentValidation
{
public interface ITestApplicationService
{
Task<ValidationOutcome> ExecuteTestMethod(TestDto dto);
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,51 @@
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
using Examples.Validation.FluentValidation;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using RCommon;
using RCommon.ApplicationServices;
using RCommon.ApplicationServices.ExecutionResults;
using RCommon.FluentValidation;
using System.Diagnostics;

try
{
var host = Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((context, builder) =>
{
ConfigurationContainer.Configuration = builder
.Build();
})
.ConfigureServices(services =>
{
// Configure RCommon
services.AddRCommon()
.WithValidation<FluentValidationBuilder>(validation =>
{
validation.AddValidatorsFromAssemblyContaining(typeof(TestDto));
});
Console.WriteLine(services.GenerateServiceDescriptorsString());
services.AddTransient<ITestApplicationService, TestApplicationService>();
}).Build();

Console.WriteLine("Example Starting");
var appService = host.Services.GetRequiredService<ITestApplicationService>();
var validationOutcome1 = await appService.ExecuteTestMethod(new TestDto("")); // Will fail
var validationOutcome2 = await appService.ExecuteTestMethod(new TestDto("test")); // Will pass

Console.WriteLine(validationOutcome1.ToString());
Console.WriteLine("Validation Outcome 1 complete...");
Console.WriteLine(validationOutcome2.ToString());
Console.WriteLine("Validation Outcome 2 complete...");

Console.WriteLine("Example Complete");
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using RCommon.ApplicationServices.Commands;
using RCommon.ApplicationServices.ExecutionResults;
using RCommon.ApplicationServices.Queries;
using RCommon.ApplicationServices.Validation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Examples.Validation.FluentValidation
{
public class TestApplicationService : ITestApplicationService
{
private readonly IValidationService _validationService;

public TestApplicationService(IValidationService validationService)
{
_validationService = validationService;
}

public async Task<ValidationOutcome> ExecuteTestMethod(TestDto dto)
{
return await _validationService.ValidateAsync(dto);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Examples.Validation.FluentValidation
{
public record TestDto
{
public TestDto(string message)
{
Message = message;
}

public string Message { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
using System.Text;
using System.Threading.Tasks;

namespace Examples.ApplicationServices.CQRS.Validators
namespace Examples.Validation.FluentValidation.Validators
{
public class TestCommandValidator : AbstractValidator<TestCommand>
public class TestDtoValidator : AbstractValidator<TestDto>
{
public TestCommandValidator()
public TestDtoValidator()
{
RuleFor(command => command.Message).NotNull().NotEmpty();
}
Expand Down

0 comments on commit d0a0630

Please sign in to comment.