In many basic examples I see the using block wrapped around DbContext
usage like so:
using (var context = new MyDbContext())
{
// Perform data access using the context
}
It makes sense, because a 'new' instance is being created, so you want to dispose of this once you are finished with it.
Using DI
However in a number of projects I am working with, I am seeing DbContext
being injected into repositories and service layers like this:
public class FileRequestService : IFileRequestService
{
private readonly MyDbContext _myDbContext;
public FileRequestService(MyDbContext myDbContext)
{
_myDbContext = myDbContext;
}
public FileRequest SaveFileRequest(FileRequest fileRequest)
{
fileRequest.Status = FileRequestStatus.New;
//...
//...
var fr = _myDbContext.FileRequests.Add(fileRequest);
_myDbContext.SaveChanges();
return fr;
}
}
And configured in the DI container like this:
container.Options.DefaultScopedLifestyle = new WebRequestLifestyle();
container.Register<MyDbContext>(Lifestyle.Singleton);
Question 1
Is it OK that there is no using statement being used here, as it is presumably being disposed of once the web request dies?
Using DI/UoW
Similar scenario for the unit of work pattern, I am seeing this:
public class RecordController : Controller
{
private readonly IUnitOfWork _unitOfWork;
public RecordController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
[HttpPost, ActionName("Index")]
public PartialViewResult Search(SearchQueryViewModel searchQueryViewModel)
{
var deptId =
_unitOfWork.DepartmentRepository.Get(x => x.DepartmentCode == searchQueryViewModel.code)
.Select(s => s.DepartmentId)
.FirstOrDefault();
//...
}
}
Which is configured in the container like so:
container.Register<IUnitOfWork, UnitOfWork>(Lifestyle.Scoped);
container.Register<IGenericRepository<Department>, GenericRepository<Department>>(Lifestyle.Scoped);
And DbContext
is being injected into the constructor of the UoW class as normal.
Question 2
Again is it OK that there is no using statement being used here or should I implement the IDisposable
interface on the UoW class and do something like this:
using (_unitOfWork)
{
var deptId =
_unitOfWork.DepartmentRepository.Get(x => x.DepartmentCode == searchQueryViewModel.code)
.Select(s => s.DepartmentId)
.FirstOrDefault();
//...
}