Reputation: 9377
I've made up 2 possible ways for how my EF implementation should interact with my app. The first one is a simple Repository, and the second is a rather dynamic version. Both of those solutions produce the same sql queries according to the sql profiler - so nothing more, nothing less.
Now is my question, is there any perfomance overhead for solution 1 compared to solution 2? - and if yes, does it matter in the long run?
Solution 1:
public class Repository
{
public MyContext Ctx = new MyContext();
public IEnumerable<T> GetAll<T>() where T : class
{
return Ctx.Set<T>();
}
}
... And the code that calls it:
var rep = new Repository();
Console.WriteLine("DEALERS:");
foreach(var dealer in rep.GetAll<Dealer>())
{
Console.WriteLine(dealer.Name);
}
Console.WriteLine("CARS:");
foreach (var car in rep.GetAll<Car>())
{
Console.WriteLine(car.CarName);
}
Solution 2:
public class Repository<T> where T : class
{
private readonly MyContext _ctx = new MyContext();
private readonly IDbSet<T> _dbset;
public Repository()
{
_dbset = _ctx.Set<T>();
}
public IEnumerable<T> GetAll()
{
return _dbset;
}
}
... And the code that calls it:
var dealerRep = new Repository<Dealer>();
var carRep = new Repository<Car>();
Console.WriteLine("DEALERS:");
foreach(var dealer in dealerRep.GetAll())
{
Console.WriteLine(dealer.Name);
}
Console.WriteLine("CARS:");
foreach (var car in carRep.GetAll())
{
Console.WriteLine(car.CarName);
}
Upvotes: 0
Views: 512
Reputation: 18068
You need to access the same set at least twice in each repository, to see if the two solutions really act the same.
Check profiler on 2nd call and perhaps make changes to data via another context, between accesses, to make sure behavior is the same.
Upvotes: 0
Reputation: 499002
EF is an ORM and already implements the Repository pattern, there is absolutely no need to wrap it in your own repository.
Upvotes: 1