当前位置:   article > 正文

C# 支持复用字典的复杂元素_c# 重复的字典类型

c# 重复的字典类型

C# 支持复用字典的复杂元素

演示项目 CloudResourceConfigs.zip





namespace System.Data.CloudResources.Common
{
    using System.Collections.Generic;

    /// <summary>
    /// 支持根据元素相同字典(描述哪一些元素是相同的字典),查找元素的信息。
    /// </summary>
    class TryGetValueFunc<TValue>
    {
        public TryGetValueFunc(Dictionary<string, string> sameItemDic, Dictionary<string, TValue> itemDic, IEqualityComparer<string> keyComparer)
        {
            this.SameItemDic = sameItemDic;
            this.ItemDic = itemDic;
            this.DefaultKeyComparer = keyComparer;
        }

        /// <summary>
        /// 元素相同字典(描述哪一些元素是相同的字典)。
        /// </summary>
        protected Dictionary<string, string> SameItemDic { get; set; }

        /// <summary>
        /// 元素的信息的字典。
        /// </summary>
        protected Dictionary<string, TValue> ItemDic { get; set; }

        private IEqualityComparer<string> DefaultKeyComparer { get; set; }

        public bool TryGetValue(string key, out TValue value)
        {
            return TryGetValue(key, null, out value);
        }

        public bool TryGetValue(string key, List<string> tryGetKeyList, out TValue value)
        {
            var isFindInDic = false;
            value = default(TValue);
            var safeItemDic = ItemDic;
            if (safeItemDic != null && key != null)
            {
                isFindInDic = safeItemDic.TryGetValue(key, out value);
                if (tryGetKeyList == null) tryGetKeyList = new List<string>(10);
                tryGetKeyList.Add(key);

                if (!isFindInDic || object.Equals(value, default(TValue)))
                {
                    var safeSameItemDic = SameItemDic;
                    string newKey;
                    if (safeSameItemDic != null && safeSameItemDic.TryGetValue(key, out newKey) &&
                        newKey != null && !Contains(tryGetKeyList, newKey, DefaultKeyComparer) &&
                        TryGetValue(newKey, tryGetKeyList, out value))
                    {
                        isFindInDic = true;
                    }
                }
            }

            return isFindInDic;
        }

        private static bool Contains(IList<string> list, string value, IEqualityComparer<string> comparer)
        {
            bool isContains = false;
            foreach (var item in list)
            {
                if (comparer.Equals(item, value))
                {
                    isContains = true;
                    break;
                }
            }
            return isContains;
        }
    }
}


namespace System.Data.CloudResources.Common
{
    using System.Collections.Generic;

    /// <summary>
    /// 支持根据元素相同字典(描述哪一些元素是相同的字典),查找元素的信息。
    /// 支持先查 ProductId(PID) ,再查语言(LanguageId),最后查默认值(DefaultId)。
    /// </summary>
    class TryGetProductIdInfoFunc<TValue> : TryGetValueFunc<TValue>
    {
        public TryGetProductIdInfoFunc(Dictionary<string, string> sameItemDic,
            Dictionary<string, TValue> itemDic, IEqualityComparer<string> keyComparer)
            : base(sameItemDic, itemDic, keyComparer)
        {
        }

        public TValue GetProductIdInfo(string productId, string languageId)
        {
            TValue value;
            TryGetProductIdInfo(productId, languageId, KeyInfo.DefaultId, out value);
            return value;
        }

        public TValue GetProductIdInfo(string productId, string languageId, string defaultId)
        {
            TValue value;
            TryGetProductIdInfo(productId, languageId, defaultId, out value);
            return value;
        }

        public bool TryGetProductIdInfo(out TValue value)
        {
            return TryGetProductIdInfo(KeyInfo.CurrentProductId, KeyInfo.CurrentLanguageId, KeyInfo.DefaultId, out value);
        }

        public bool TryGetProductIdInfo(string productId, string languageId, out TValue value)
        {
            return TryGetProductIdInfo(productId, languageId, KeyInfo.DefaultId, out value);
        }

        public bool TryGetProductIdInfo(string productId, string languageId, string defaultId, out TValue value)
        {
            var tryGetKeyList = new List<string>(20);
            var isFindInDic = TryGetValue(productId, tryGetKeyList, out value);
            if ((!isFindInDic || object.Equals(value, default(TValue))) &&
                TryGetValue(languageId, tryGetKeyList, out value))
            {
                isFindInDic = true;
            }
            if ((!isFindInDic || object.Equals(value, default(TValue))) &&
                !KeyInfo.DefaultKeyComparer.Equals(defaultId, languageId) &&
                TryGetValue(defaultId, tryGetKeyList, out value))
            {
                isFindInDic = true;
            }
            return isFindInDic;
        }
    }
}

namespace System.Data.CloudResources.Common
{
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// 按照分组信息字典,对元素集合进行分组。
    /// </summary>
    /// <typeparam name="TItem"></typeparam>
    class GetItemsByGroupFunc<TItem>
    {
        public GetItemsByGroupFunc(TItem[] items, Dictionary<string, int[]> itemsGroupDic)
        {
            this.Items = items;
            this.ItemsGroupDic = itemsGroupDic;
        }

        /// <summary>
        /// 元素的分组信息字典。
        /// 可能为 null 。为 null 表示所有元素的分组的下标都为 0 。
        /// </summary>
        private Dictionary<string, int[]> ItemsGroupDic { get; set; }

        /// <summary>
        /// 元素的信息的列表。
        /// </summary>
        private TItem[] Items { get; set; }

        public TItem[] GetItemsByGroup(int groupIndex, string languageId)
        {
            return GetItemsByGroup(groupIndex, languageId, KeyInfo.DefaultLanguageId);
        }

        public TItem[] GetItemsByGroup(int groupIndex, string languageId, string defaultLanguageId)
        {
            TItem[] safeItems = Items;
            if (safeItems == null) safeItems = new TItem[0];
            int[] findGroup = GetItemsGroup(languageId, defaultLanguageId);

            TItem[] findItemsByGroup;
            if (findGroup == null)
            {
                if (groupIndex == 0)
                {
                    findItemsByGroup = safeItems;
                }
                else
                {
                    findItemsByGroup = new TItem[0];
                }
            }
            else
            {
                findItemsByGroup = GetItemsByGroupCore(groupIndex, safeItems, findGroup);
            }
            return findItemsByGroup;
        }

        private TItem[] GetItemsByGroupCore(int groupIndex, TItem[] safeItems, int[] findGroup)
        {
            TItem[] findItemsByGroup;
            if (groupIndex >= 0 && groupIndex < findGroup.Length)
            {
                var beginIndexForGroup = GetBeginIndexForGroup(groupIndex, findGroup);
                var lengthForGroup = findGroup[groupIndex];
                lengthForGroup = Math.Min(lengthForGroup, safeItems.Length - beginIndexForGroup);
                lengthForGroup = Math.Max(lengthForGroup, 0);
                findItemsByGroup = new TItem[lengthForGroup];
                if (lengthForGroup > 0)
                {
                    Array.Copy(safeItems, beginIndexForGroup, findItemsByGroup, 0, lengthForGroup);
                }
            }
            else
            {
                findItemsByGroup = new TItem[0];
            }
            return findItemsByGroup;
        }

        private int[] GetItemsGroup(string languageId, string defaultLanguageId)
        {
            int[] safeGroup = null;
            var safeGroupDic = ItemsGroupDic;
            if (languageId != null && safeGroupDic != null &&
                !safeGroupDic.TryGetValue(languageId, out safeGroup))
            {
                safeGroupDic.TryGetValue(defaultLanguageId, out safeGroup);
            }
            return safeGroup;
        }

        private int GetBeginIndexForGroup(int groupIndex, int[] findGroup)
        {
            var beginIndexForGroup = 0;
            for (int index = 0; index < groupIndex; ++index)
            {
                beginIndexForGroup += findGroup[index];
            }
            return beginIndexForGroup;
        }
    }
}


namespace System.Data.CloudResources.Common
{
    using System;
    using System.Collections.Generic;

    public static class KeyInfo
    {
        private static string currentLanguageId = "chinese";

        public static int DefaultCapacity
        {
            get { return 40; }
        }

        public static StringComparer DefaultKeyComparer
        {
            get { return StringComparer.OrdinalIgnoreCase; }
        }

        public static string DefaultId { get { return "default"; } }

        public static string DefaultLanguageId
        {
            get { return "english"; }
        }

        public static string CurrentLanguageId
        {
            get { return currentLanguageId; }
            set
            {
                if (!DefaultKeyComparer.Equals(currentLanguageId, value))
                {
                    currentLanguageId = value;
                    OnCurrentLanguageIdChanged();
                }
            }
        }

        public static string CurrentProductId
        {
            get { return "englishProductId"; }
        }

        public static string UserDataProductName
        {
            get { return "UserDataProductName"; }
        }

        public static event EventHandler CurrentLanguageIdChanged;

        internal static IDictionary<string, string> GetAllProductIdLanguageId()
        {
            var dic = new KeyDictionary<string>();
            dic["productid-english"] = "english";
            return dic;
        }

        private static void OnCurrentLanguageIdChanged()
        {
            CurrentLanguageIdChanged?.Invoke(null, EventArgs.Empty);
        }
    }
}

namespace System.Data.CloudResources.Common
{
    using System.Collections.Generic;

    /// <summary>
    /// 默认创建 Key 忽略大小写的字典。
    /// <para>
    /// 如果 <see cref="TValue"/> 是 string ,
    /// 向本对象对应的配置文件添加(或删除)成员,
    /// 本对象会添加(或删除)成员
    /// </para>
    /// <para>
    /// 如果 <see cref="TValue"/> 是类型对象(例如 <see cref="KeyDictionary{TValue}"/>),
    /// 向本对象对应的配置文件添加(或删除)成员,
    /// 本对象会添加(或不会删除)成员。
    /// </para>
    /// </summary>
    /// <typeparam name="TValue"></typeparam>
    public class KeyDictionary<TValue> : Dictionary<string, TValue>
    {
        public KeyDictionary() : base(KeyInfo.DefaultCapacity, KeyInfo.DefaultKeyComparer)
        {
        }

        public KeyDictionary(int capacity) : base(capacity, KeyInfo.DefaultKeyComparer)
        {
        }

        public KeyDictionary(IDictionary<string, TValue> dictionary) : base(dictionary, KeyInfo.DefaultKeyComparer)
        {
        }

        public KeyDictionary(int capacity, IEqualityComparer<string> comparer) : base(capacity, comparer)
        {
        }
    }
}



  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号