src/Framework/Config/Config.Core/ConfigProvider.cs

(开头部分) 5KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using System;
using System.Linq;
using Microsoft.Extensions.Configuration;
using NetModular.Lib.Config.Abstractions;
using Newtonsoft.Json;

namespace NetModular.Lib.Config.Core
{
    public class ConfigProvider : IConfigProvider
    {
        /// <summary>
        /// 含有了实例的配置描述符集合
        /// </summary>
        private static readonly IConfigCollection ConfigWidthInstanceCollection = new ConfigCollection();

        private readonly IConfigCollection _configs;
        private readonly IConfigStorageProvider _storageProvider;
        private readonly IServiceProvider _serviceProvider;
        private readonly IConfiguration _cfg;

        public ConfigProvider(IConfigCollection configs, IConfigStorageProvider storageProvider, IServiceProvider serviceProvider, IConfiguration cfg)
        {
            _configs = configs;
            _storageProvider = storageProvider;
            _serviceProvider = serviceProvider;
            _cfg = cfg;
        }

        public IConfig Get(Type implementType)
        {
            var descriptor = GetDescriptor(m => m.ImplementType == implementType);
            if (descriptor.Instance == null)
            {
                var json = _storageProvider.GetJson(descriptor.Type, descriptor.Code).Result;
                if (json.IsNull())
                {
                    descriptor.Instance = (IConfig)Activator.CreateInstance(implementType);
                    if (descriptor.Type == ConfigType.Library)
                    {
                        var section = _cfg.GetSection(descriptor.Code);
                        if (section != null)
                        {
                            section.Bind(descriptor.Instance);
                        }
                    }
                }
                else
                    descriptor.Instance = (IConfig)JsonConvert.DeserializeObject(json, implementType);
            }

            return descriptor.Instance;
        }

        public IConfig Get(string code, ConfigType type)
        {
            var descriptor = GetDescriptor(m => m.Code.EqualsIgnoreCase(code) && m.Type == type);
            if (descriptor.Instance == null)
            {
                var json = _storageProvider.GetJson(descriptor.Type, descriptor.Code).Result;
                if (json.IsNull())
后面还有 97 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。