static void Main(string[] args) { // В проекте CompositeKeyCollection имеется 3 реализации класса коллекций с композитными (составными) ключами: // 1. CompositeKeyCollection<TKey1, Tkey2, TData> - обычная обобщенная реализация класса коллекции (не потокобезопасная) // 2. ThreadSafeCompositeKeyCollection<Tkey1, Tkey2, TData> - потокобезопасная реализация класса CompositeKeyCollection<TKey1, TKey2, TData> // 3. TransactionalCompositeKeyCollection<TKey1, TKey2, TData> - класс коллекции с комопозитными ключами и поддержкой транзакционности на операции вставки и удаления записей в коллекцию (PS.: пока еще не потокобезопасная) // Примеры использования коллекций: // 1. CompositeKeyCollection<TKey1, Tkey2, TData> var currentDate = DateTime.Today; var tasksDoneCollection1 = new CompositeKeyCollection <UserType, string, int>(); tasksDoneCollection1.Add(new UserType(currentDate), "Developers", 10); tasksDoneCollection1.Add(new UserType(currentDate), "Managers", 12); tasksDoneCollection1.Add(new UserType(currentDate.AddDays(1)), "Developers", 7); PrintCollection(tasksDoneCollection1); var key1 = new UserType(currentDate); var key2 = "Developers"; tasksDoneCollection1.Remove(key1, key2); PrintCollection(tasksDoneCollection1); key1 = new UserType(currentDate.AddDays(1)); var items = tasksDoneCollection1.Find(key1, key2); Console.WriteLine("Items found: {0}", items.Count); var value = tasksDoneCollection1[key1, key2]; Console.WriteLine("Value in dictionary: {0}", value); tasksDoneCollection1.Clear(); // 2. ThreadSafeCompositeKeyCollection<Tkey1, Tkey2, TData> _collection2 = new ThreadSafeCompositeKeyCollection <UserType, string, int>(); Task.Run(() => RunFirstWriteThread()); Task.Run(() => RunSecondWriteThread()); Task.Run(() => RunShowKeyCountThread()); // 3.TransactionalDictionary<TKey1, TKey2, TData> var collection3 = new TransactionalCompositeKeyCollection <UserType, string, int>(); collection3.Add(new UserType(DateTime.Today), "Managers", 10); collection3.Add(new UserType(DateTime.Today), "Analitics", 15); // adding an invalid object to the collection try { collection3.Add(null, "Developers", 20); } catch (ArgumentNullException ex) { } Console.Read(); }
public void RemovingItemsFromCompositeKeyCollectionTest() { DateTime todayDt = DateTime.Today; var taskDoneCollection = new CompositeKeyCollection <UserType, string, int>(); taskDoneCollection.Add(new UserType(todayDt), "Developers", 10); taskDoneCollection.Add(new UserType(todayDt), "Managers", 12); taskDoneCollection.Add(new UserType(todayDt), "Analiytics", 15); taskDoneCollection.Remove(new UserType(todayDt), "Developers"); taskDoneCollection.Remove(new UserType(todayDt), "Analiytics"); const int expectedRecordsCount = 1; int actualRecordsCount = taskDoneCollection.Count; Assert.AreEqual(expectedRecordsCount, actualRecordsCount); bool keyExist = taskDoneCollection.ContainsKey(new UserType(todayDt), "Managers"); Assert.IsTrue(keyExist); }
static void Main(string[] args) { var collection = new CompositeKeyCollection <int, string, Emploee>(); collection.Add(1, "Nikita", new Emploee("Java Developer", 340_000)); collection.Add(1, "Valera", new Emploee("JS Developer", 345_000)); collection.Add(2, "Nikita", new Emploee("C# Developer", 330_000)); var id1 = collection.GetById(1); PrintValues(id1); Console.WriteLine(); collection.Remove(1, "Valera"); id1 = collection.GetById(1); PrintValues(id1); Console.WriteLine(); var nikitas = collection.GetByName("Nikita"); PrintValues(nikitas); Console.WriteLine(); var nikita = collection[2, "Nikita"]; Console.WriteLine(nikita); Console.WriteLine(); collection.Add(2, "Vladimir", new Emploee("Manager", 450_000)); var id2 = collection.GetById(2); PrintValues(id2); Console.WriteLine(); collection[2, "Nikita"] = new Emploee("Senior C# Developer", 360_000); nikita = collection[2, "Nikita"]; Console.WriteLine(nikita); }