How can I log all of my validation classes using DryIoc dynamically?
I'm trying the following way down;
using DryIoc;
using FluentValidation;
using System;
using System.Linq;
using System.Reflection;
namespace TesteDryIoC.IU
{
class Program
{
private static Type[] GetTypesInNamespace()
{
return Assembly.GetExecutingAssembly().GetTypes().Where(t => String.Equals(t.Namespace, "TesteDryIoC.IU.Validator", StringComparison.Ordinal)).ToArray();
}
static void Main(string[] args)
{
var container = new Container();
var implementingClasses =
Assembly.GetExecutingAssembly()
.GetTypes().Where(type => String.Equals(type.Namespace, "TesteDryIoC.IU.Validator", StringComparison.Ordinal) &&
type.Name.EndsWith("Validator"));
foreach (var implementingClass in implementingClasses)
{
container.Register(implementingClass, Reuse.Singleton);
}
Cliente cliente = new Cliente();
var validador = new ProvaderValidator<Cliente>().GetValidator(container);
cliente.Nome = "";
cliente.SobreNome = "Souza";
cliente.Observacao = "teste";
cliente.Email = "[email protected]";
cliente.Idade = 32;
cliente.DtInclusao = DateTime.Now;
cliente.Ativo = true;
cliente.Senha = "";
var resultad0 = validador.Validate(cliente);
foreach (var item in resultad0.Errors)
{
Console.WriteLine(item.ErrorMessage);
}
Console.WriteLine("--------------------------------------");
Console.ReadKey();
}
}
}
My validation classes always end with Validator as shown below.
using FluentValidation;
namespace TesteDryIoC.IU.Validator
{
class ProdutoValidator : AbstractValidator<Produto>
{
public ProdutoValidator()
{
RuleFor(x => x.Nome)
// Garante que a propriedade especificada não seja nula
.NotNull().WithMessage("{PropertyName} deve ser preenchido");
}
}
}
Product class;
namespace TesteDryIoC.IU
{
public class Produto
{
public string Nome { get; set; }
}
}
Generic class to register.
using DryIoc;
using FluentValidation;
namespace TesteDryIoC.IU
{
public class ProvaderValidator<T> where T : class
{
public IValidator GetValidator(Container container)
{
var validador = container.Resolve<IValidator<T>>();
return validador;
}
}
}
The problem here is that when it enters the line var validador = container.Resolve<IValidator<T>>();
it returns the following error;
DryIoc.ContainerException: 'Unable to resolve IValidator
Where no service registrations found and no dynamic registrations found in 0 Rules.DynamicServiceProviders and nothing in 0 Rules.UnknownServiceResolvers'
The error in more detail;
DryIoc.ContainerException occurred HResult = 0x80131509
Message = Unable to resolve IValidator Where no service registrations found and no dynamic registrations found in 0 Rules.DynamicServiceProviders and nothing in 0 Rules.UnknownServiceResolvers Source = TestDryIoC.IU StackTrace:
at DryIoc.Throw.It (Int32 error, Object arg0, Object arg1, Object arg2, Object arg3) in C: \ Work \ SolutionTest \ TestDryIoC \ TestDryIoC.IU \ DryIcon \ Container.cs: line 11242 at DryIoc.Container.ThrowUnableToResolve (Request request) in C: \ Work \ SolutionTest \ TestDryIoC \ TestDryIoC.IU \ DryIcon \ Container.cs: line 893 at DryIoc.Container.DryIoc.IContainer.ResolveFactory (Request request) in C: \ Work \ SolutionTest \ TestDryIoC \ TestDryIoC.IU \ DryIcon \ Container.cs: line 851 at DryIoc.Container.ResolveAndCacheDefaultDelegate (Type serviceType, Boolean ifUnresolvedReturnDefault) in C: \ Work \ SolutionTest \ TestDryIoC \ TestDryIoC.IU \ DryIcon \ Container.cs: line 557 at DryIoc.Container.DryIoc.IResolver.Resolve (Type serviceType, Boolean ifUnresolvedReturnDefault) in C: \ Work \ SolutionTest \ TestDryIoC \ TestDryIoC.IU \ DryIcon \ Container.cs: line 475 at DryIoc.Resolver.Resolve [TService] (IRresolve Resolve, IfUnresolved ifUnresolved) in C: \ Work \ SolutionTest \ TestDryIoC \ TestDryIoC.IU \ DryIcon \ Container.cs: line 5665 at TestDryIoC.IU.ProvaderValidator'1.GetValidator (Container container) in C: \ Work \ SolutionTest \ TestDryIoC \ TestDryIoC.IU \ IProvaderValidator.cs: line 11 at TestDryIoC.IU.Program.Main (String [] args) in C: \ Work \ SolutionTest \ TestDryIoC \ TestDryIoC.IU \ Program.cs: line 31