8

我有一个类调用互联网服务来获取一些数据:

public class MarketingService
{
    private IDataProvider _provider;
    public MarketingService(IDataProvider provider)
    {
        _provider = provider;
    }

    public string GetData(int id)
    {
        return _provider.Get(id);
    }
}

目前我有两个提供者:HttpDataProvider 和 FileDataProvider。通常我会连接到 HttpDataProvider 但如果外部 Web 服务失败,我想更改系统以绑定到 FileDataProvider 。就像是:

public string GetData(int id)
{
    string result = "";

    try
    {
        result = GetData(id); // call to HttpDataProvider
    }
    catch (Exception)
    {
        // change the Windsor binding so that all future calls go automatically to the
        // FileDataProvier
        // And while I'm at it, retry against the FileDataProvider    
    }

    return result;
}

因此,当这被执行后,MarketingService 的所有未来实例都将自动连接到 FileDataProvider。如何即时更改温莎绑定?

4

2 回答 2

8

一种解决方案是使用选择器

public class ForcedImplementationSelector<TService> : IHandlerSelector
{
    private static Dictionary<Type, Type>  _forcedImplementation = new Dictionary<Type, Type>();

    public static void ForceTo<T>() where T: TService
    {
        _forcedImplementation[typeof(TService)] = typeof(T);
    }

    public static void ClearForce()
    {
        _forcedImplementation[typeof(TService)] = null;
    }

    public bool HasOpinionAbout(string key, Type service)
    {
        return service == typeof (TService);
    }

    public IHandler SelectHandler(string key, Type service, IHandler[] handlers)
    {
        var tService = typeof(TService);
        if (_forcedImplementation.ContainsKey(tService) && _forcedImplementation[tService] != null)
        {
            return handlers.FirstOrDefault(handler => handler.ComponentModel.Implementation == _forcedImplementation[tService]);
        }

        // return default
        return handlers[0];
    }
}

测试和使用

[TestFixture]
public class Test
{
    [Test]
    public void ForceImplementation()
    {
        var container = new WindsorContainer();

        container.Register(Component.For<IFoo>().ImplementedBy<Foo>());
        container.Register(Component.For<IFoo>().ImplementedBy<Bar>());

        container.Kernel.AddHandlerSelector(new ForcedImplementationSelector<IFoo>());

        var i = container.Resolve<IFoo>();
        Assert.AreEqual(typeof(Foo), i.GetType());

        ForcedImplementationSelector<IFoo>.ForceTo<Bar>();

        i = container.Resolve<IFoo>();
        Assert.AreEqual(typeof(Bar), i.GetType());


        ForcedImplementationSelector<IFoo>.ClearForce();

        i = container.Resolve<IFoo>();
        Assert.AreEqual(typeof(Foo), i.GetType());
    }
}
于 2013-04-29T20:50:35.127 回答
1

或者,您可以创建一个代理:

public class AutoSelectingDataProvider : IDataProvider 
{
    public AutoSelectingDataPovider(HttpDataProvider httpDataProvider, FallBackDataProvider fallBackProvider)
    {
        _httpDataProvider = httpDataProvider;
        _fallBackDataProvider = fallBackDataProvider;
    }


    public string GetData(int id)
    {
        try
        {
            return _httpDataProvider.GetData(id);
        }
        catch (Exception)
        {
            return _fallBackDataProvider.GetData(id);
        }
    return result;
    }
}


container.Register(
    Component.For<HttpDataProvider>(),
    Component.For<FallBackDataProvider>(),
    Component.For<IDataProvider>().ImplementedBy<FallBackDataProvider>());

如果不成功使用回退,这将始终首先尝试从 HttpDataProvider 获取数据。如果您愿意,您可以引入状态并在失败后始终使用回退。这样,您可以继续在应用程序中使用 IDataProvider,而无需从容器中获取新的 IDataProvider。

于 2013-05-01T15:12:00.993 回答