我在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;
}
}
爲什麼驗證顯示錯誤?並且它是確定在單個容器使用瞬態和作用域 ????
異常消息包含此鏈接https://simpleinjector.org/diagnostics到文檔。您是否閱讀過這些文檔?閱讀本文檔後,您是否有任何疑問,特別是[此](https://simpleinjector.readthedocs.io/en/latest/LifestyleMismatches.html)? – Steven
你的意思是說利用混合生活方式?如果是的話如何定義它? –