public Dictionary <string, Dependences> InitializeDependences() { Dictionary <string, Dependences> dependences = new Dictionary <string, Dependences>(); Dependences dependence = new Dependences(); dependence.Dependence = new Dictionary <string, List <object> >(); dependence.Dependence.Add("Privilege", new List <object>() { "Admin", "User", "Other" }); dependences["Users"] = dependence; return(dependences); }
/// <summary>Добавить зависимости между свойствами</summary> /// <param name="PropertyName">Имя исходного свойства</param> /// <param name="Dependences">Перечисление свойств, на которые исходное свойство имеет влияние</param> protected void PropertyDependence_Add([NotNull] string PropertyName, [NotNull, ItemNotNull] params string[] Dependences) { // Если не указано имя свойства, то это ошибка if (PropertyName is null) { throw new ArgumentNullException(nameof(PropertyName)); } // Блокируем критическую секцию для многопоточных операций lock (_PropertiesDependenciesSyncRoot) { // Если словарь зависимостей не существует, то создаём новый Dictionary <string, List <string> > dependencies_dictionary; if (_PropertiesDependenciesDictionary is null) { dependencies_dictionary = new Dictionary <string, List <string> >(); _PropertiesDependenciesDictionary = dependencies_dictionary; } else { dependencies_dictionary = _PropertiesDependenciesDictionary; } // Извлекаем из словаря зависимостей список зависящих от указанного свойства свойств (если он не существует, то создаём новый var dependencies = dependencies_dictionary.GetValueOrAddNew(PropertyName, () => new List <string>()); // Перебираем все зависимые свойства среди указанных исключая исходное свойство foreach (var dependence_property in Dependences.Where(name => name != PropertyName)) { // Если список зависимостей уже содержит зависящее свойство, то пропускаем его if (dependencies.Contains(dependence_property)) { continue; } // Проверяем возможные циклы зависимостей var invoke_queue = IsLoopDependency(PropertyName, dependence_property); if (invoke_queue != null) // Если цикл найден, то это ошибка { throw new InvalidOperationException($"Попытка добавить зависимость между свойством {PropertyName} и (->) {dependence_property} вызывающую петлю зависимости [{string.Join(">", invoke_queue)}]"); } // Добавляем свойство в список зависимостей dependencies.Add(dependence_property); foreach (var other_property in dependencies_dictionary.Keys.Where(name => name != PropertyName)) { var d = dependencies_dictionary[other_property]; if (!d.Contains(PropertyName)) { continue; } invoke_queue = IsLoopDependency(other_property, dependence_property); if (invoke_queue != null) // Если цикл найден, то это ошибка { throw new InvalidOperationException($"Попытка добавить зависимость между свойством {other_property} и (->) {dependence_property} вызывающую петлю зависимости [{string.Join(">", invoke_queue)}]"); } d.Add(dependence_property); } } } }