So I've run into a bit of a roadblock, albeit a small one. I think I have a leaky abstraction similar to the answer below:
I'm using the abstract factory pattern to create factories of repositories. This gives me the ability to keep DBContext light-weight and still allow me to call multiple methods in repository before I call SaveChanges()
. The advantage of this is I can batch multiple commands before saving it.
using System.Collections.Generic;
namespace RouteMiningBLL
{
public class ZIPCodeInfoService
{
private readonly IRouteMiningRepositoryFactory factoryRepository;
public ZIPCodeInfoService(IRouteMiningRepositoryFactory factoryRepository)
{
this.factoryRepository = factoryRepository;
}
public IList<ZIPCodeInfo> GetAllZIPCodeInfo(int zipcode, int radius)
{
using (IZIPCodeInfoRepository repo = factoryRepository.CreateZIPCodeInfoRepository())
{
List<ZIPCodeInfo> zipCodeInfos = new List<ZIPCodeInfo>();
// Get all zipcodes within radius of zipcode from site
ZIPCodePage page = new ZIPCodePage();
IEnumerable<int> zipCodes = page.GetZIPCodes(zipcode, radius);
foreach (int foundZipcode in zipCodes)
{
zipCodeInfos.Add(GetZIPCodeInfo(foundZipcode));
}
repo.SaveChanges();
return zipCodeInfos;
}
}
}
}
Above is my current implementation, but it requires IRouteMiningRepositoryFactory to implement IDisposable
which obviously is a leak abstraction. What I'm trying to figure out is how to handle this?
Prior to this I just disposed of the DBContext inside the factory. However, this requires me to create a new DBContext with each Repository method call (not a big issue at all), but this then forces me to call dbContext.SaveChanges()
at the end of every repository method (therefore, not letting me batch work). I'm stuck on how to handle Leaky abstraction vs Batching.
In either case I will have a leaky abstraction because DBContext needs to be disposed of. It just changes where it happens (BLL vs DAL). Thanks!