ASP.Net 中的 Dipendecy 注入原理

问题描述

我有一个 APS.NET 服务的配置,它通过把手解析它的一些配置字符串。它是由 AddSingleton 使用 GetSection 添加的单例,如下所示:

    services.AddSingleton<Configuration.IConfiguration(Configuration.GetSection("Configuration")
            .Get<Rest_Api.Configuration.Configuration>());

现在我是 DI 的新手并尝试了一些原则。所以配置需要一个我想注入的所谓的 HandlebarsHandle

Handler的代码为:

public interface IHandlebarsHandler 
{
    string Resolve(string contentTemplate,dynamic context);
}

public class HandlebarsHandler : IHandlebarsHandler
{
    public string Resolve(string contentTemplate,dynamic context)
    {
        var template = Handlebars.Compile(contentTemplate);

        // resolve bindings with dynamic context
        var result = template(context);

        return result;
    }
}

配置代码为:

public interface IConfiguration 
{
    string StudentsJsonPath { get; set; }
    string EmployeesJsonPath { get; set; }
    string IdmsRestService { get; set; }
    string AddressRoute { get; set; }
    string Get(string propertyName,dynamic context = null);
    bool Contains(string propertyName);
    List<string> GetPropertyNames();
    void SetHandlebars(IHandlebarsHandler handler);
}

public class Configuration : IConfiguration
{
    private static NLog.Logger Log = LogManager.GetCurrentClassLogger();

    public string StudentsJsonPath { get; set; }
    public string EmployeesJsonPath { get; set; }
    public string IdmsRestService { get; set; }
    public string AddressRoute { get; set; }

    private PropertyInfo _actual;

    private IHandlebarsHandler _handler;

    public bool Contains(string propertyName)
    {
        return (_actual = typeof(Configuration).GetProperty(propertyName)) != null;
    }

    public List<string> GetPropertyNames() 
        => typeof(Configuration).GetProperties().Select(p => p.Name).ToList();

    public void SetHandlebars(IHandlebarsHandler handler) => _handler = handler;

    public string Get(string propertyName,dynamic context = null)
    {
        var propertyValue = 
            _actual?.Name == propertyName ? _actual.GetValue(this) : typeof(Configuration).GetProperty(propertyName).GetValue(this);

        if (_handler == null)
        {
            Log.Warn("HandlerbarsHandler was null. Default will be implementation of RestApi. If you are running Tests you may want to inject an other Handler");
            _handler = new HandlebarsHandler();
        }

        return _handler.Resolve(propertyValue.ToString(),context);
    }
}

在 StartUp.cs 中,我将处理程序添加为作用域,将配置添加为单例:

        services.AddScoped<IHandlebarsHandler,HandlebarsHandler>();
        services.AddSingleton<Configuration.IConfiguration>(Configuration.GetSection("Configuration").Get<Rest_Api.Configuration.Configuration>());

如您所见,每次我需要“已解析”字段时,我都必须使用以下命令在配置中设置处理程序:SetHandlebars

但我认为有更好的方法将处理程序注入到配置中。

有人可以解释一下吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...