c# – Unity PerThreadLifetimeManager和Tasks

前端之家收集整理的这篇文章主要介绍了c# – Unity PerThreadLifetimeManager和Tasks前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用EntityFramework并在一堆后台作业类中实现通用存储库和工作单元模式.使用Unity DI创建作业类,以便可以向它们注入依赖项,这些依赖项主要是存储库和UnitOfWork对象.
存储库和工作单元应共享EF DbContext.

一个常见的工作看起来像这样:

  1. public class CommonJob : IJob,IDisposable
  2. {
  3. private IRepo<SomeEntity> _repo;
  4. private IUnitOfWork _uow;
  5.  
  6. public CommonJob(IRepo<SomeEntity> repo,IUnitOfWork uow)
  7. {
  8. _repo = repo;
  9. _uow = uow;
  10. }
  11.  
  12. public void RunJob()
  13. {
  14. // do stuff here
  15. }
  16.  
  17. public void Dispose()
  18. {
  19. _uow.Commit();
  20. _uow.Dispose();
  21. }
  22. }

所有工作都在新任务中运行,就像这样

  1. Task.Factory.StartNew(() => {
  2. // container is UnityContainer
  3. var job = container.Resolve<CommonJob>();
  4. job.RunJob();
  5. job.Dispose();
  6. });

我使用PerThreadLifetimeManager在Unity中注册了工作单元和存储库,认为这样可以在一个任务(以及一个作业对象)的上下文中共享已注册的实例,但不能在外部共享.

我遇到的问题是,有时工作会注入被处理的对象,这显然不是很好.我一直在读,Task.Factory.StartNew()并不总是使用新线程.这是否意味着PerThreadLifetimeManager将在任务之间共享对象?如果这是真的,有没有另一种方法统一管理对象生命周期,这将允许每个任务孤立地工作,无论它运行的线程是什么?

编辑:

虽然下面选择的答案将实现相同的目的,但我最终使用HierarchicalLifetimeManager和子容器来实现每个作业的依赖性隔离.

这是一个例子:

  1. // registering the dependencies,// these should be singletons,but only within the context of one job
  2. _container.Register(typeof(IRepo<>),typeof(Repo<>),new HierarchicalLifetimeManager())
  3. .Register<IUnitOfWork,UnitOfWork>(new HierarchicalLifetimeManager());
  4.  
  5. // starting a new job
  6. Task.Factory.StartNew<IUnityContainer>(() =>
  7. {
  8. // create a child container to remove instance sharing between tasks
  9. var childContainer = _container.CreateChildContainer();
  10.  
  11. // figure out and resolve the job class from the child container
  12. // this will make sure that different jobs do not share instances
  13. var jobType = GetJobType();
  14. var job = childContainer.Resolve(jobType) as IJob;
  15.  
  16. job.RunJob();
  17.  
  18. return childContainer;
  19. }).ContinueWith(prevIoUsTask => {
  20. // when the job is done,dispose of the child container
  21. task.Result.Dispose();
  22. });

解决方法

由于并行库使用线程池,并且Unity为池的同一线程返回相同的对象,因此您将获得处置对象.

如果您按照发布的方式使用容器,我建议您使用PerResolveLifetimeManager.这样,当您解析一个对象时,整个分辨率图共享同一个实例,但每个分辨率的实例都是唯一的:每个任务都有自己的实例它叫Resolve.

猜你在找的C#相关文章