C# - EF 存储库
我已经为我的 EF 实现如何与我的应用程序交互制定了 2 种可能的方法。第一个是简单的存储库,第二个是相当动态的版本。这两种解决方案都会根据 sql 探查器生成相同的 sql 查询 - 因此仅此而已。
现在我的问题是,与解决方案 2 相比,解决方案 1 是否有任何性能开销? - 如果是的话,从长远来看这重要吗?
解决方案 1:
public class Repository
{
public MyContext Ctx = new MyContext();
public IEnumerable<T> GetAll<T>() where T : class
{
return Ctx.Set<T>();
}
}
... 以及调用它的代码:
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);
}
解决方案 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;
}
}
... 以及调用它的代码:
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);
}
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
EF 是一个 ORM,并且已经实现了存储库模式,完全没有必要将其包装在您自己的存储库中。
EF is an ORM and already implements the Repository pattern, there is absolutely no need to wrap it in your own repository.
您需要在每个存储库中至少访问同一组两次,
看看这两种解决方案的作用是否真的相同。
在第二次调用时检查分析器并可能对数据进行更改
通过另一个上下文,在访问之间,以确保行为是相同的。
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.