question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RFC: De-Couple data access from EF

See original GitHub issue

Problem

Currently, the controller leans on IEntityRepository directly which is tightly coupled to IQueryable. However, this doesn’t make much sense for ORMs, like Dapper, that do not use IQueryable. Also, it is entirely possible that resources may be retrieved from non SQL storage solutions, such as another HTTP API or RPC.

Tightly Coupled Areas:

Proposed Solution

  1. Extract controller logic into IResourceService. Reduces controller methods to something like the following. Would allow any intermediate service to perform the data access.
public class JsonApiController<T, TId>
    : JsonApiControllerMixin where T : class, IIdentifiable<TId>
{
    private readonly IResourceService<T, TId> _service;

    public JsonApiController(IResourceService<T, TId> resourceService)
    {
        _service = resourceService;
    }

    [HttpGet]
    public virtual async Task<IActionResult> GetAsync()
    {
        var entities = await _service.GetAsync();
        return Ok(entities);
    }
}
  1. Refactor the context graph:
  • Rename ContextGraphBuilder to EntityContextGraphBuilder, since it uses the EF DbContext.
  • Can ContextGraph<T> be used without the DbContext generic? If not, perform rename Entity...
  • Define API to either inject custom IContextGraph implementations or an API to build a generalized context graph
  1. Refactor service extensions or add methods for using the new APIs defined by (2)

Considerations

  • Need to take a closer look at json:api operations and how this would fit in.
  • Since this may introduce a variety of breaking changes, perhaps we should consider further de-coupling from the MVC middleware in this solution as well (see discussion in #71)?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
JanMattnercommented, Apr 7, 2017

Sounds good. In order to decouple it completely, we must get rid of the DbContext generic type. So rather something like:

services.AddJsonApi(opt => {
  opt.BuildContextGraph(builder => {
     builder.WithDbContext<AppDbContext>();
     builder.WithResource<MyResource>("my-resources");
  });
});

=> Everything inside the DbContext can be added and further more custom ressources not bound to any DbContext. For backwards compatibility, we can still leave the currently public methods as is and call this builder internally.

1reaction
donniefitz2commented, Apr 6, 2017

Been getting familiar with the project and that’s one of the first things that came to mind. Can I use this with a custom business/data layer? That would be really nice.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Entity Framework generated classes in Business ...
Without the decoupling, the EF objects can become deeply embedded in the BLL and even presentation layers, requiring a huge refactoring.
Read more >
EF Core: Effectively decouple the data and domain model
In this post, I'll be expressing my views on the clear separation of persistence related models and business models and look into how...
Read more >
RFC 2764 - A Framework for IP Based Virtual Private ...
RFC 2764 IP Based Virtual Private Networks February 2000 2.1.2 Data Security In general customers using VPNs require some form of data security....
Read more >
Implementing the infrastructure persistence layer with ...
Entity Framework (EF) Core is a lightweight, extensible, and cross-platform version of the popular Entity Framework data access technology. It ...
Read more >
c# - Where should helper classes/methods for transforming ...
I have created a Business Logic Layer (BLL) and a Data Access Layer (DAL) (in folders/namespaces in the same project, not as class...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found