/// <summary> /// Adds an item to the collection. /// </summary> private void AddItem(TItem item) { Check.NotNull(item, nameof(item)); if (item.Id == null) { throw new ArgumentException("Id must not be null.", nameof(item)); } if (item.Code == null) { throw new ArgumentException("Code must not be null.", nameof(item)); } lock (_lock) { // Check uniqueness of Id, Code and Mappings. if (_rdcId.Contains(item.Id)) { throw new ArgumentException($"Item with Id '{item.Id}' already exists within the collection.", nameof(item)); } if (_rdcCode.Contains(ConvertCode(item.Code !))) { throw new ArgumentException($"Item with Code '{item.Code!}' already exists within the collection.", nameof(item)); } if (item.HasMappings) { foreach (var map in item.Mappings) { var key = new MappingsKey { Name = map.Key, Value = map.Value }; if (_mappingsDict.ContainsKey(key)) { throw new ArgumentException($"Item with Mapping Name '{key.Name}' and Value '{key.Value}' already exists within the collection."); } } } // Once validated they can be added to underlying collections. _rdcId.Add(item); _rdcCode.Add(item); if (item.HasMappings) { foreach (var map in item.Mappings) { _mappingsDict.Add(new MappingsKey { Name = map.Key, Value = map.Value }, item.Code !); } } } }
/// <summary> /// Adds an item to the collection. /// </summary> private void AddItem(TItem item) { if (item == null) { throw new ArgumentNullException("item"); } lock (_lock) { // Check uniqueness of Id, Code and Mappings. if (_rdcId.Contains(item.Id)) { throw new ArgumentException(string.Format("Item with Id '{0}' already exists within the collection.", item.Id.ToString())); } if (_rdcCode.Contains(ConvertCode(item.Code))) { throw new ArgumentException(string.Format("Item with Code '{0}' already exists within the collection.", item.Code.ToString())); } if (item.HasMappings) { foreach (var map in item.Mappings) { var key = new MappingsKey { Name = map.Key, Value = map.Value }; if (_mappingsDict.ContainsKey(key)) { throw new ArgumentException($"Item with Mapping Name '{key.Name}' and Value '{key.Value}' already exists within the collection."); } } } // Once validated they can be added to underlying collections. _rdcId.Add(item); _rdcCode.Add(item); if (item.HasMappings) { foreach (var map in item.Mappings) { _mappingsDict.Add(new MappingsKey { Name = map.Key, Value = map.Value }, item.Code); } } } }