题:
当实体中的直接数据访问需要 0.4 秒时,为什么将列表从受保护的内部类传递到 API 方法然后返回到我的 UI 需要 4 秒?是因为通过这些方法实际传递了列表吗?
背景:
我希望创建一个“API”层,它位于我使用实体框架的数据访问层和我的 UI 层之间。这个想法是限制对 CRUD 操作的访问并通过 API 强制执行所有操作,但是我注意到性能很差。
当我在这里使用我的类结构时,这种情况下的 get 方法需要4 秒才能运行:
public class API
{
DataAccessClass _dataAccess = new DataAccessClass();
public List<Items> GetById(int id)
{
return _dataAccess.Get(id);
}
protected internal class DataAccessClass
{
protected internal List<Items> GET(int id)
{
using (var context = dbcontext)
{
return context.GetItems();
}
}
protected internal List<Items> GET(long id)
{
using (var context = dbcontext)
{
return context.GetItems();
}
}
}
}
但是,当我直接在代码中使用我的 dbcontext 时(我想阻止),它使用在上面受保护的类中找到的相同代码在0.4 秒内运行:
using (var context = dbcontext)
{
return context.GetItems();
}
编辑:
当我排除 API 的数据访问部分(即受保护的内部部分)并直接在 API 中运行 using 语句(只是切出受保护的内部部分)时,我得到了可接受的 0.4 秒。
POPMUISE
相关分类