首页 > 解决方案 > DryIoc中的autofacs“WithParameter”等价于什么?

问题描述

为了从 Autofac 迁移到 DryIoc,我遇到了一个场景,我需要将参数值传递给服务的构造函数。

// given the following class
public class SomeService
{
    public SomeService(Foo foo, Bar bar) { }
}
// in autofac it looks like this
container.RegisterType<SomeService>()
         .WithParameter("foo", SomeFoo.Value)
         .WithParameter("bar", SomeBar.Value)
         .AsSelf();

DryIoc 中的等价物是什么?

现在我正在尝试使用RegisterDelegate,但我不确定我是否走在正确的道路上。

container.RegisterDelegate(x => new SomeService(SomeFoo.Value, SomeBar.Value));

标签: dryioc

解决方案



container.Register<SomeService>(
made: Made.Of(Parameters.Of
.Name("foo", _ => SomeFoo.Value)
.Name("bar", _ => SomeBar.Value)));

以下是可能的重载:https ://www.fuget.org/packages/DryIoc.dll/4.2.0/lib/netstandard2.0/DryIoc.dll/DryIoc/Parameters

有一个类似的Parameters PropertiesAndFields类来指定属性注入细节。

此外还有一个 DryIoc.Syntax.Autofac 包,您可能对https://www.fuget.org/packages/DryIoc.Syntax.Autofac.dll感兴趣

从 V1 开始,它仅涵盖 Autofac API 表面的一小部分,但我鼓励您查看它的源代码,并可能有助于 PR ot 两个 :) https://github.com/dadhi/DryIoc/tree/master/src /DryIoc.Syntax.Autofac


推荐阅读