LINQ 创建数据库管理器类的最佳实践是什么?

发布于 2024-12-08 22:19:59 字数 1063 浏览 0 评论 0原文

最好的性能和一致性是什么?

变体一(继承自DataContext):

public class MyDbManager : DataContext
{
    public MyDbManager(string conn):base(conn)
    {
        var tblMyTable1 = GetTable<MyTable1>();
        var tblMyTable2 = GetTable<MyTable2>();
    }
    public void GetEntityById(int id)
    {
        var myEntity = (from p in tblMyTable1 where p.Id == id  select p).FirstOrDefault();
    }
}

变体2(每次新连接时创建)

    public class MyDbManager
    {
        public string ConnectionString = "myConnStr";

        public List<Program> GetAllPrograms ()
        {
            var programs = new List<Program>();
            using (var db = new DataContext(ConnectionString))
            {
                var tblPrograms = db.GetTable<Program>();
                programs = (from p in tblPrograms select p).FirstOrDefault( .ToList());
            }
            return programs;
        }
}

或者也许你有第三种变体?非常感谢!

What is the best performance and consistency?

Variant one (inherit from DataContext):

public class MyDbManager : DataContext
{
    public MyDbManager(string conn):base(conn)
    {
        var tblMyTable1 = GetTable<MyTable1>();
        var tblMyTable2 = GetTable<MyTable2>();
    }
    public void GetEntityById(int id)
    {
        var myEntity = (from p in tblMyTable1 where p.Id == id  select p).FirstOrDefault();
    }
}

Variant 2 (Create each time new connection)

    public class MyDbManager
    {
        public string ConnectionString = "myConnStr";

        public List<Program> GetAllPrograms ()
        {
            var programs = new List<Program>();
            using (var db = new DataContext(ConnectionString))
            {
                var tblPrograms = db.GetTable<Program>();
                programs = (from p in tblPrograms select p).FirstOrDefault( .ToList());
            }
            return programs;
        }
}

Or maybe you have third variant? Thank you a lot!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

跨年 2024-12-15 22:19:59

为了保持一致性,我将采用存储库模式,为了性能我将采用工作单元模式。

您似乎使用第二种方法沿着这条路线走下去,但是,我会在数据库中为每个实体创建一个存储库,例如 Table1Repository/Table2Repository,并将每个实体的所有责任委托给适当的存储库....只是我的意见。

如果您查看第一种方法,您实际上并没有在本地正确缓存表,但我明白您要得到的是什么。因此,如果您考虑一下,尽管系统启动后速度可能会更快。缓存表中的每条记录真的很有效(或有必要)吗?在大多数情况下,您会查询诸如 Customer->Orders 之类的内容,因此如果 & 查询数据库肯定会更有意义。什么时候需要?因为不仅您的行程会更短,而且服务器上的密集程度也会降低。

DataContext 是轻量级的,在设计时就考虑到了这种方法。

For consistency, I would adopt the Repository Pattern and for performance I would adopt the Unit of Work pattern.

You appear to be kind of going down that route with your second approach, however, I would create a repository per entity in my database e.g. Table1Repository/Table2Repository, and delegate all responsibility for each entity to the appropriate repo....just my opinion.

If you look at your first approach, you aren't actually correctly caching the tables locally but I understand what it is you are getting at. So if you think about it, although it may be faster once your system is up & running is it really efficient (or necessary) to cache every record from a table? In most cases you would query things like Customer->Orders so surely it would make more sense to query the database if & when it is needed? As not only will your trip be shorter it will be less intensive on the server.

DataContext's are lightweight and were designed with this sort of approach in mind.

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