Autofac基于TypedParameter的不同服务解析

发布于 2024-09-13 08:06:35 字数 1843 浏览 3 评论 0原文

无论如何,有没有办法让 autofac 来解析具有具体实现的服务,该服务的构造函数与最具体地匹配给定参数。

我的意思是匹配子类型的构造函数而不是匹配基类型的构造函数。

例如,我想进行以下测试,

[TestFixture]
public class AutofacResolvTestFixture
{
    [Test]
    public void test()
    {
        var cb = new ContainerBuilder();
        cb.RegisterType<Child1>();
        cb.RegisterType<Child2>();
        cb.RegisterType<ServiceImpl1>().As<IService>();
        cb.RegisterType<ServiceImpl2>().As<IService>();
        var container = cb.Build();
        var c1=container.Resolve<Child1>();
        var c2=container.Resolve<Child2>();
        var s1 = container.Resolve<IService>(new TypedParameter(c1.GetType(), c1));
        var s2 = container.Resolve<IService>(new TypedParameter(c2.GetType(), c2));
        Assert.That(s1, Is.InstanceOf<ServiceImpl1>());
        Assert.That(s2, Is.InstanceOf<ServiceImpl2>());
        Assert.That(s1.ConstructorParam, Is.SameAs(c1));
        Assert.That(s2.ConstructorParam, Is.SameAs(c2));
    }
}

public class baseClass {}
public class Child1 : baseClass{}
public class Child2 : baseClass{}

public interface IService
{
    baseClass ConstructorParam { get; }
}

public class ServiceImpl1 : IService
{
    private baseClass _b;
    public ServiceImpl1(baseClass b)
    {
        _b = b;
    }

    public baseClass ConstructorParam
    {
        get
        {
            return _b;
        }
    }
}


public class ServiceImpl2 : IService
{
    private baseClass _b;
    public ServiceImpl2(Child2 b)
    {
        _b = b;
    }

    public baseClass ConstructorParam
    {
        get
        {
            return _b;
        }
    }
}

我的目标是创建 IService、IEditor,并能够通过将 IEditor 传递给 autofac 来解析类型。我想这样做,以便我可以通过简单地继承 IEditor 并使用 IEditor 所属的适当构造函数类型来“覆盖”类型的编辑器。

谢谢。

Is there anyway to get autofac to resolve the service that has a concrete implementation with the constructor that matches a given parameter most specifically.

By this i mean match a constructor for a child type instead of matching the constructor for the base type.

For example i'd like to make the following test pass

[TestFixture]
public class AutofacResolvTestFixture
{
    [Test]
    public void test()
    {
        var cb = new ContainerBuilder();
        cb.RegisterType<Child1>();
        cb.RegisterType<Child2>();
        cb.RegisterType<ServiceImpl1>().As<IService>();
        cb.RegisterType<ServiceImpl2>().As<IService>();
        var container = cb.Build();
        var c1=container.Resolve<Child1>();
        var c2=container.Resolve<Child2>();
        var s1 = container.Resolve<IService>(new TypedParameter(c1.GetType(), c1));
        var s2 = container.Resolve<IService>(new TypedParameter(c2.GetType(), c2));
        Assert.That(s1, Is.InstanceOf<ServiceImpl1>());
        Assert.That(s2, Is.InstanceOf<ServiceImpl2>());
        Assert.That(s1.ConstructorParam, Is.SameAs(c1));
        Assert.That(s2.ConstructorParam, Is.SameAs(c2));
    }
}

public class baseClass {}
public class Child1 : baseClass{}
public class Child2 : baseClass{}

public interface IService
{
    baseClass ConstructorParam { get; }
}

public class ServiceImpl1 : IService
{
    private baseClass _b;
    public ServiceImpl1(baseClass b)
    {
        _b = b;
    }

    public baseClass ConstructorParam
    {
        get
        {
            return _b;
        }
    }
}


public class ServiceImpl2 : IService
{
    private baseClass _b;
    public ServiceImpl2(Child2 b)
    {
        _b = b;
    }

    public baseClass ConstructorParam
    {
        get
        {
            return _b;
        }
    }
}

My goal is to make IService, IEditor and be able to resolve the IEditor for a type by passing it to autofac. I want to do this so that i can "override" the editor for a type by simply inheriting IEditor and using the appropriate constructor type that that IEditor is for.

Thank you.

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

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

发布评论

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

评论(1

唐婉 2024-09-20 08:06:35

看看我对这个问题的解决方案。也许您可以使用相同的解决方案,使用 Func 工厂委托,并将 ServiceImplX Keyed 指定为相应的子类型,而不是使用命名注册。

Take a look at my solution to this question. Perhaps you can use the same solution, with a Func<Type, IService> factory delegate instead, and have ServiceImplX Keyed to the corresponding child type instead of using Named registrations.

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