0

我在asp.net mvc的使用簡單的注射用的NuGet 5生活方式不匹配的Web請求取決於過渡過程簡單噴油器

Install-Package SimpleInjector -Version 3.1.5 
Install-Package SimpleInjector.Integration.Web.Mvc -Version 3.1.5 

在Global.asax中

protected void Application_Start() 
     { 
      //removed for bravity 
      // Container container = new DependencyInjectionContiner().DI(); 
      var container = new Container(); 
      container.Options.DefaultScopedLifestyle = new WebRequestLifestyle(); 
      container.Register<IHomeRepository, HomeRepository>(Lifestyle.Scoped); 

      //For WebApi Request 
      // container.Register<IWebApi, WebApi>(Lifestyle.Transient); 
      container.RegisterMvcControllers(Assembly.GetExecutingAssembly()); 
      ////To verify life styles of injection 
      container.Verify(); 
      DependencyResolver.SetResolver(
       new SimpleInjectorDependencyResolver(container)); 
     } 

我表現出對

錯誤
The configuration is invalid. The following diagnostic warnings were reported: 

-[Lifestyle Mismatch] HomeRepository (Web Request) depends on UnitOfWork (Transient). 

IHomeRepository

public interface IHomeRepository 
    { 
     void InsertDepartment(Department department, List<Student> students); 
    } 

HomeRepository

public class HomeRepository : IHomeRepository 
    { 
     private UnitOfWork unitOfWork; 
     public HomeRepository(UnitOfWork unitOfWork) 
     { 
      this.unitOfWork = unitOfWork; 
     } 

     public void InsertDepartment(Department department, List<Student> students) 
     { 
      department.Students = students; 
      unitOfWork.DepartmentRepository.Insert(department); 
      unitOfWork.Save(); 
     } 
} 

IUnitOfWork

public class UnitOfWork : IDisposable 
    { 
     private SchoolContext context = new SchoolContext(); 
     private GenericRepository<Department> departmentRepository; 
     private GenericRepository<Student> studentRepository; 

     public GenericRepository<Department> DepartmentRepository 
     { 
      get 
      { 

       if (this.departmentRepository == null) 
       { 
        this.departmentRepository = new GenericRepository<Department>(context); 
       } 
       return departmentRepository; 
      } 
     } 

     public GenericRepository<Student> StudentRepository 
     { 
      get 
      { 

       if (this.studentRepository == null) 
       { 
        this.studentRepository = new GenericRepository<Student>(context); 
       } 
       return studentRepository; 
      } 
     } 

     public void Save() 
     { 
      context.SaveChanges(); 
     } 

     private bool disposed = false; 

     protected virtual void Dispose(bool disposing) 
     { 
      if (!this.disposed) 
      { 
       if (disposing) 
       { 
        context.Dispose(); 
       } 
      } 
      this.disposed = true; 
     } 

     public void Dispose() 
     { 
      Dispose(true); 
      GC.SuppressFinalize(this); 
     } 
    } 

GenericRepository

public class GenericRepository<TEntity> where TEntity : class 
    { 
     internal SchoolContext context; 
     internal DbSet<TEntity> dbSet; 

     public GenericRepository(SchoolContext context) 
     { 
      this.context = context; 
      this.dbSet = context.Set<TEntity>(); 
     } 

     public virtual IEnumerable<TEntity> Get(
      Expression<Func<TEntity, bool>> filter = null, 
      Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, 
      string includeProperties = "") 
     { 
      IQueryable<TEntity> query = dbSet; 

      if (filter != null) 
      { 
       query = query.Where(filter); 
      } 

      foreach (var includeProperty in includeProperties.Split 
       (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) 
      { 
       query = query.Include(includeProperty); 
      } 

      if (orderBy != null) 
      { 
       return orderBy(query).ToList(); 
      } 
      else 
      { 
       return query.ToList(); 
      } 
     } 

     //public IQueryable GetIQueryable() 
     //{ 
     // return context.Set<TEntity>().AsQueryable(); 
     //} 
     public IEnumerable<TEntity> Query(Expression<Func<TEntity, bool>> filter) 
     { 
      return context.Set<TEntity>().Where(filter); 
     } 
     public IEnumerable<TEntity> Query() 
     { 
      return context.Set<TEntity>(); 
     } 

     //public IQueryable Query() 
     //{ 
     // return context.Set<TEntity>(); 
     //} 
     public virtual TEntity GetByID(object id) 
     { 
      return dbSet.Find(id); 
     } 

     public virtual void Insert(TEntity entity) 
     { 
      dbSet.Add(entity); 
     } 

     public virtual void Delete(object id) 
     { 
      TEntity entityToDelete = dbSet.Find(id); 
      Delete(entityToDelete); 
     } 

     public virtual void Delete(TEntity entityToDelete) 
     { 
      if (context.Entry(entityToDelete).State == EntityState.Detached) 
      { 
       dbSet.Attach(entityToDelete); 
      } 
      dbSet.Remove(entityToDelete); 
     } 

     public virtual void Update(TEntity entityToUpdate) 
     { 
      dbSet.Attach(entityToUpdate); 
      context.Entry(entityToUpdate).State = EntityState.Modified; 
     } 
    } 

爲什麼驗證顯示錯誤?並且它是確定在單個容器使用瞬態作用域 ????

+0

異常消息包含此鏈接https://simpleinjector.org/diagnostics到文檔。您是否閱讀過這些文檔?閱讀本文檔後,您是否有任何疑問,特別是[此](https://simpleinjector.readthedocs.io/en/latest/LifestyleMismatches.html)? – Steven

+0

你的意思是說利用混合生活方式?如果是的話如何定義它? –

回答

1

異常消息指以下文檔頁:https://simpleinjector.org/diagnostics。該文檔指出以下有關生活方式不匹配:

一般而言,組件應該僅依賴於配置爲至少存活的其他組件。換句話說,暫態組件依賴於單例是安全的,但不是相反。由於部件存儲在(私有)的實例字段到它們的依賴性的引用,這些依賴於該組件的壽命保持存活。這意味着配置壽命比消費者更短的依賴關係意外地比預期壽命更長。這可能會導致各種錯誤,比如很難調試多線程問題。

的文檔還提供了一種用於修復了以下建議:

  • 更改組件的生活方式的生活方式比依賴性的短或更短。
  • 更改依賴性比成分的如長或更長生活方式的生活方式。

這意味着你要麼讓HomeRepository短暫的,或使UnitOfWork作用域。

使得UnitOfWork作用域是最明顯的,如this q&a討論。

你的意思是說化妝用的混合生活方式

你應該忘記混合動力車的生活方式。他們不提供你的問題的答案。

+0

有一個問題?我的知識庫使用WebRequestLife循環或瞬態生活方式可以嗎?據我瞭解,在IIS應用程序池中,Transient可能會耗費內存 –

+0

@syedmhamudulhasanakash:你在哪裏學習到瞬態可能會消耗內存? ASP.NET應用程序吐出大量內存,創建一些額外的存儲庫對性能來說是不可忽視的。你的'HomeRepository'的大小是20個字節。通常沒有什麼可擔心的。 – Steven

+0

所以你建議堅持'瞬態'的方式,我只是喜歡你的答案。謝謝男人 –

相關問題