What is the equivalent code for the asp.net core DI framework from this example using Ninject?

本秂侑毒 提交于 2019-12-11 02:32:33

问题


I've been looking at using CQS pattern with EF Core within an asp.net Core web application.

I found this sample, which seems to be what I want however the DI container used is Ninject.

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();

I have gotten this far:

services.AddTransient<IQueryFactory>(qf => new QueryFactory(q => q));

But I'm not sure how to actually do this part in asp.net core di container:

Container.Current.Resolve(x)

回答1:


It would seem I was overthinking it and I have resolved my problem.

services.AddTransient<IQueryFactory>
    (serviceProvider => new QueryFactory(serviceProvider.GetService));


来源:https://stackoverflow.com/questions/56260643/what-is-the-equivalent-code-for-the-asp-net-core-di-framework-from-this-example

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!