GithubHelp home page GithubHelp logo

alveflo / crudmaker Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 0.0 54 KB

Generate your crud endpoints automatically :rocket: More popular than a pair of socks!

License: MIT License

C# 100.00%
dotnet-core aspnetcore odata efcore automapper fluentvalidation

crudmaker's Introduction

๐Ÿš€ CrudMaker

Description

CrudMaker is intended to potentially save time and energy by automatically generating CRUD-endpoints for given entities. It has a tight coupling to OData, Entity Framework Core, AutoMapper and Fluentvalidation in order to achive rich queryablity, persistance, mapping entities to dto's and building business logic into operations by utilizing validators.

โš ๏ธ This repository is in an experimental stage

Dependencies

This project is tightly coupled to

How about...

Can I use my own controllers alongside this library?

Yes, you can! If you'd like to use CrudMaker to generate crud operations for e.g. a Blog entity and also write your own controller for a Post entity with more complex business logic into it, that's totally fine.

CrudMaker doesn't include relations when getting data

That's easily fixed by implementing a custom repository (shown below)

Usage

When setting up a CRUD-operation (as below) with CrudMaker you'll get the following endpoints:

services.AddCrud<TestDbContext>(options =>
{
    options.Add<BlogDto, Blog>("/blogs");
});
  • GET /api/blogs Querying blogs by using OData, e.g.:

    • GET /api/blogs?$select=Property1, Property2$top=10$skip=10

    (This will ofcourse return a projection to the provided dto rather than the database entity to not expose sensitive data)

  • GET /api/blogs/{id} Gets Blog by id

  • POST /api/blogs with provided dto as parameter

  • PUT /api/blogs/{id} with provided dto as parameter

  • DELETE /api/blogs/{id}

Setup

For complete setup inspiration, please visit example/CrudMaker.TestHost.

Configuration

Startup service collection configuration

public void ConfigureServices(IServiceCollection services)
{
    // An EF Core database context is required
    services.AddDbContext<TestDbContext>(...);
    // AutoMapper is required
    services.AddAutoMapper(typeof(AutoMapperProfile));

    services.AddControllers();

    // Endpoint routing is not supported due to OData configurations
    services.AddMvc(option => option.EnableEndpointRouting = false)
        // Fluent validation (aspnet core style) is recommended
        .AddFluentValidation(...);

    services.AddCrud<TestDbContext>(options =>
    {
        // Adds CRUD endpoints for Post operations on /posts
        options.Add<PostDto, Post>("/posts");

        // Adds CRUD endpoints for Blog operations on /blogs
        // with custom repository.
        options.Add<BlogDto, Blog, BlogRepository>("/blogs");
    });
}

Startup application builder configuration

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    ...
    // Add CrudMaker OData support
    app.UseCrudMakerOData();
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });
}

Entities and dtos

Both entities and dtos needs to be implemented using the CrudMaker.IIdentity interface in order to identify entities

public class Blog : IIdentity
{
    public Guid Id { get; set; }
    ...
}

public class BlogDto : IIdentity
{
    public Guid Id { get; set; }
    ...
}

Custom repositories

Custom repositories is implemented by implementing the interface CrudMaker.Abstractions.IRepository<TEntity> and configured when configuring your service collection

services.AddCrud<TestDbContext>(options =>
{
    options.Add<BlogDto, Blog, BlogRepository>("/blogs");
});

Validation

Validation of incoming dto's for POST and PUT is achieved by implementing FluentValidation validators for the entities. This is not required but it's highly recommended.

public class BlogDtoValidator : AbstractValidator<BlogDto>
{
    public BlogDtoValidator()
    {
        RuleFor(x => x.Property).NotEmpty();
    }
}

Entity<->Dto mapping

Mapping is done using AutoMapper and thus need to be defined both ways in your auto mapper profile.

public class AutoMapperProfile : Profile
{
    public AutoMapperProfile()
    {
        CreateMap<BlogDto, Blog>()
            .ReverseMap();
    }
}

License

The MIT License

crudmaker's People

Contributors

alveflo avatar

Stargazers

Daniel Olsson avatar Martin Fylke avatar Sebastian Appler avatar

Watchers

James Cloos avatar  avatar

crudmaker's Issues

Only require reference of CrudMaker in client/presentation

Currently I need to reference CrudMaker in all projects where I have my DTOs and entites to be able to put the IIdentity interface.
In my case when using clean architecture it means I need to reference it to both my Domain- and Application-project which will cause dependency for CrudMaker in my Core-layer.

It would be awesome to reference CrudMaker only at the client/presentation-layer :)

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.