C# - EF 存储库

发布于 2024-10-26 15:36:46 字数 1454 浏览 3 评论 0原文

我已经为我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

缺⑴份安定 2024-11-02 15:36:46

EF 是一个 ORM,并且已经实现了存储库模式,完全没有必要将其包装在您自己的存储库中。

EF is an ORM and already implements the Repository pattern, there is absolutely no need to wrap it in your own repository.

墨小沫ゞ 2024-11-02 15:36:46

您需要在每个存储库中至少访问同一组两次,
看看这两种解决方案的作用是否真的相同。

在第二次调用时检查分析器并可能对数据进行更改
通过另一个上下文,在访问之间,以确保行为是相同的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文