Unity 在正在解析的属性的构造函数中注入参数

发布于 2024-12-09 10:51:26 字数 951 浏览 0 评论 0原文

我找不到在这种情况下注入参数的方法:

class Top
{
    private ISome some;
    public Top()
    {
        some = CreateSome(localRuntimeVariable);
    }

    //I need to pass "some" instance as a InjectionParameter to Child constructor
    [Dependency]
    public Child Child {get;set;}
}
class Child
{
    //I need to inject ISome but it can only be constructed in Top
    public Child(ISome some, Foo foo)
    {
    }
}
public class Usage
{
    private void Top GetTop(Foo foo)
    {
        return unity.Resolve<Top>(new DependencyOverride<Foo>(foo));
        //I expect: Top.Constuctor called and 'some' is assigned;
        //          Top.Child property beeing resolved: Child.Constructor called
        //          'foo' instance to be taken from unity.Resolve<Top>(new DependencyOverride<Foo>(foo));
        //          'some' instance to be taken from Top.some, but how to tell unity to inject it?
    }
}

I can't find a way to inject parameters in this scenario:

class Top
{
    private ISome some;
    public Top()
    {
        some = CreateSome(localRuntimeVariable);
    }

    //I need to pass "some" instance as a InjectionParameter to Child constructor
    [Dependency]
    public Child Child {get;set;}
}
class Child
{
    //I need to inject ISome but it can only be constructed in Top
    public Child(ISome some, Foo foo)
    {
    }
}
public class Usage
{
    private void Top GetTop(Foo foo)
    {
        return unity.Resolve<Top>(new DependencyOverride<Foo>(foo));
        //I expect: Top.Constuctor called and 'some' is assigned;
        //          Top.Child property beeing resolved: Child.Constructor called
        //          'foo' instance to be taken from unity.Resolve<Top>(new DependencyOverride<Foo>(foo));
        //          'some' instance to be taken from Top.some, but how to tell unity to inject it?
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

︶ ̄淡然 2024-12-16 10:51:26
class Top
{
    public Top( Foo foo, IUnityContainer container )
    {
        some = CreateSome(localRuntimeVariable);
        Child = container.Resolve<Child>(new ParameterOverride("some" some),
            new ParameterOverride("Foo", foo));
    }

    public Child Child {get;private set;}
}

class Child
{
    public Child(ISome some, Foo foo)
    {
    }
}

现在,您可以使用 unity.Resolve(new ParameterOverride("Foo", foo)) 解析 top 的实例,

不需要 Usage 类。 GetTop(Foo foo) 只是 unity.Resolve(new DependencyOverride(foo)) 的语法糖

class Top
{
    public Top( Foo foo, IUnityContainer container )
    {
        some = CreateSome(localRuntimeVariable);
        Child = container.Resolve<Child>(new ParameterOverride("some" some),
            new ParameterOverride("Foo", foo));
    }

    public Child Child {get;private set;}
}

class Child
{
    public Child(ISome some, Foo foo)
    {
    }
}

Now you can resolve an instance of top using unity.Resolve<Top>(new ParameterOverride("Foo", foo))

The class Usage isn't needed. GetTop(Foo foo) is just syntactic sugar for unity.Resolve<Top>(new DependencyOverride<Foo>(foo))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文