I am beginning a new project and am wanting to leverage off of the async/await items in .NET along with the new Async operations in entity framework (version 6.1). However, what I am curious about is how to refactor certain parts of my application to reduce code repetition and the best way to accomplish this given the non-thread-safe nature of EF.
Example, I have a few methods in my service layer that look something like this
public async Task<MyViewModel> GetProgramIndexAsync(long programId){
using (var db = new DbEntities())
{
var program = await db.Programs.FirstOrDefaultAsync(x => x.Id == programId && x.Active);
if (program == null)
{
throw new ApplicationException("Cannot find the requested program");
}
//more code to populate the view model
}
}
So far, so good. But I find myself doing this check in multiple places in this service layer. What would be ideal would be to extract that to a separate function
private async Task<Program> GetProgramAsync(long programId){
using (var db = new DbEntities())
{
var program = await db.Programs.FirstOrDefaultAsync(x => x.Id == programId && x.Active);
if (program == null)
{
throw new ApplicationException("Cannot find the requested program");
}
return program;
}
}
Now, my public service layer calls can simply call this function and not have to repeat this check each time.
var program = await GetProgramAsync(programId);
My concern is the spinning up of multiple contexts for each one of these requests. In the past (synchronous), I could simply have a second function parameter such as
private async Task<Program> GetProgramAsync(long programId, DbContext db){
and pass the existing context into the method. However, I am assuming this would be bad with the async/await nature.
Am I over thinking this (multiple contexts are not that bad) or is there a better way of accomplishing this? I should note that these calls are read only and I will not be updating entities in this manner.