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 project is tightly coupled to
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)
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}
GetsBlog
by id -
POST /api/blogs
with provided dto as parameter -
PUT /api/blogs/{id}
with provided dto as parameter -
DELETE /api/blogs/{id}
For complete setup inspiration, please visit example/CrudMaker.TestHost
.
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");
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
...
// Add CrudMaker OData support
app.UseCrudMakerOData();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
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 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 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();
}
}
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();
}
}
The MIT License