Call multiple classes with the same interface

后端 未结 3 615
-上瘾入骨i
-上瘾入骨i 2021-01-06 04:34

I have an interface like

public interface IAddressProvider
{
    string GetAddress(double lat, double long);
}

In my consuming class I wan

相关标签:
3条回答
  • 2021-01-06 05:06

    I'm not familiar with StructureMap specifically but there are two solutions as far as I can see.

    1) Named Instances - You register your 3 concrete implementations of IAddressProvider with StructureMap as named instances, and then configure the constructor parameters.

    StructureMap named instance configuration: http://docs.structuremap.net/InstanceExpression.htm#section14

    Using named parameters in constructor injection: http://lookonmyworks.co.uk/2011/10/04/using-named-instances-as-constructor-arguments/

    2) More Interfaces - Assuming that there's only ever going to be a few IAddressProvider implementations and not hundreds you could create an ICachedAddressProvider, ILocalDbAddressProvider, and IExternalAddressProvider that implement IAddressProvider and then use those in the constructor of the consuming class.

    If there are likely to be significantly more concrete IAddressProvider implementations then you might want to look into something along the lines of an Abstract Factory instead.

    0 讨论(0)
  • 2021-01-06 05:07

    Could you not just use container.GetAllInstances?, like so:

    var address = new List<string>();
    foreach (var provider in container.GetAllInstances<IAddressProvider>())
    {
        address.add(provider.GetAddress(lat, long));
    }
    

    Edit:

    I see what you mean now. If you're using StructureMap 2.x then I would recommend looking at the Conditionally clause. However this has been removed in version 3 in favour of creating your own builder class that should be responsible for returning the correct instance.

    For example:

    public class AddressProviderBuilder : IInstanceBuilder
    {
        private readonly IContainer container;
    
        public AddressProviderBuilder(IContainer container)
        {
            this.container = container;
        }
    
        public IAddressProvider Build()
        {
            foreach (var provider in this.container.GetAllInstances<IAddressProvider>())
            {
                if (provider.GetAddress(lat, long) != null)
                {
                    return provider;
                }
            }
    
            return null;
        }
    }
    
    0 讨论(0)
  • 2021-01-06 05:12

    The fact that you have multiple address-providers is not something the calling code should have to deal with. So create a specific provider-proxy to handle with these multiple providers.

    Like this.

    public interface IAddressProvider {
        string GetAddress(double lat, double long);
    }
    
    public class AddressProviderProxy: IAddressProvider {
        public AddressProviderProxy(IAddressProvider[] providers) {
            _providers = providers; // TODO: Add a NULL guard
        }
    
        private readonly IAddressProvider[] _providers;
    
        string IAddressProvider.GetAddress(double lat, double long) {
            foreach (var provider in _providers) {
                string address = provider.GetAddress(lat, long);
                if (address != null)
                    return address;
            }
            return null;
        }
    }
    
    // Wire up using DI
    container.Register<IAddressProvider>(
        () => new AddressProviderProxy(
            new IAddressProvider[3] {
                cachedAddressProvider,
                localDbAddressProvider,
                externalAddressProvider
            }
        )
    );
    
    // Use it
    IAddressProvider provider = ...from the container, injected..
    string address = provider.GetAddress(lat, long) ?? "no address found";
    
    0 讨论(0)
提交回复
热议问题