0

我希望能够在我的 Unity 容器中为同一接口注册 2 个类。然后,我想根据一个参数选择当前的实现。

这是我的界面:

public interface ICheckService
{
    HttpResponseMessage Validate(string p1, string p2);
}
}

我的服务:

public class CheckService1 : ICheckService
{
    public HttpResponseMessage Validate(string p1, string p2)
    {
      /////code
    }
}

public class CheckService2 : ICheckService
{
    public HttpResponseMessage Validate(string p1, string p2)
    {
        ////code
    }
}

在 bootstraper.cs 我声明我的服务:

        `container.RegisterType<ICheckService, CheckService1>();`
        `container.RegisterType<ICheckService, CheckService2>();`

我的 API 控制器:

public class ServiceController : ApiController
{
    private readonly ICheckService _checkService;

    public ServiceController(ICheckService checkService)
    {
        _checkService = checkService;
    }
    [HttpGet]
    public HttpResponseMessage Validate(string p1, string p2)
    {
        return _checkService.Validate(p1, p2);
    }
 }

现在,当我调用我的 api 时,我想根据 p1 参数选择实现。

如果 p1 等于 Service1,则从 Service1 类调用 Validate 方法,如果 p1 等于 Service2,则从 Service2 类调用 validate 方法。

谢谢你的帮助。

4

3 回答 3

1

You should use a factory and not the container for that. The factory implementation could request the proper implementation from the container internally (i.e. that's just an implementation detail and not nothing that the factory invoker should be aware of).

But the point is that using the IoC directly is not a good fit in your case.

public class ServiceController : ApiController
{
    private readonly ICheckServiceFactory _checkFactory;

    public ServiceController(ICheckServiceFactory factory)
    {
        _checkFactory = factory;
    }
    [HttpGet]
    public HttpResponseMessage Validate(string p1, string p2)
    {
        var service = _checkFactory.Create(p1);
        service.Validate(p1, p2);
    }
 }
于 2013-03-28T11:33:34.500 回答
0

在您的情况下,CheckService1 和 CheckService2 是验证的实现类/详细信息。您可以创建一个 CheckServiceFactory,它可以在运行时为您创建这些类。注册工厂类而不是实现类。

于 2016-08-10T16:55:45.730 回答
0

另一种选择是使用策略模式。一个类将充当检查服务的工厂。工厂类将初始化 ICheckServices 的字典。工厂(ICheckServiceFactory)将使用选择的 IoC 容器进行注册:

public interface ICheckServiceFactory
{
    string Validate(string validationStrategy, string stringToValidate);
}

public class CheckServiceFactory : ICheckServiceFactory
{
    private readonly Dictionary<string, ICheckService> _checkServices;

    public CheckServiceFactory()
    {
        _checkServices = new Dictionary<string, ICheckService>
        {
            {"cash", new CheckService1()},
            {"card", new CheckService2()}
        };
    }

    public string Validate(string validationStrategy, string stringToValidate)
    {
        return  $"Parameter '{stringToValidate}' is Valid equal {_checkServices[validationStrategy].Validate(stringToValidate)} using {_checkServices[validationStrategy].CheckType}";
    }
}

每个检查服务都实现了 ICheckServices:

public interface ICheckService
{
    string CheckType { get; }

    bool Validate(string validateMe);
}

public class CheckService2 : ICheckService
{
    public string CheckType { get; }

    public CheckService2()
    {
        CheckType = "CheckService2";
    }

    public bool Validate(string validateMe)
    {
        return true;
    }
}

public class CheckService1 : ICheckService
{
    public string CheckType { get; }

    public CheckService1()
    {
        CheckType = "CheckService1";
    }

    public bool Validate(string validateMe)
    {
        return true;
    }
}

因此,您的服务控制器将类似于您最初希望的样子:

public class ServiceController : ApiController
{
    private readonly ICheckServiceFactory _checkServiceFactory;

    public ServiceController(ICheckServiceFactory factory)
    {
        _checkServiceFactory = factory;
    }
    [HttpGet]
    public HttpResponseMessage Validate(string p1, string p2)
    {
        return _checkServiceFactory.Validate(p1, p2);
    }
}
于 2016-08-10T17:49:46.937 回答