Autofac从单个接口注册多个实现

时间:2018-03-07 04:26:40

标签: autofac

如果给出下面的代码片段,如何在Autofac上注册?

public interface IService
{
    IEnumerable<string> GetNames();
}

public class GospelNames : IService
{
    public IEnumerable<string> GetNames() { return new List<string>{ "John", "Mark" };}
}

public class CommonNames : IService
{
    public IEnumerable<string> GetNames() { return new List<string>{ "Ben", "Matt" };}
}

public class WeirdNames : IService
{
    public IEnumerable<string> GetNames() { return new List<string>{ "Weird Al" };}
}

public class BaseImplementation : IService
{
    private readonly IEnumerable<IService> _services;

    public BaseImplementation(params IService[] services)
    {
        _services = services;
    }

    public IEnumerable<string> GetNames()
    {
        var results = new List<string>();
        foreach(var service in _services)
        {
            results.AddRange(service.GetNames());
        }
        return results;
    }
}

我一直在阅读有关装饰器模式的内容,我不确定这是否是转换为该模式的候选者?或者这是一种不好的做法?

0 个答案:

没有答案