简体   繁体   English

如何在Factory中重载泛型C#方法?

[英]How to overload generic C# methods in Factory?

I want to create something like a factory, but it is not an implementation of the factory pattern. 我想创建类似工厂的东西,但它不是工厂模式的实现。

I have an IServiceFactory interface: 我有一个IServiceFactory接口:

public interface IServiceFactory
{
    TServiceInterface CreateService<TServiceInterface>(string url);
}

I want to create into my implementation two different services which implement two different interfaces. 我想在我的实现中创建两个不同的服务,实现两个不同的接口。 Let think I have: 我想我有:

public class FirstService : IFirstService {}
public class SecondService : ISecondService {}

public class ServiceFactory : IServiceFactory
{
    public IFirstService CreateService<IFirstService>(string url)
    {
        // logic of creation
    }

    public ISecondService CreateService<ISecondService>(string url)
    {
        // Unforchantly, it's not an overloaded method
    }
}

How to create the SecondService in this way? 如何以这种方式创建SecondService I know that my question and my code example is terrible and this is an improper approach, but want to know what other developers do in this case. 我知道我的问题和我的代码示例很糟糕,这是一种不正确的方法,但想知道其他开发人员在这种情况下做了什么。 Thanks. 谢谢。

The simplest solution to your problem might be to avoid overloading the factory method completely (not very 'OO' though). 解决问题的最简单方法可能是避免完全重载工厂方法(尽管不是'OO')。 Examine the generic type parameter, create the appropriate instance, and cast it back to the correct type: 检查泛型类型参数,创建适当的实例,然后将其强制转换为正确的类型:

public class ServiceFactory : IServiceFactory {
    public TServiceType CreateService<TServiceType>(string url)
        {
            Type t = typeof(TServiceType);

            if (t == typeof(IFirstService))
                return (TServiceType) CreateFirstServiceInstance(url);

            if (t == typeof(ISecondService))
                return (TServiceType) CreateSecondServiceInstance(url);

            throw new InvalidOperationException("Unsupported service type");
        }
}

Currently, the contract of IServiceFactory specifies that any service can be created. 目前, IServiceFactory的合同规定可以创建任何服务。 Your concrete implementation, however, only provides two specific services. 但是,您的具体实现仅提供两种特定服务。 Thus, you did not implement the interface contract. 因此,您没有实现接口契约。

How do we fix this? 我们如何解决这个问题? By changing the implementation (so that any service can be created) or by changing the contract (so that only the two given services can be created). 通过更改实现(以便可以创建任何服务)或通过更改合同(以便只能创建两个给定的服务)。 The answer from richzilla gives an example for the former, here is an example for the latter: richzilla的答案为前者提供了一个例子,这是后者的一个例子:

public interface IServiceFactory
{
    IFirstService CreateFirstService(string url);
    ISecondService CreateSecondService(string url);
}

public class ServiceFactory : IServiceFactory
{
    public IFirstService CreateFirstService(string url) { ... }
    public ISecondService CreateSecondService(string url) { ... }
}

You don't even need generics for that. 你甚至不需要泛型。 Additional bonus: If the first and the second service require different parameters, your can support that, too. 额外奖励:如果第一个和第二个服务需要不同的参数,您也可以支持。

The FirstService und SeconService should extend a common Interface (eg their Interfaces extend a common Interface like ServiceInterface, or they extend directly from some common Interface like ServiceInterface) For concrete creation you would pass the wished concrete type (or a string...) to the create method of the factory: FirstService和SeconService应扩展一个通用接口(例如,它们的接口扩展了一个公共接口,如ServiceInterface,或者它们直接从一些常见接口扩展,如ServiceInterface)对于具体创建,您可以将希望的具体类型(或字符串...)传递给工厂的创建方法:

public interface IService {}
public interface IFirstService : IService {}
public interface ISecondService : IService {}
public class FirstService : IFirstService {}
public class SecondService : ISecondService {}

public class ServiceFactory : IServiceFactory {
    public IService CreateService<IService>(Type class, string url)
        {
            // logic of creation
            new class(url)
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM