c# - DryIoC - 在解析时指定一些构造函数参数

标签 c# dependency-injection dryioc

我在使用 DryIoC 时遇到了一个相当令人费解的情况。

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

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

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

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

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

...和另一个实现上述接口(interface)的类库。

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 一个或多个构造函数参数不是依赖项,而是必须在解析时指定?

最佳答案

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

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

顺便说一句,你可以看看major feature list许多 IoC/DI 容器以节省您的时间。此链接也可在 DryIoc 自述文件的 Benchmarks 下找到。

关于c# - DryIoC - 在解析时指定一些构造函数参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42734491/

相关文章:

c# - DryIoc 递归依赖异常与工厂(Func<>)

c# - 如何将 XElement 转换为 XComment (C#)

c# - 将具有双属性的对象列表与 FluentAssertions 进行比较 (C#)

c# - 显示多个数组

java - 为什么要使用@PostConstruct?

java - DI 与 Java 6?

c# - 使用 DryIoc 创建具有多个服务注册的单例

c# - 如何跨多个服务器在 OWIN 中为 IIS 使用 OAuth AccessTokenFormat?

php - Symfony2 : Choose a service depending on an object classname

xamarin.forms - 在 Xamarin Forms 中使用 Prism 重新实例化单例