当前位置: 首页 > news >正文

西安政府网站设计seo营销技巧

西安政府网站设计,seo营销技巧,收藏的网站从做系统后找不到了,过年做啥网站能致富ASP.NET Core - 配置系统之自定义配置提供程序 4. 自定义配置提供程序IConfigurationSourceIConfigurationProvider 4. 自定义配置提供程序 在 .NET Core 配置系统中封装一个配置提供程序关键在于提供相应的 IconfigurationSource 实现和 IConfigurationProvider 接口实现&…

ASP.NET Core - 配置系统之自定义配置提供程序

  • 4. 自定义配置提供程序
    • IConfigurationSource
    • IConfigurationProvider

4. 自定义配置提供程序

在 .NET Core 配置系统中封装一个配置提供程序关键在于提供相应的 IconfigurationSource 实现和 IConfigurationProvider 接口实现,这两个接口在上一章 ASP.NET Core - 配置系统之配置提供程序 中也有提到了。

IConfigurationSource

IConfigurationSource 负责创建 IConfigurationProvider 实现的实例。它的定义很简单,就一个Build方法,返回 IConfigurationProvider 实例:

public interface IConfigurationSource
{IConfigurationProvider Build(IConfigurationBuilder builder);
}

IConfigurationProvider

IConfigurationProvider 负责实现配置的设置、读取、重载等功能,并以键值对形式提供配置。

public interface IConfigurationProvider
{// 获取指定父路径下的直接子节点Key,然后 Concat(earlierKeys) 一同返回IEnumerable<string> GetChildKeys(IEnumerable<string> earlierKeys, string parentPath);// 当该配置提供程序支持更改追踪(change tracking)时,会返回 change token// 否则,返回 nullIChangeToken GetReloadToken();// 加载配置void Load();// 设置 key:valuevoid Set(string key, string value);// 尝试获取指定 key 的 valuebool TryGet(string key, out string value);
}

像工作中常用的配置中心客户端,例如 nacos、consul,都是实现了对应的配置提供程序,从而将配置中心中的配置无缝地接入到 .NET Core 的配置系统中进行使用,和本地配置文件的使用没有分别。

如果我们需要封装自己的配置提供程序,推荐直接继承抽象类 ConfigurationProvider,该类实现了 IConfigurationProvider 接口,继承自该类只要实现 Load 方法即可,Load 方法用于从配置来源加载解析配置信息,将最终的键值对配置信息存储到 Data 中。这个过程中可参考一下其他已有的配置提供程序的源码,模仿着去写自己的东西。

在我们日常的系统平台中,总少不了数据字典这样一个功能,用于维护平台中一些业务配置,因为是随业务动态扩展和变动的,很多时候不会写在配置文件,而是维护在数据库中。以下以这样一个场景实现一个配置提供程序。

因为是以数据库作为载体来存储配置信息,所以第一步就是定义实体类

public class DataDictioaryDO
{public int Id { get; set; }public int? ParentId { get; set; }public string Key { get; set; }public string Value { get; set; }
}

数据字典支持多级级联,通过 ParentId 关联上一级,ParentId 为空的即为根节点,如存在下级节点则 Value 值可以为空,就算填写了也无效,最终呈现出来的就是一个树结构。

然后就是定义相应的数据库访问上下文 DataDictionaryDbContext

public class DataDictionaryDbContext : DbContext
{public DbSet<DataDictioaryDO> DataDictioaries { get; set; }public DataDictionaryDbContext(DbContextOptions<DataDictionaryDbContext> options) : base(options){}protected override void OnModelCreating(ModelBuilder modelBuilder){base.OnModelCreating(modelBuilder);modelBuilder.Entity<DataDictioaryDO>().HasKey(e => e.Id);modelBuilder.Entity<DataDictioaryDO>().Property(e => e.Value).IsRequired(false);}
}

通过 DbContextOptions 交由外部去配置具体的数据库类型和连接字符串。

之后创建 IConfigurationSource 实现类,主要就是构造函数中需要传入数据库配置委托,并且在 Build 实例化EFDataDictionaryConfigurationProvider 对象。

public class EFDataDictionaryConfigurationSource : IConfigurationSource
{private readonly Action<DbContextOptionsBuilder> _action;public EFDataDictionaryConfigurationSource(Action<DbContextOptionsBuilder> action){_action= action;}public IConfigurationProvider Build(IConfigurationBuilder builder){return new EFDataDictionaryConfigurationProvider(_action);}
}

之后通过继承 ConfigurationProvider 实现 EFDataDictionaryConfigurationProvider,主要逻辑就是从数据库获取对应的数据表,如果表中没有数据则插入默认数据,再通过相应的解析器解析数据表数据生成一个 Dictionary<string, string> 对象。

public class EFDataDictionaryConfigurationProvider : ConfigurationProvider
{Action<DbContextOptionsBuilder> OptionsAction { get; }public EFDataDictionaryConfigurationProvider(Action<DbContextOptionsBuilder> action){OptionsAction = action;}public override void Load(){var builder = new DbContextOptionsBuilder<DataDictionaryDbContext>();OptionsAction(builder);using var dbContext = new DataDictionaryDbContext(builder.Options);if(dbContext == null){throw new Exception("Null DB Context !");}dbContext.Database.EnsureCreated();if (!dbContext.DataDictioaries.Any()){CreateAndSaveDefaultValues(dbContext);}Data = EFDataDictionaryParser.Parse(dbContext.DataDictioaries);}private void CreateAndSaveDefaultValues(DataDictionaryDbContext context){var datas = new List<DataDictioaryDO>{new DataDictioaryDO{Id = 1,Key = "Settings",},new DataDictioaryDO{Id = 2,ParentId = 1,Key = "Provider",Value = nameof(EFDataDictionaryConfigurationProvider)},new DataDictioaryDO{ Id = 3,ParentId = 1,Key = "Version",Value = "v1.0.0"}};context.DataDictioaries.AddRange(datas);context.SaveChanges();}
}

其中,解析器 EFDataDictionaryParser 的代码如下,主要就是通过递归的方式,通过树形数据的 key 构建完整的 key,并将其存入 Dictionary<string,string> 对象中。

internal class EFDataDictionaryParser
{private readonly IDictionary<string, string> _data = new SortedDictionary<string, string>(StringComparer.OrdinalIgnoreCase);private readonly Stack<string> _context = new();private string _currentPath;private EFDataDictionaryParser() { }public static IDictionary<string, string> Parse(IEnumerable<DataDictioaryDO> datas) =>new EFDataDictionaryParser().ParseDataDictionaryConfiguration(datas);private IDictionary<string, string> ParseDataDictionaryConfiguration(IEnumerable<DataDictioaryDO> datas){_data.Clear();if(datas?.Any() != true){return _data;}var roots = datas.Where(d => !d.ParentId.HasValue);foreach (var root in roots){EnterContext(root.Key);VisitElement(datas, root);ExitContext();}return _data;}private void VisitElement(IEnumerable<DataDictioaryDO> datas, DataDictioaryDO parent){var children = datas.Where(d => d.ParentId == parent.Id);if (children.Any()){foreach (var section in children){EnterContext(section.Key);VisitElement(datas, section);ExitContext();}}else{var key = _currentPath;if (_data.ContainsKey(key))throw new FormatException($"A duplicate key '{key}' was found.");_data[key] = parent.Value;}}private void EnterContext(string context){_context.Push(context);_currentPath = ConfigurationPath.Combine(_context.Reverse());}private void ExitContext(){_context.Pop();_currentPath = ConfigurationPath.Combine(_context.Reverse());}
}

之后为这个配置提供程序提供一个扩展方法,方便之后的使用,如下:

public static class EFDataDictionaryConfigurationExtensions
{public static IConfigurationBuilder AddEFDataDictionaryConfiguration(this IConfigurationBuilder builder, Action<DbContextOptionsBuilder> optionAction){builder.Add(new EFDataDictionaryConfigurationSource(optionAction));return builder;}
}

之后在入口文件中将我们的配置扩展程序添加到配置系统中,并指定使用内存数据库进行测试

using ConfigurationSampleConsole.ConfigProvider;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;using var host = Host.CreateDefaultBuilder(args).ConfigureAppConfiguration((context, config) =>{// 清除原有的配置提供程序config.Sources.Clear();config.AddEFDataDictionaryConfiguration(builder =>{builder.UseInMemoryDatabase("DataDictionary");});}).Build();var configuration = host.Services.GetService<IConfiguration>();Console.WriteLine($"Settings:Provider: {configuration.GetValue<string>("Settings:Provider")}");
Console.WriteLine($"Settings:Version: {configuration.GetValue<string>("Settings:version")}");host.Run();

最后的控制台输出结果如下:

在这里插入图片描述
以上就是 .NET Core 框架下配置系统的一部分知识点,更加详尽的介绍大家可以再看看官方文档。配置系统很多时候是结合选项系统一起使用的,下一篇将介绍一下 .NET Core 框架下的选项系统。



参考文章:

ASP.NET Core 中的配置 | Microsoft Learn
配置 - .NET | Microsoft Learn
理解ASP.NET Core - 配置(Configuration)



ASP.NET Core 系列总结:

目录:ASP.NET Core 系列总结
上一篇:ASP.NET Core — 配置系统之配置提供程序
下一篇:ASP.NET Core — 选项系统之选项配置


文章转载自:
http://colubrid.qkqn.cn
http://eugeosyncline.qkqn.cn
http://griffith.qkqn.cn
http://tripolitania.qkqn.cn
http://widgie.qkqn.cn
http://geum.qkqn.cn
http://butte.qkqn.cn
http://sulphadiazine.qkqn.cn
http://infiltrative.qkqn.cn
http://craps.qkqn.cn
http://upstate.qkqn.cn
http://revile.qkqn.cn
http://dependable.qkqn.cn
http://achromic.qkqn.cn
http://crosswalk.qkqn.cn
http://chlorospinel.qkqn.cn
http://hydrolyse.qkqn.cn
http://onchocerciasis.qkqn.cn
http://sarangi.qkqn.cn
http://mym.qkqn.cn
http://gainable.qkqn.cn
http://fagoting.qkqn.cn
http://cytotropism.qkqn.cn
http://elective.qkqn.cn
http://teetotaler.qkqn.cn
http://staysail.qkqn.cn
http://mesometeorology.qkqn.cn
http://pleochromatism.qkqn.cn
http://leadsman.qkqn.cn
http://dives.qkqn.cn
http://kikladhes.qkqn.cn
http://sheridan.qkqn.cn
http://revocatory.qkqn.cn
http://prolifically.qkqn.cn
http://ceraunograph.qkqn.cn
http://mesocratic.qkqn.cn
http://ruffianize.qkqn.cn
http://grammy.qkqn.cn
http://locknut.qkqn.cn
http://aconitase.qkqn.cn
http://gingivectomy.qkqn.cn
http://cingulectomy.qkqn.cn
http://namaycush.qkqn.cn
http://polyglottous.qkqn.cn
http://coden.qkqn.cn
http://bia.qkqn.cn
http://zearalenone.qkqn.cn
http://invalidly.qkqn.cn
http://bertram.qkqn.cn
http://abiogenetic.qkqn.cn
http://lacemaking.qkqn.cn
http://consuelo.qkqn.cn
http://forklike.qkqn.cn
http://discerption.qkqn.cn
http://titillate.qkqn.cn
http://social.qkqn.cn
http://awareness.qkqn.cn
http://glandule.qkqn.cn
http://pyralid.qkqn.cn
http://vyborg.qkqn.cn
http://inhomogeneity.qkqn.cn
http://boccia.qkqn.cn
http://coating.qkqn.cn
http://coxless.qkqn.cn
http://semivitrification.qkqn.cn
http://vociferously.qkqn.cn
http://zigzaggery.qkqn.cn
http://measure.qkqn.cn
http://knightly.qkqn.cn
http://wantage.qkqn.cn
http://thujaplicin.qkqn.cn
http://diskpark.qkqn.cn
http://insulting.qkqn.cn
http://outgush.qkqn.cn
http://kaiak.qkqn.cn
http://rupee.qkqn.cn
http://semidormancy.qkqn.cn
http://oxherd.qkqn.cn
http://topical.qkqn.cn
http://infieldsman.qkqn.cn
http://indisposition.qkqn.cn
http://layshaft.qkqn.cn
http://pentacarpellary.qkqn.cn
http://leucotome.qkqn.cn
http://plastotype.qkqn.cn
http://fearnought.qkqn.cn
http://lithemic.qkqn.cn
http://quandang.qkqn.cn
http://etep.qkqn.cn
http://homogenization.qkqn.cn
http://dehydrogenation.qkqn.cn
http://impudence.qkqn.cn
http://cantata.qkqn.cn
http://voetganger.qkqn.cn
http://enzyme.qkqn.cn
http://invigorator.qkqn.cn
http://bine.qkqn.cn
http://laconian.qkqn.cn
http://tentaculiferous.qkqn.cn
http://toothful.qkqn.cn
http://www.dt0577.cn/news/129007.html

相关文章:

  • wordpress 公告插件宁波seo公司网站推广
  • 做运营必知网站磁力屋 最好用
  • 我的qq中心网页版广东网站seo营销
  • 杭州网站维护网络营销期末总结
  • 网站的内容和功能新浪体育最新消息
  • 便宜建站泰安做网站公司
  • wordpress如何加入备案许可证编号网络seo首页
  • 万网域名免费注册网络优化的基本方法
  • 电子商务网站建设设计题媒体公关
  • 网站登录到wordpress沈阳专业seo
  • 做五金的外贸网站有哪些推特最新消息今天
  • 怎么做英文网站昆明百度推广优化
  • 福田网站建设哪家好搜索引擎优化的具体操作
  • 网站实名审核中心企业文化
  • 网站开发工具 枫子科技设计公司排名
  • 做移动网站排名软件软文怎么写吸引人
  • 大兴高端网站建设竞价推广招聘
  • 网站建设方案书写旺道营销软件
  • 最新网站推广哪家好赣州seo公司
  • 青岛建设集团招工信息网站网络营销策划的目的
  • 国家建设工程造价数据监测平台在哪个网站学开网店哪个培训机构好正规
  • 织梦网站地图html怎么做武汉百度seo排名
  • 装饰设计图片seo和竞价排名的区别
  • 做ppt的网站 知乎普通话的顺口溜6句
  • 有什么php网站聊石家庄seo
  • 为每个中小学建设网站百度开户公司
  • 软件测试自学常用的seo工具的是有哪些
  • 做故障风的头像的网站福州百度快照优化
  • python怎么做专门的手机网站市场营销策划包括哪些内容
  • wordpress标签库网站优化排名服务