Skip to content

Instantly share code, notes, and snippets.

@sergiorgiraldo
Last active May 20, 2016 19:12
Show Gist options
  • Select an option

  • Save sergiorgiraldo/d017b2f82b5e678a6ec93b744f3aa0c5 to your computer and use it in GitHub Desktop.

Select an option

Save sergiorgiraldo/d017b2f82b5e678a6ec93b744f3aa0c5 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using Autofac;
using FluentValidation;
using FluentValidation.Results;
namespace ConsoleApplication1
{
internal class Program
{
private static void Main()
{
var sample1 = new Sample
{
Name = "Foo",
Country = "France",
Number = 10
};
var sample2 = new Sample
{
Name = "Bar",
Country = "Brazil",
Number = 10
};
var stuff1 = new Stuff
{
Name = "John Doe"
};
var stuff2 = new Stuff
{
Name = "John Doe I"
};
new Core().DoIt(new List<Sample> { sample1, sample2 }, new List<Stuff> { stuff1, stuff2 });
Console.ReadLine();
}
}
public class Core
{
private static Dictionary<Type, object> _validators = new Dictionary<Type, object>();
public Core()
{
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
var builder = new Autofac.ContainerBuilder();
builder.RegisterAssemblyTypes(assemblies)
.Where(t => t.IsAssignableTo<IValidator>())
.As<IValidator>();
var container = builder.Build();
foreach (var validator in container.Resolve<IEnumerable<IValidator>>())
{
var baseType = validator.GetType().BaseType;
if (baseType != null)
_validators.Add(baseType.GetGenericArguments()[0], validator);
}
}
private AbstractValidator<T> GetValidatorFor<T>(T entity) where T : IValidatable<T>
{
return (AbstractValidator<T>) _validators[entity.GetType()];
}
public void DoIt(List<Sample> samples, List<Stuff> stuffs)
{
foreach (var sample in samples)
{
ValidationResult results = sample.Validate(GetValidatorFor(sample));
foreach (var failure in results.Errors)
{
Console.WriteLine(failure.ErrorMessage);
}
}
foreach (var stuff in stuffs)
{
ValidationResult results = stuff.Validate(GetValidatorFor(stuff));
foreach (var failure in results.Errors)
{
Console.WriteLine(failure.ErrorMessage);
}
}
}
}
public class SampleValidator : AbstractValidator<Sample>
{
public SampleValidator()
{
RuleFor(smpl => smpl.Name).NotEmpty();
RuleFor(smpl => smpl.Name).NotEmpty().Length(3);
RuleFor(smpl => smpl.Country).Equal("France").WithMessage("Country must be France");
RuleFor(smpl => smpl.Number)
.Must(BePrime)
.WithMessage("Number {0} is not prime in {1}", smpl => smpl.Number, smpl => smpl.Descriptor);
}
private bool BePrime(int number)
{
var boundary = Math.Floor(Math.Sqrt(number));
if (number == 1) return false;
if (number == 2) return true;
for (var i = 2; i <= boundary; ++i)
{
if (number%i == 0) return false;
}
return true;
}
}
public class StuffValidator : AbstractValidator<Stuff>
{
public StuffValidator()
{
RuleFor(smpl => smpl.Name).NotEmpty().Length(10);
}
}
public interface IValidatable<T>
{
ValidationResult Validate(AbstractValidator<T> validator);
}
public class Sample : IValidatable<Sample>
{
public string Descriptor
{
get { return Guid.NewGuid().ToString(); }
}
public string Name { get; set; }
public string Country { get; set; }
public int Number { get; set; }
public ValidationResult Validate(AbstractValidator<Sample> validator)
{
return validator.Validate(this);
}
}
public class Stuff : IValidatable<Stuff>
{
public string Descriptor
{
get { return Guid.NewGuid().ToString(); }
}
public string Name { get; set; }
public ValidationResult Validate(AbstractValidator<Stuff> validator)
{
return validator.Validate(this);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment