温莎城堡 - 解析时的默认界面

发布于 2024-12-10 06:15:06 字数 415 浏览 0 评论 0原文

我最近才开始使用温莎城堡,有一个问题。

假设我有一个接口 - IService - 并且有五个实现它的类(IServiceOne、IServiceTwo、IServiceThree)等等。

从那里我有实现每个 IService 的类。它们被适当地命名为 ServiceOneProvider、ServiceTwoProvider、ServiceThreeProvider 等。在这些类中,我正在解析 IService 对象。

所以我想知道的是 - 是否可以使用 DefaultInterfaces 的等效项(可以在注册期间使用)来解析 IService 对象,以便 ServiceOneProvider 将获得 ServiceOne 对象,ServiceTwoProvider 将获得 ServiceTwo 对象等等?

谢谢,

-罗斯

I've only just started using Castle Windsor recently and have a question.

Suppose I have an interface- IService - and I have five classes that implement it (IServiceOne, IServiceTwo, IServiceThree) and so on.

From there I have classes that implmenent each of the IServices. They are appropriately named as ServiceOneProvider, ServiceTwoProvider, ServiceThreeProvider etc. Within these classes I am resolving an IService object.

So what I want to know is - Is it possible to have that IService object resolved using the equivalent of DefaultInterfaces (which can be used during registration) such that ServiceOneProvider will get a ServiceOne object, a ServiceTwoProvider will get a ServiceTwo object and so on?

Thanks,

-Ross

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

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

发布评论

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

评论(1

鹿童谣 2024-12-17 06:15:06

我没有看到 Windsor 内置任何东西来做类似的事情。我虽然有一个 HandlerSelector,但是当选择器运行时,您不知道请求依赖项的类型,因此这没有帮助。

您可以使用 TypedFactoryFacility。只需创建一个接口来返回您的 IService 对象(C# 版本):

public interface IProviderFactory
{
    IService GetServiceOne();
    IService GetServiceTwo();
    IService GetServiceThree();
}

然后将您的 IService 实例命名为“ServiceOne”、“ServiceTwo”等...

Component.For<IService>().ImplementedBy<ServiceOne>().Named("ServiceOne"),
Component.For<IService>().ImplementedBy<ServiceTwo>().Named("ServiceTwo"),
Component.For<IService>().ImplementedBy<ServiceThree>().Named("ServiceThree"),

然后使您的提供程序依赖于工厂并调用适当的“Get”方法:

public class ServiceOneProvider
{
    public ServiceOneProvider(IProviderFactory factory)
    {
        var service = factory.GetServiceOne();
    }
}

public class ServiceTwoProvider
{
    public ServiceTwoProvider(IProviderFactory factory)
    {
        var service = factory.GetServiceTwo();
    }
}

public class ServiceThreeProvider
{
    public ServiceThreeProvider(IProviderFactory factory)
    {
        var service = factory.GetServiceThree();
    }
}

I didn't see anything built in to Windsor to do something like that. I though about a HandlerSelector, but when the selector is running, you don't know the type that is requesting the dependency so that wouldn't help.

You could use the TypedFactoryFacility. Just create an interface to return your IService objects (C# version):

public interface IProviderFactory
{
    IService GetServiceOne();
    IService GetServiceTwo();
    IService GetServiceThree();
}

Then name your IService instances as "ServiceOne", "ServiceTwo", etc...

Component.For<IService>().ImplementedBy<ServiceOne>().Named("ServiceOne"),
Component.For<IService>().ImplementedBy<ServiceTwo>().Named("ServiceTwo"),
Component.For<IService>().ImplementedBy<ServiceThree>().Named("ServiceThree"),

Then make your providers dependent on the factory and call the appropriate "Get" method:

public class ServiceOneProvider
{
    public ServiceOneProvider(IProviderFactory factory)
    {
        var service = factory.GetServiceOne();
    }
}

public class ServiceTwoProvider
{
    public ServiceTwoProvider(IProviderFactory factory)
    {
        var service = factory.GetServiceTwo();
    }
}

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