使用存储库和依赖注入获取记录需要一些时间

Fetching records taking some time using repository and dependency injection

我正在创建 1 个演示应用程序来研究存储库和依赖项注入。

我的数据库表中只有 10 到 15 条记录,读取它需要花费一些时间,我不知道 why.is 因为存储库或依赖项注入??

所以如果您对我的代码有任何建议或改进,请指导我

这是我的项目架构:

1) 数据 Model:This 项目包含我的 EDMX 文件(数据模型是数据库优先方法)。

public partial class RepositoryDemoEntities : DbContext
    {
        public RepositoryDemoEntities ()
            : base("name=RepositoryDemoEntities ")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }
        public virtual DbSet<Employee> Employee { get; set; }
    }

2) 存储库:

public class EfRepository<T> : IRepository<T> where T : class
    {
        private System.Data.Entity.DbSet<T> _entities;
        private RepositoryDemoEntities _context;
        public EfRepository()
        {
            this._context = new RepositoryDemoEntities();
            this._entities = this._context.Set<T>();
        }

        public T GetById(object id)
        {
            _context.Configuration.AutoDetectChangesEnabled = false;
            return this._entities.Find(id);
        }
        public IQueryable<T> Table
        {
            get
            {
                return this._entities;
            }
        }
      }
    //And other methods....


public interface IRepository<T> where T : class
    {
        T GetById(object id); 
        void Insert(T entity);
       IQueryable<T> Table { get; }
     }

3)服务:

public partial interface IEmployeeService
    {
            IList<Employee> GetAllEmployees();
    }

public partial class EmployeeService : IEmployeeService
{
       private readonly IRepository<Employee> _employeeMasterRepository;
      public EmployeeService(IRepository<Employee> employeeMasterRepository)
    {
        this._employeeMasterRepository = employeeMasterRepository;
    }

     public virtual IList<Employee> GetAllEmployees()
    {
        var query = _employeeMasterRepository.Table.ToList();
        return query;
    }

4)我的 Mvc 项目:

 public class HomeController : Controller
    {
           private readonly IEmployeeService _employeeService;
           public HomeController(IEmployeeService employeeService)
        {
            this._employeeService = employeeService;
        }

public ActionResult Index()
        {
          var data=_employeeService.GetAllEmployees() //Taking Some amount of time
        }
    }

autofac 配置中的依赖注入:

 public static class AutofacConfig
    {
        public static void RegisterComponents()
        {
            var builder = new ContainerBuilder();
              builder.RegisterGeneric(typeof(EfRepository<>)).As(typeof(IRepository<>)).InstancePerLifetimeScope();

 builder.RegisterType<EmployeeService>().As<IEmployeeService>().InstancePerLifetimeScope();

}

我的Global.asax:

  protected void Application_Start()
        {
          AutofacConfig.RegisterComponents();
        }

如果不看视图,很难说您的延迟在哪里。有几件事需要考虑:

  • DI在这里几乎没有消耗时间,所以不要怪它
  • 使用 ORM 时,请始终将其与分析器配对,例如来自 Hibernating Rhinos 或 DevArt 的 EF Profiler。查看 SQL 被触发的查询以避免常见问题
  • 典型问题是 SELECT N+1 问题,当你延迟加载你的关系时,例如,员工记录视图中的每一行都会触发一堆查询以获取相关元素(profiler 会显示这个)
  • 您注册了 PerLifetimeScope,而对于 MVC,我强烈建议您将 DAL 依赖项注册为 PerRequest。在这种情况下,您的数据库会话将在请求关闭时终止,并且任何延迟加载将立即显示(空数据或因空引用异常而崩溃)
  • Entity Framework 这个 DbContext 黑盒子让我很困扰。将它与 NHibernate 进行比较,在 NHibernate 中我们有一个 ISessionFactory,创建起来很昂贵,但您在应用程序启动时只做一次,还有一个轻量级的 ISession,创建起来很便宜,并且按请求注册。