7

DryIoC 面临着一个相当令人费解的情况。

好吧,实际上,这是我第一次使用 IoC 容器,所以我可能只是误解了一切:从依赖注入到 IoC 容器,再到 DryIoC 本身。

尽管如此,我还是一名专业程序员已经有一段时间了,我有不错的谷歌搜索技能,我什至找不到其他人暴露的类似问题。

假设我有一个公开这些接口的类库......

namespace Logging
{
    public interface ILogger
    {
        void Info(string message);
    }

    public interface ILoggerFactory
    {
        ILogger CreateLogger(string name);
    }
}

...以及另一个实现上述接口的类库。

namespace Logging.Console
{
    internal class ConsoleLogger : ILogger
    {
        readonly string _name;

        public ConsoleLogger(string name)
        {
            _name = name;
        }

        void Info(string message) => System.Console.WriteLine($"[{_name}] {message}");
    }

    public class ConsoleLoggerFactory : ILoggerFactory
    {
        public ILogger CreateLogger(string name) => new ConsoleLogger(name);
    }
}

然后是第三个图书馆,里面有我需要的其他东西:

namespace LibraryA
{
    public interface IServiceA
    {
        // bla bla
    }

    public class ServiceA : IServiceA
    {
        // Implement service A
    }

    public interface IServiceB
    {
        // yada yada
    }

    public class ServiceB : IServiceB
    {
        // Implement service B
    }
}

最后,一个使用上面所有库的类库来实现咖啡研磨机(我喜欢咖啡!)

using Logging;
using LibraryA;

namespace Coffee
{
    public interface ICoffeeGrinder
    {
        GrindCoffee(int grams);
    }

    public class CoffeeGrinder : ICoffeeGrinder
    {
        readonly ILogger _logger;
        readonly IServiceA _serviceA;
        readonly IServiceB _serviceB;

        public CoffeeGrinder(ILoggerFactory loggerFactory, string loggerName,
            IServiceA serviceA, IServiceB serviceB)
        {
            _logger = loggerFactory.CreateLogger(loggerName);
            _serviceA = serviceA;
            _serviceB = serviceB;
        }

        public GrindCoffee(int grams)
        {
            _logger.Info($"About to grind {grams}g of coffee...");
            // Grind coffee
            _logger.Info("Done grinding.");
        }
    }
}

根据(例如)配置文件,一个应用程序可能需要多个研磨机,每个都有自己的名称。

因此,我希望能够loggerName在解析时指定,如下所示:

using Coffee;
using DryIoC;
using LibraryA;
using Logging;
using Logging.Console;

namespace MyApplication
{
    class Program
    {
        static void Main()
        {
            using (var container = new Container())
            {
                container.Register<ILoggerFactory, ConsoleLoggerFactory>();
                container.Register<IServiceA, ServiceA>();
                container.Register<IServiceB, ServiceB>();
                container.Register<ICoffeeGrinder, CoffeeGrinder>(
                    /* Maybe some magic here? */);

                // This won't work
                var grinder = container.Resolve<ICoffeeGrinder>("My grinder"); 
                // Use grinder
            }
        }
    }
}

换句话说,我如何告诉 DryIoC 一个或多个构造函数参数不是依赖项,而是必须在解析时指定?

4

1 回答 1

13

解析为Func<string, ICoffeeGrinder>,这不是主要容器支持的常见功能,例如 Autofac。这是有关此主题的 DryIoc wiki 。

var getGrinder = container.Resolve<Func<string, ICoffeeGrinder>>();
var grinder = getGrinder(name);

顺便说一句,您可以查看许多 IoC/DI 容器的主要功能列表以节省您的时间。此链接也可在 DryIoc 自述文件中的Benchmarks下找到。

于 2017-03-11T19:22:00.207 回答