插入记录器实例的.Net Core 2.2 AutoFac注册依赖项

时间:2019-06-13 19:20:05

标签: c# asp.net-core autofac

工具版本

  • asp.net核心2.2
  • Autofac 4.9.2

问题

我正在尝试将记录器实例注入到已经配置为由Autofac进行依赖项注入的类中

设置

我有一个查询数据库的类。该类具有由Autofac注入的连接字符串。以下设置适用。

未插入Logger的类:

public class ItemQueries
        : IItemQueries
    {
        private readonly string _connectionString = default(string);

        public ItemQueries(string connectionString)
        {
            _connectionString = !string.IsNullOrWhiteSpace(connectionString) ? connectionString : throw new ArgumentNullException(nameof(connectionString));
        }

        public async Task<ItemViewModel> GetItemAsync(int id)
        {
            using (var connection = new SqlConnection(_connectionString))
            {
                connection.Open();

                var result = await connection.QueryAsync<dynamic>(
                    @"SELECT *                        
                    FROM dbo.items
                    WHERE Id=@id"
                    , new { id }
                );

                if (result.AsList().Count == 0)
                {
                    throw new KeyNotFoundException();
                }

                return MapItem(result.FirstOrDefault());
            }
        }

        private ItemViewModel MapItem(dynamic result)
        {
            return new ItemViewModel()
            {
                Id = result.Id,
                Name = result.Name
            };
        }
    }

这是我向Autofac注册课程的方式:

public class ItemModule
        : Autofac.Module
    {
        public string QueriesConnectionString { get; }

        public ItemModule(string queriesConnectionString)
        {
            QueriesConnectionString = queriesConnectionString;
        }

        protected override void Load(ContainerBuilder builder)
        {
            builder.Register(c => new ItemQueries(QueriesConnectionString)) // <-- this breaks when I make the below changes because it's looking for an instance of ILogger<ItemQueries>
                .As<IItemQueries>()
                .InstancePerLifetimeScope();
        }
    }

但是,我想将Logger实例注入到ItemQueries类中,因此我将其注入到构造函数中,如下所示:

private readonly ILogger<ItemQueries> _logger;
private readonly string _connectionString = default(string);

        public ItemQueries(ILogger<ItemQueries> logger, string connectionString)
        {
            _logger = logger;
            _connectionString = !string.IsNullOrWhiteSpace(connectionString) ? connectionString : throw new ArgumentNullException(nameof(connectionString));
        }

但这破坏了我在Autofac模块中的注册。如何告诉Autofac ItemQueries类要求注入Logger实例?

我认为Autofac可以锻炼ItemQueries类需要注入ILogger<>实例(您知道,就好像是魔术!)

2 个答案:

答案 0 :(得分:2)

您可以解决lambda注册中的问题。这就是lambda中的context参数的作用。

builder
  .Register(c =>
    new ItemQueries(QueriesConnectionString, c.Resolve<ILogger<ItemQueries>>()))
  .As<IItemQueries>()
  .InstancePerLifetimeScope();

答案 1 :(得分:0)

您需要使用Autofac的“工厂”功能。

这是使用Factory进行解决方案的设置

private readonly ILogger<ItemQueries> _logger;
private readonly string _connectionString = default(string);

public delegate ItemQueries Factory(string connectionString); // --> here, you create the delegate factory

// connectionString needs to be the first parameter
public ItemQueries(string connectionString, ILogger<ItemQueries> logger) 
{
    _logger = logger;
    _connectionString = !string.IsNullOrWhiteSpace(connectionString) ? connectionString : throw new ArgumentNullException(nameof(connectionString));
}

模块设置应如下所示:

public class ItemModule: Autofac.Module
{
    public string QueriesConnectionString { get; }

    public ItemModule(string queriesConnectionString)
    {
        QueriesConnectionString = queriesConnectionString;
    }

    protected override void Load(ContainerBuilder builder)
    {
        builder.RegisterType<ItemQueries>();  // --> not sure if needed

        builder.Register(c => 
        {
           var factory = c.Resolve<ItemQueries.Factory>();
           return factory.Invoke(QueriesConnectionString);
        })  
        .As<IItemQueries>()
        .InstancePerLifetimeScope();
    }
}