gpt4 book ai didi

c# - Unity DI 使用 PerRequestLifetimeManager 注入(inject) DbContext

转载 作者:行者123 更新时间:2023-11-30 14:10:33 29 4
gpt4 key购买 nike


我有以下使用 Unity 初始化实例的代码:

IUnityContainer container = new UnityContainer();
container.RegisterType<DbContext, VotingSystemContext>(new PerRequestLifetimeManager(), new InjectionConstructor());
container.RegisterType(typeof(IGenericRepository<>), typeof(GenericRepository<>));
container.RegisterType<IUnitOfWork, UnitOfWork>(new PerRequestLifetimeManager());
container.RegisterTypes(
AllClasses.FromAssemblies(
Assembly.GetAssembly(typeof(IUserService)),
Assembly.GetAssembly(typeof(UserService))),
WithMappings.FromMatchingInterface,
WithName.Default, WithLifetime.PerResolve);
DependencyResolver.SetResolver(new Unity.Mvc4.UnityDependencyResolver(container));
GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);

我使用 PerRequestLifetimeManager 所以我遵循了 MSDN 上的建议并在上面代码的末尾添加新行:

DynamicModuleUtility.RegisterModule(typeof(UnityPerRequestHttpModule));

但是在我放置它之后。加载页面(仅静态 html)时,我向调用 GenericReposirory Get() 方法的 WebApi Controller 发送 ajax 请求,该方法抛出错误:操作不能已完成,因为 DbContext 已被释放。
没有这行代码,一切正常,但如果不设置它,上下文可能不会被处理。
我的 UnitOfWork 类:

public class UnitOfWork : IUnitOfWork, IDisposable
{
private readonly VotingSystemContext _context;
private bool _disposed;

//GenericRepository properties

private void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
_context.Dispose();
}
}
_disposed = true;
}

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

附言我使用的是最新版本的 Unity 3.5.1404
提前致谢。

编辑:

Repository的Get()方法:

public sealed class GenericRepository<TEntity> : IGenericRepository<TEntity> where TEntity : BaseEntity
{
public GenericRepository(VotingSystemContext context)
{
_context = context;
_dbSet = context.Set<TEntity>();
}

private readonly DbSet<TEntity> _dbSet;
private readonly VotingSystemContext _context;

public IEnumerable<TEntity> Get(Expression<Func<TEntity, bool>> filter = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
string includeProperties = "", int? page = null, int? pageSize = null)
{
IQueryable<TEntity> query = _dbSet;
if (filter != null)
{
query = query.Where(filter);
}
List<string> properties = includeProperties.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
properties.ForEach(property =>
{
query = query.Include(property);
});
if (orderBy != null)
{
query = orderBy(query);
}
if (page != null && pageSize != null)
{
query = query.Skip((page.Value - 1) * pageSize.Value).Take(pageSize.Value);
}
return query;
}
// other methods like Delete, Update and GetById
}
}

ApiController 的 Get() 方法:

public IEnumerable<VotingModel> Get(int page = 1, int size = 10)
{
//get all themes
List<Theme> themes = _themeService.GetAll(page, size);
//convert themes to VotingModel (same model as Theme just without converting system throw an error about serializing object and also add new filed UserName).
List<VotingModel> model = themes.Select(t =>
{
MembershipUser membershipUser = Membership.GetUser(t.UserId ?? -1);
return t.ToVotingModel(membershipUser != null ? membershipUser.UserName : string.Empty);
}).ToList();
return model;
}

服务 GetAll() 方法:

public List<Theme> GetAll(int page = 1, int pageSize = 10)
{
return UnitOfWork.ThemeRepository.Get(null, null, "Comments", page, pageSize).ToList();
}

最佳答案

所以我会有这样的依赖结构:

  • UnitOfWork - 获取 DbContext
  • Repository - 获取 UnitofWork
  • 服务 - 获取存储库
  • ApiController - 获取服务

并且您会坚持使用 Unity 来处理每个对象的生命周期。但问题是,您希望服务具有请求范围,就像其他服务(UoW 和 Repos)一样。您可能会以这种方式设置服务生命周期,但我并不知道 Unity。我可以看到您确实设置了 UofW 和 repos 以及请求生命周期。

最大的区别在于 UnitOfWork不依赖于存储库,而是相反。所以存储库基类得到它的 DbSet<T>通过 UnitOfWork其中有 DbContext.你会在 UnitOfWork 上有一些方法这将返回 IDbSet<T>就像你在 DbContext. 上调用它一样UnitOfWork作为 DbContext 的包装器这本身就很像工作单元。

public sealed class GenericRepository<T> : IRepository<T> where T : BaseEntity
{
private readonly IDbSet<T> _dbSet;
private readonly IUoW _uoW;

public GenericRepository(IUoW unitOfWork)
{
_uoW = unitOfWork;
_dbSet = _uoW.Set<T>();
}

public IEnumerable<TEntity> Get(Expression<Func<TEntity, bool>> filter = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
string includeProperties = "", int? page = null, int? pageSize = null)
{
IQueryable<TEntity> query = _dbSet;
if (filter != null)
{
query = query.Where(filter);
}
List<string> properties = includeProperties.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
properties.ForEach(property =>
{
query = query.Include(property);
});
if (orderBy != null)
{
query = orderBy(query);
}
if (page != null && pageSize != null)
{
query = query.Skip((page.Value - 1) * pageSize.Value).Take(pageSize.Value);
}
return query;
}
// other methods like Delete, Update and GetById
}

UnitOfWork将是相似的,但采取 DbContext作为依赖项(你可能已经有了这个但省略了构造函数):

public class UnitOfWork : IUnitOfWork
{
private readonly VotingSystemContext _context;
private bool _disposed;

public UnitOfWork(DbContext context)
{
_context = context;
}

public IDbSet<T> Set<T>()
{
return _context.Set<T>();
]
}

该服务将注入(inject)存储库:

public class ThemeService
{
private IRepository<Theme> ThemeRepository { get; set; }

public ThemeService(IRepository<Theme> themeRepo)
{
ThemeRepository = themeRepo;
}

public List<Theme> GetAll(int page = 1, int pageSize = 10)
{
return ThemeRepository.Get(null, null, "Comments", page, pageSize).ToList();
}

// etc.
}

ApiController将注入(inject)所需的服务,在本例中为 ThemeService :

public class ApiController ThemeController
{
private ThemeService _themeService;

public ThemeController(ThemeService service) // along with any other needed services
{
_themeService = service;
}

public IEnumerable<VotingModel> Get(int page = 1, int size = 10)
{
//get all themes
List<Theme> themes = _themeService.GetAll(page, size);
//convert themes to VotingModel (same model as Theme just without converting system throw an error about serializing object and also add new filed UserName).
List<VotingModel> model = themes.Select(t =>
{
MembershipUser membershipUser = Membership.GetUser(t.UserId ?? -1);
return t.ToVotingModel(membershipUser != null ? membershipUser.UserName : string.Empty);
}).ToList();
return model;
}

最终的想法是 Unity 容器处理所有依赖项的生命周期和 UnitOfWork不必尝试管理存储库。你的线路

DynamicModuleUtility.RegisterModule(typeof(UnityPerRequestHttpModule));

会留下来,DbContext会被 Unity 处理掉,你不必调用 Dispose()自己动手。

关于c# - Unity DI 使用 PerRequestLifetimeManager 注入(inject) DbContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23430377/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com