Read metadata from your entities no matter how they were defined.
Escrutinador use IMetadataProvider 's implementation to read the metadata of your entities.
Nowadays the only available metadata provider is DataAnnotationsMetadataProvider.
But the idea is have a lot of IMetadataProvider tha allow define metadata using constants, database, xml files, fluent apis, etc.
PM> Install-Package Escrutinador
PM> Install-Package Escrutinador.Extensions.EntityFramework
PM> Install-Package Escrutinador.Extensions.KissSpecifications
For an entity, like MyEntity below:
public class MyEntity
{
[StringLength(50, MinimumLength = 10)]
[Required]
[Display(Order = 2)]
public string Text { get; set; }
}
You can read the metadata in this way:
var provider = EscrutinadorConfig.MetadataProvider;
var metadata = provider.Property<MyEntity>(p => p.Text);
Console.WriteLine("Name: {0}", metadata.Name);
Console.WriteLine("MinLength: {0}", metadata.MinLength);
Console.WriteLine("MaxLength: {0}", metadata.MaxLength);
Console.WriteLine("Order: {0}", metadata.Order);
Console.WriteLine("Required: {0}", metadata.Required);
The console output will be: Name: Text MinLength: 10 MaxLength: 50 Order: 2 Required: true
Escrutinador's extensions allow to extend the library to combine it with others libraries.
It has an EntityTypeConfiguration called MetadataEntityTypeConfiguration that allows auto map the EF entity using the information from metadata.
public class MyEntityMap : MetadataEntityTypeConfiguration<MyEntity>
{
public MyEntityMap()
{
this.ToTable("MyEntity");
this.MapMetadata(t => t.Text);
}
}
The line "this.MapMetadata(t => t.Text);" is equivalent to:
this.Property(t => t.Text).HasMaxLength(50).IsRequired();
This extension has a SpecificationBase called MustComplyWithMetadataSpecification that verify if the entity's state is complying with the metadata.
var entity = new MyEntity() { Text = "A" };
SpecificationService.ThrowIfAnySpecificationIsNotSatisfiedBy(
entity,
new MustComplyWithMetadataSpecification<MyEntity>());
The code above will throw a SpecificationNotSatisfiedException because the property Text with value "A" does not comply with MinLength metadata information.
- Ask on Stack Overflow
- Add new IMetadataProvider's implementation
- Add a fluent metadata definer.
- Create a fork of Escrutinador.
- Did you change it? Submit a pull request.
Licensed under the The MIT License (MIT). In others words, you can use this library for developement any kind of software: open source, commercial, proprietary and alien.