ASP.NET Core???????2??????????????
???????????? ???????[ 2016/6/2 14:19:09 ] ?????????????????? ???霉???
????????ConfigurationProvider
?????????????????????????ConfigurationProvider????????????IConfigurationProvider???????????????????????????????????????????????ConfigurationProvider??????????????????????????????????????????????????????????????????????????????????????????IConfigurationProvider????械?????????????????????????????
????1: public interface IConfigurationProvider
????2: {
????3: void Load();
????4:
????5: bool TryGet(string key?? out string value);
????6: void Set(string key?? string value);
????7: IEnumerable<string> GetChildKeys(IEnumerable<string> earlierKeys?? string parentPath?? string delimiter)
????8: }
???????????????????????ConfigurationProvider??Load????????伞???????????TryGet??????????????Key????????????????????????????????????ConfigurationProvider???????????????????ConfigurationProvider?????????????卸?????????????????????卤????????????????????????????????Set???????????????????????????????小?ConfigurationProvider??GetChildKeys?????????????路????????y???????????Key??
????????????????????????卸????ConfigurationProvider?????????????????????????IConfigurationProvider??????????????????ConfigurationProvider?????????????????????????????????渭??????慰??????????????????IDictionary<string?? string>???????????Set??TryGetValue??????????????????????????????Load??????????????榉�?????????ConfigurationProvider?????????写????????????????????卸????????????????????????谐??????
1: public abstract class ConfigurationProvider : IConfigurationProvider
2: {
3: protected IDictionary<string?? string> Data { get; set; }
4:
5: public IEnumerable<string> GetChildKeys(IEnumerable<string> earlierKeys?? string parentPath?? string delimiter)
6: {
7: //??????
8: }
9:
10: public virtual void Load()
11: {}
12:
13: public void Set(string key?? string value)
14: {
15: this.Data[key] = value;
16: }
17:
18: public bool TryGet(string key?? out string value)
19: {
20: return this.Data.TryGetValue(key?? out value);
21: }
22: //???????
23: }
??????????????????????????????????????GetChildKeys??????????????????路????Key????????????????????????位???谓??????????????????????????y?????????????????y??Key???????????y????????parentPath?????路??????????????????delimiter????路????????????????????????????????????????????????????earlierKeys???????????????????Key??????斜??????????????小?
1: class Program
2: {
3: static void Main(string[] args)
4: {
5: Dictionary<string?? string> source = new Dictionary<string?? string>
6: {
7: ["A:B:C"] = ""??
8: ["A:B:D"] = ""??
9: ["A:E"] = ""??
10: };
11:
12: MemoryConfigurationProvider provider = new MemoryConfigurationProvider(source);
13: Console.WriteLine("{0?? -20}{1}"?? "Parent Path"?? "Child Keys");
14: Console.WriteLine("------------------------------------------");
15:
16: Print("Null"?? provider.GetChildKeys(new string[] { "X"?? "Y"?? "Z" }?? null?? ":"));
17: Print("A"?? provider.GetChildKeys(new string[] { "x"?? "y"?? "z" }?? "A"?? ":"));
18: Print("A:B"?? provider.GetChildKeys(new string[] { "X"?? "Y"?? "Z }?? "A:B"?? ":"));
19: Print("A:B:C"??provider.GetChildKeys(new string[] { "X"?? "Y"?? "Z }?? "A:B:C"?? ":"));
20: }
21:
22: static void Print(string parentPath?? IEnumerable<string> keys)
23: {
24: Console.WriteLine("{0?? -20}{1}"?? parentPath?? string.Join("?? "?? keys.ToArray()));
25: }
26: }
????????????????????????????????GetChildKeys????????????????写????????????????????????????MemoryConfigurationProvider???????????????????????????????????????????Key?????“A??B??C”??“A??B??D”??“A??E”?????????????GetChildKeys?????????????????路?????????“A”??“A??B??“A??B??C”????????????Key????????????????e???“??”???????????????earlierKeys????????“X”??“Y”??“Z”???????????椤�??纬????????????????????????渭???????????????锌???????????????????????薪??????Key?????
????1: Parent Path Child Keys
????2: ------------------------------------------
????3: Null X?? Y?? Z
????4: A B?? B?? E?? X?? Y?? Z:
????5: A:B C?? D?? X?? Y?? Z
????6: A:B:C X?? Y?? Z
???????ConfigurationBuilder
????ConfigurationBuilder????????????IConfigurationBuilder??????????????????????????械???????????????ConfigurationProvider??????????????????????????????????Configuration??????????????????????????????????ConfigurationRoot???????ConfigurationBuilder???ConfigurationProvider?????IConfigurationBuilder????Providers???????????????Add??????ConfigurationProvider????????????小?
????1: public interface IConfigurationBuilder
????2: {
????3: IEnumerable<IConfigurationProvider> Providers { get; }
????4: Dictionary<string?? object> Properties { get; }
????5:
????6: IConfigurationBuilder Add(IConfigurationProvider provider);
????7: IConfigurationRoot Build();
????8: }
???????????IConfigurationBuilder????????????????????????Properties???????????????????????????????ConfigurationBuilder?????????????????Key?????些???????ConfigurationRoot??????????Build??????伞?
????????????????????????????IConfigurationBuilder??????????????????????????????卸??????ConfigurationBuilder????????????????????????????????????Build?????小???????????????ConfigurationBuilder???械?Build????????????????????ConfigurationRoot??????????????????ConfigurationSection????????????y??????????UML???????????????Configuration??ConfigurationProvider??ConfigurationBuilder???????????/???????????????????
????ConfigurationRoot??ConfigurationSection????????????????????????????????????????????????????????????????????????????????????????????????????????Configuration??????????????????ConfigurationRoot????ConfigurationSection???????????????蟹???魏蔚????????????????????????????????写??????????????????ConfigurationProvider???????Configuration????????????ConfigurationProvider????????????????ConfigurationBuilder??????????ConfigurationRoot???????效???????????????????????????效???
????1: IConfigurationBuilder builder = new ConfigurationBuilder().Add(new MemoryConfigurationProvider());
????2:
????3: IConfiguration config1 = builder.Build();
????4: IConfiguration config2 = builder.Build();
????5:
????6: config1["Foobar"] = "ABC";
????7: Debug.Assert(config2["Foobar"] == "ABC");
????xxx???????????ConfigurationRoot??ConfigurationSection????????????????????????y?????????????????????????????????霉?????????????????????????????????????????????y??ConfigurationSection??????????????????????????????????????????????ConfigurationRoot??????????????ConfigurationSection?????????????????????????????????????????????????????????????ConfigurationRoot????
????????????????????????????ConfigurationSection????????????y?????????????????????????????????????????????????????ConfigurationRoot?????????????路???????????????ConfigurationRoot??????ConfigurationProvider?????????路?????????????????????????????????????
????????????ConfigurationRoot??ConfigurationSection???????????????????写??????????????????????????????????????????????????????????????????????????????????ConfigurationPath?????????????澹珻onfigurationPath????????????????????路?????????????Combine???????????魏?????????????路????GetSectionKey??????????????路??????????Key????GetParentPath??????????路????????????路????
????1: public static class ConfigurationPath
????2: {
????3: public static string Combine(params string[] pathSegements) ;
????4: public static string Combine(IEnumerable<string> pathSegements) ;
????5: public static string GetSectionKey(string path) ;
????6: public static string GetParentPath(string path) ;
????7: }
????ConfigurationRoot
????ConfigurationRoot???????????????????????????????????????小????ConfigurationRoot?????????????????????????ConfigurationProvider???????味???????????写???????????????????????????????????????????Key??????????????????????????ConfigurationProvider????????????????????????Key??????????蔚??????ConfigurationProvider??TryGet????????梅???????True????????????????????????????????????????????????????????????????????????????????????ConfigurationProvider??Set??????
1: public class ConfigurationRoot: IConfigurationRoot
2: {
3: private IList<IConfigurationProvider> providers;
4:
5: public ConfigurationRoot(IList<IConfigurationProvider> providers)
6: {
7: this.providers = providers;
8: providers.ForEach(provider => provider.Load());
9: }
10:
11: public string this[string key]
12: {
13: get
14: {
15: string value = null;
16: return providers.Reverse().Any(p => p.TryGet(key?? out value))
17: ? value: null;
18: }
19: set
20: {
21: providers.ForEach(provider => provider.Set(key?? value));
22: }
23: }
24:
25: public IEnumerable<IConfigurationSection> GetChildren()=> this.GetChildren(null);
26:
27: public IConfigurationSection GetSection(string key) => new ConfigurationSection(this?? key);
28:
29: public void Reload() => providers.ForEach(provider => provider.Load());
30:
31: internal IEnumerable<IConfigurationSection> GetChildrenCore(string path)
32: {
33: return providers.Aggregate(Enumerable.Empty<string>()??
34: (seed?? source) => source.GetChildKeys(seed?? path?? ":"))
35: .Distinct()
36: .Select(key => GetSection(ConfigurationPath.Combine(path?? key)));
37: }
38: //???????
39: }
????ConfigurationRoot??????????卸???????????????????????????????????????????????????????????卸????????????????ConfigurationBuilder?械?ConfigurationProvider???懈????????????????????????????ConfigurationProvider“????????”?????????????????????械????????????????????????????ConfigurationProvider????ConfigurationBuilder?????伞?
????????????????????????GetChildrenCore???????????y???????????y?????????????y?????????????路????????????????????????????ConfigurationProvider??GetChildKeys???????????????????????y??Key???????????????????????y??ConfigurationSection??????????GetChildren?????校?????????????????????????????????????????y?????????GetSection?????校?????????????????路???????????????y??????????key??????y??路??????????ConfigurationSection????
????ConfigurationSection
????????????????????ConfigurationRoot??????????????????????????????y??ConfigurationSection?????????????路????????????y??ConfigurationRoot?????????ConfigurationRoot????????写??????????????ConfigurationProvider????????ConfigurationSection??????????????y???????写??????????????????????????????????????小?
1: public class ConfigurationSection: IConfigurationSection
2: {
3: private ConfigurationRoot root;
4: private string key;
5:
6: public string Key
7: {
8: get { return key ?? (key = ConfigurationPath.GetSectionKey(this.Path)); }
9: }
10: public string Path { get; private set; }
11: public string Value { get; set; }
12: public string this[string key]
13: {
14: get
15: {
16: return root[this.Path];
17: }
18:
19: set
20: {
21: root[this.Path] = Value;
22: }
23: }
24:
25: public ConfigurationSection(ConfigurationRoot root?? string path)
26: {
27: this.root = root;
28: this.Path = path;
29: }
30:
31: public IConfigurationSection GetSection(string key) => root.GetSection(ConfigurationPath.Combine(this.Path?? key));
32: public IEnumerable<IConfigurationSection> GetChildren() => root.GetChildren(this.Path);
33: //???????
34: }
????[1] ForEach???????????????????????IEnumerable<T>???????????????????????????????????????
??????
???路???
??????????????????
2023/3/23 14:23:39???写?貌??????????
2023/3/22 16:17:39????????????????????些??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???路???????路
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11