Example #1
0
        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 DontBreakInternalDictionaryStateWhenExceptionOccured()
        {
            var collection = new TransactionalCompositeKeyCollection <UserType, string, int>();

            collection.Add(new UserType(DateTime.Today), "Managers", 10);
            collection.Add(new UserType(DateTime.Today), "Analitics", 15);
            int expectedRecordsCount = 2;

            Assert.AreEqual(expectedRecordsCount, collection.Count);

            // adding an invalid object to the collection
            try
            {
                collection.Add(null, "Developers", 20);
            }
            catch (ArgumentNullException ex) { }
            Assert.AreEqual(expectedRecordsCount, collection.Count);

            collection.Add(new UserType(DateTime.Today), "Developers", 20);
            expectedRecordsCount = 3;
            Assert.AreEqual(expectedRecordsCount, collection.Count);
        }
        public void RollBackTransactionWhenExceptionOccured()
        {
            var collection = new TransactionalCompositeKeyCollection <UserType, string, int>();

            collection.Add(new UserType(DateTime.Now), null, 10);
        }