1
\$\begingroup\$

I normally abstract data access code into another layer, but I'm currently prototyping and it's not necessary. Does anyone see any issues with this as a general data query pattern, considering the queries may contain very large amounts of data (which will be consumed by an Angular 2 front end). I just want to be sure my implementation doesn't have any gaping holes regarding the async await pattern, and the context doesn't need to be disposed (normally I would wrap a using around it, but I don't think its necessary here).

IDbContextScopeFactory is a neat EF abstraction detailed here. I may swap it out for the IAmbientDbContextLocator detailed in the blog at a later date, should I need to.

Data.Person is the EF entity. Person is the API layer POCO. I'm using AutoMapper to map between them (rules omitted as they're not relevant here). This code works, but I don't have the tools to generate a mass of data to benchmark it with.

public class PersonController : ApiController {
    private readonly IDbContextScopeFactory _dbFactory;

    public PersonController(IDbContextScopeFactory dbFactory) {
        _dbFactory = dbFactory;
    }

    [HttpGet]
    public async Task<IEnumerable<Person>> Get() {
        using(var scope = _dbFactory.Create()) {
            using(var ctx = scope.DbContexts.Get<Data.MyContext>()) {
                var data = await ctx.Set<Data.Person>()
                    .ToArrayAsync();

                return data
                    .Select(Mapper.Map<Person>);
            }
        }
    }
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.