C# - 使用 dapper 的存储库非常慢

问题描述 投票:0回答:1

我有一个小型测试项目来获取供应商的信息,例如零件和发票。 为此,我尝试使用存储库模式。但我不确定我是否正确使用它。

一切正常,但速度很慢。 我有几个课程,例如:

 public class SupplierPart
    {
        public int PartId { get; set; }
        public string PartNumber { get; set; } 
        public string PartName { get; set; }
...

对于每个类,我都会创建一个如下所示的接口:

 public interface ISupplierPartRepository
    {
        IEnumerable<SupplierPart> GetAllSupplierParts();
        IEnumerable<SupplierPart> GetAllSupplierPartsOfSupplier(int SupplierId);
        void UpdateSupplierPart(SupplierPart part);       
    }

然后是存储库:


 public class SupplierPartRepository : ISupplierPartRepository
    {

 public IEnumerable<SupplierPart> GetAllSupplierParts()
        {
            using (IDbConnection db = new MySqlConnection(AppConnection.ConnectionString))
            {
                string q = @"SELECT ...";
                
                return db.Query<SupplierPart>(q, commandType: CommandType.Text);
            }
        }

        public IEnumerable<SupplierPart> GetAllSupplierPartsOfSupplier(int SupplierId)
        {
            using (IDbConnection db = new MySqlConnection(AppConnection.ConnectionString))
            {
                string q = @"SELECT ...";

                return db.Query<SupplierPart>(q, new { SupplierId });
            }
        }

public void UpdateSupplierPart(SupplierPart part)
        {
            using (IDbConnection db = new MySqlConnection(AppConnection.ConnectionString))
            {
...
}
}

这就是我在表单中使用它的方式:

dataGridView1.DataSource = supplierPartRepository.GetAllSupplierParts().ToList();

Code_Actions.FillComboBox(combobox2, productGroupRepository.GetAllProductGroups(), "GroupName", "GroupId");
Code_Actions.FillComboBox(combobox3, partRepository.GetAllParts(), "Number", "Number");
Code_Actions.FillComboBox(combobox4, unitRepository.GetAllUnits(), "UnitText", "UnitId");

...

如果这样做的话会非常慢。如果我只使用填充 dataGridView1 的代码,速度会非常快。所以我认为这不是最好的方法?!

我可以做什么来提高性能?

c# repository repository-pattern
1个回答
0
投票

我希望问题是您一次创建了太多数据库连接,请检查此post。 尝试使用工作单元和存储库模式来解决这个问题。 良好的工作单元代码库

© www.soinside.com 2019 - 2024. All rights reserved.