Streaming Results from Entity Framework Core and Web API Core – Part 2

Full source code available here.

Some time ago I wrote a post showing how to stream results from Entity Framework over Web API. This approach a few benefits – the results would not be materialized in the API code, a small amount of memory would be used irrespective of the size of the data returned, the results would being steaming as soon as possible and the speed of the request was faster than doing something like .ToList() or .ToListAsync().

In the example code I directly accessed the DbContext from the API controller, a reader of the blog got in touch to ask the database access code could be kept away from the API controller. This blog post shows a simple way of achieving this.

In the related post I had a method like this –

1[HttpGet("streaming/")]
2public IActionResult GetStreaming()
3{
4    IQueryable<Product> products = _salesContext.Products.AsNoTracking();
5    return Ok(products);
6}

The _salesContext was passed into the constructor of the ProductsController class using dependency injection and access directly.

In this post I’m going to pass a ProductsService into the ProductsController and use it to make the request to the database.

Here is how the controller now looks –

 1[Route("api/[controller]")]
 2[ApiController]
 3public class ProductsController : ControllerBase
 4{
 5    private readonly ProductsService _productsService;
 6 
 7    public ProductsController(ProductsService productsService)
 8    {
 9        _productsService = productsService;
10    }
11 
12    [HttpGet("streamingFromService/{count}")]
13    public ActionResult GetStreamingFromService(int count)
14    {
15        return Ok(_productsService.GetStreaming(count));
16    }
17 
18    [HttpGet("streamingFromServiceWithProjection/{count}")]
19    public ActionResult GetStreamingFromServiceWithProjection(int count)
20    {
21        return Ok(_productsService.GetStreamingWithProjection(count));
22    }
23 
24    [HttpGet("nonStreamingFromService/{count}")]
25    public async Task<ActionResult> GetNonStreamingFromService(int count)
26    {
27        return Ok(await _productsService.GetNonStreaming(count));
28    }
29}

The ProductService is very simple, it makes the relevant calls to the database and returns the Products or ProductModels. For good measure I’ve added Automapper to project the Product to ProductModel, separating the underlying data type from the one presented to the caller.

 1public class ProductsService
 2{
 3    private readonly SalesContext _salesContext;
 4    private readonly IMapper _mapper;
 5 
 6    public ProductsService(SalesContext salesContext, IMapper mapper)
 7    {
 8        _salesContext = salesContext;
 9        _mapper = mapper;
10    }
11 
12    public IQueryable<Product> GetStreaming(int count)
13    {
14        IQueryable<Product> products = _salesContext.Products.OrderBy(o => o.ProductId).Take(count).AsNoTracking();
15        return products;
16    }
17 
18    public IQueryable<ProductModel> GetStreamingWithProjection(int count)
19    {
20        var productModels = _mapper.ProjectTo<ProductModel>(_salesContext.Products.OrderBy(o => o.ProductId).Take(count).AsNoTracking());
21        return productModels;
22    }
23 
24    public async Task<IList<Product>> GetNonStreaming(int count)
25    {
26        IList<Product> products = await _salesContext.Products.OrderBy(o => o.ProductId).Take(count).ToListAsync();
27        return products;
28    }
29}

Now, some people might be concerned about returning an IQueryable, if you are, see this post by Mark Seemann to start digging into the topic.

While writing this I came across what seems like a severe performance bug in Entity Framework Core 3.x, I’m working on another post which will cover this is detail.

Full source code available here.

comments powered by Disqus

Related