Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在具体类实现相同接口/服务的情况下编写服务定位器模式?_C#_Design Patterns_Service Locator - Fatal编程技术网

C# 如何在具体类实现相同接口/服务的情况下编写服务定位器模式?

C# 如何在具体类实现相同接口/服务的情况下编写服务定位器模式?,c#,design-patterns,service-locator,C#,Design Patterns,Service Locator,考虑下面的问题 class ServiceA : IServiceA { public void SayHelloFromA() { Console.WriteLine("Hello Service A"); Console.ReadKey(); } } class ServiceB : IServiceB{ } class ServiceC : IServi

考虑下面的问题

class ServiceA : IServiceA
    {
        public void SayHelloFromA()
        {
            Console.WriteLine("Hello Service A");
            Console.ReadKey();
        }
    }    
    class ServiceB : IServiceB{ } 
    class ServiceC : IServiceC{ }
    interface IServiceA 
    { 
        void SayHelloFromA(); 
    }
    interface IServiceB{ }
    interface IServiceC{ }
如果我想使用服务定位器模式,那么所提供的示例非常有效

现在假设另一个类实现IServiceA接口,如下所示

class ServiceA1 : IServiceA
{
    public void SayHelloFromA()
    {
        Console.WriteLine("Hello Service A1");
        Console.ReadKey();
    }
} 
相应地,我需要将服务添加到字典中,如下所示

internal ServiceLocator()    
        {        
            services = new Dictionary<object, object>();         

            this.services.Add(typeof(IServiceA), new ServiceA());
            this.services.Add(typeof(IServiceA), new ServiceA1());     
            this.services.Add(typeof(IServiceB), new ServiceB());        
            this.services.Add(typeof(IServiceC), new ServiceC());    
        } 
混凝土工厂来源于IBaseCustomer


谢谢

您是否考虑过按嵌套在顶级字典中的具体类型为每个抽象类型键入字典

Dictionary<T, Dictionary<T, U>>
如果返回true,则您知道componentDictionary将包含实现该服务的组件:

foreach (c in componentDictionary.Values) {... }

通过巧妙地使用抽象基类型和泛型,您可以创建一个比这更强类型的解决方案。但是,这应该可以很好地工作,尽管是在强制转换中。

在Java中,有一个多映射类,它支持每个键的多个值。我相信您也可以为C#找到类似的数据结构。

如何检索特定的实现?服务定位器的全部要点是在不知道是哪个的情况下为您提供一个IServiceA的实现。在您的示例中,如何调用服务定位器以获得特定的实现?(假设您可以有一个带有副本的键控容器)我已经更新了答案,进一步演示了这个概念。基本上,您的服务定位器需要一个标识映射来保存/解析组件到服务,但它不是JDK的一部分。如果必须使用纯java,请将Map与
List
一起用作值。更多信息:
var services = new Dictionary<Type, Dictionary<Type, Object>>();
services.TryGetValue(typeof(IServiceA), componentDictionary);
foreach (c in componentDictionary.Values) {... }