Code Monkey home page Code Monkey logo

escrutinador's Introduction

Escrutinador

Read metadata from your entities no matter how they were defined.


Metadata provider

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.

Setup

PM> Install-Package Escrutinador
PM> Install-Package Escrutinador.Extensions.EntityFramework
PM> Install-Package Escrutinador.Extensions.KissSpecifications

Usage

Reading metadata from DataAnnotation

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

Extensions

Escrutinador's extensions allow to extend the library to combine it with others libraries.

Escrutinador.Extensions.EntityFramework

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();

Escrutinador.Extensions.KissSpecifications

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.

FAQ

Having troubles?

Roadmap

  • Add new IMetadataProvider's implementation
  • Add a fluent metadata definer.

How to improve it?

License

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.

escrutinador's People

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.