我试图使用DryIOC解析一个作用域的实体框架上下文。虽然这个注册有效,但感觉很奇怪:
container.Register<MyDbContext>( Reuse.ScopedTo<IMarkerInterface>(), Made.Of(() => Arg.Of<IDbContextFactory<MyDbContext>>().CreateDbContext()) );特别是,Arg.Of的用途似乎是不同的。有更好的方法来编写Made部件吗?
我可以很容易地用(微软-DI-) ServiceCollection和AddDbContextFactory()注册一个工厂,所以我这样做了。然后,通过使用DryIOC扩展从ServiceCollection创建应用程序的其余部分使用的DryIoc.Microsoft.DependencyInjection容器。
在现在的DryIOC世界中,我想要一个限定范围的MyDbContext。因此,我使用MyDbContext注册ScopedTo,但是我希望每个上下文实例都由以前注册的工厂(在ServiceCollection世界中)生成。因此,DryIOC应该解析一个工厂,调用create并将结果放入当前范围,每次注入一个MyDbContext。
编辑:我已经升级到
container.Register<MyDbContext>( Reuse.ScopedTo<IMarkerInterface>(), Made.Of(container.Resolve<IDbContextFactory<MyDbContext>>(), factory => factory.CreateDbContext()) );这仍然是臭气熏天,因为Resolve是不必要的早期,但总感觉好一点。
container.Register<MyDbContext>( Reuse.ScopedTo<IMarkerInterface>(), Made.Of(() => container.Resolve<IDbContextFactory<MyDbContext>>(), factory => factory.CreateDbContext()) );我想是完美的,但不起作用(至少在这种语法中是这样的)。
发布于 2022-04-25 22:58:32
据我所知你需要这样的东西:
container.RegisterDelegate<IDbContextFactory<MyDbContext>, MyDbContext>(
factory => factory.CreateDbContext(),
Reuse.ScopedTo<IMarkerInterface>());https://stackoverflow.com/questions/71956113
复制相似问题