本文介绍了使用Ninject的本示例中的asp.net核心DI框架的等效代码是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在考虑在asp.net Core Web应用程序中将CQS模式与EF Core一起使用.
I've been looking at using CQS pattern with EF Core within an asp.net Core web application.
我找到了这个样本,这似乎是我想要的,但是使用的DI容器是Ninject.
I found this sample, which seems to be what I want however the DI container used is Ninject.
我似乎无法将Ninject配置转换为asp.net核心中的内置DI容器.
I can't seem to be able to translate the Ninject configuration into the inbuilt DI container in asp.net core.
具体来说,我的问题是这些行:
Specifically my problem is with these lines:
Bind<IQueryFactory>().ToMethod(t => new QueryFactory(x => Container.Current.Resolve(x))).InTransientScope();
Bind<ICommandsFactory>()
.ToMethod(t => new CommandFactory(x => (object[]) Container.Current.ResolveAll(x)))
.InTransientScope();
我已经走了这么远:
services.AddTransient<IQueryFactory>(qf => new QueryFactory(q => q));
但是我不确定如何在asp.net核心di容器中实际执行此部分:
But I'm not sure how to actually do this part in asp.net core di container:
Container.Current.Resolve(x)
推荐答案
似乎我想得太多,并且已经解决了我的问题.
It would seem I was overthinking it and I have resolved my problem.
services.AddTransient<IQueryFactory>
(serviceProvider => new QueryFactory(serviceProvider.GetService));
这篇关于使用Ninject的本示例中的asp.net核心DI框架的等效代码是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!