You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
二、我们经常的代码都是分层的,有可能到处都在 new DbContext(options),这是就要面对如何管理这些DbContext,在AspNetCore中 services.AddDbContext<>默认是用的Scope的作用域,也就是每次HttpRequest,比以前好了很多。但是事务这些管理还是很麻烦。
publicstaticclassUnitOfWorkMiddleWare{publicstaticvoidUseUnitOfWork<TContext>(thisIPipeConfigurator<TContext>configurator,IUnitOfWorkunitOfWork=null)whereTContext:IContext<IMessage>{if(unitOfWork==null&&configurator.DependencyScope==null){thrownewDependencyScopeNotConfiguredException($"{nameof(unitOfWork)} is not provided and IDependencyScope is not configured, Please ensure {nameof(unitOfWork)} is registered properly if you are using IoC container, otherwise please pass {nameof(unitOfWork)} as parameter");}unitOfWork??=configurator.DependencyScope.Resolve<IUnitOfWork>();configurator.AddPipeSpecification(newUnifyOfWorkSpacification<TContext>(unitOfWork));}}