Entity Framework Core 2.0 CRUD best practices / automation

asp.net-core c# entity-framework-core

Question

Are there established best practices for a CRUD in EF Core 2.0?

Is there any way to automatically generate CRUDs for the models, preferably using these best practices?

As an example, this is what I have currently, but I have to replicate it to something like 20 other entities:

[Route("entities")]
public class EntitiesController : Controller
{
    private readonly ProjectContext _context;

    public EntitiesController(ProjectContext context)
    {
        _context = context;
    }

    [HttpPost]
    public async Task<IActionResult> Post([FromBody]Entities entity)
    {
        _context.Add(entity);
        return Json(await _context.SaveChangesAsync() >= 1);
    }

    [HttpGet]
    public JsonResult Get(int? entityId)
    {
        if (entityId == null)
            return Json(_context.Entities.Select(x => x));
        else
            return Json(_context.Entities.FirstOrDefault(x => x.EntityId == entityId));
    }

    [HttpPut]
    public async Task<IActionResult> Put([FromBody]Entities entity)
    {
        if (entity.EntityId < 1)
            return BadRequest();

        _context.Entities.Update(entity);
        return Json(await _context.SaveChangesAsync() >= 1);
    }

    [HttpDelete]
    public async Task<IActionResult> Delete(int entityId)
    {
        if (entityId < 1)
            return BadRequest();

        Entities entity = _context.Entities.Where(x => x.EntityId == entityId).FirstOrDefault();
        if (entity == null)
            return BadRequest();

        _context.Entities.Remove(entity);
        return Json(await _context.SaveChangesAsync() >= 1);
    }
}
1
2
12/21/2017 12:40:23 PM

Popular Answer

There is an insightful article in Micorosoft's website. In this article, they have defined some of the best practices. For example there is a controversial debate about using DbContext directly or a repository and they have addressed this issue as follows:

The Entity Framework DbContext class is based on the Unit of Work and Repository patterns, and can be used directly from your code, such as from an ASP.NET Core MVC controller. That is the way you can create the simplest code, as in the CRUD catalog microservice in eShopOnContainers. In cases where you want the simplest code possible, you might want to directly use the DbContext class, as many developers do.

However, implementing custom repositories provides several benefits when implementing more complex microservices or applications. The Unit of Work and Repository patterns are intended to encapsulate the infrastructure persistence layer so it is decoupled from the application and domain model layers. Implementing these patterns can facilitate the use of mock repositories simulating access to the database.

So, as the first step maybe you should change the way you are using Dbcontext and use repositories instead of that. There are many more points and hints in this article.

5
1/16/2018 5:33:32 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow