Пример #1
0
        public void FindRecordsByCompositeKeyTest()
        {
            DateTime dt1 = new DateTime(2017, 03, 13);
            DateTime dt2 = new DateTime(2017, 03, 14);

            var taskDoneCollection = new CompositeKeyCollection <UserType, string, int>();

            taskDoneCollection.Add(new UserType(dt1), "Developers", 10);
            taskDoneCollection.Add(new UserType(dt1), "Managers", 12);
            taskDoneCollection.Add(new UserType(dt1), "Analiytics", 15);
            taskDoneCollection.Add(new UserType(dt2), "Developers", 12);
            taskDoneCollection.Add(new UserType(dt2), "Managers", 10);
            taskDoneCollection.Add(new UserType(dt2), "Analiytics", 4);

            var key1  = new UserType(dt1);
            var key2  = "Developers";
            var pairs = taskDoneCollection.Find(key1, key2);
            int expectedRecordsCount = 1;

            Assert.AreEqual(expectedRecordsCount, pairs.Count, "Just one key value pair was expected");

            pairs = taskDoneCollection.Find(key1, null);
            expectedRecordsCount = 3;
            Assert.AreEqual(expectedRecordsCount, pairs.Count, "Three records was expected");

            pairs = taskDoneCollection.Find(null, key2);
            expectedRecordsCount = 2;
            Assert.AreEqual(expectedRecordsCount, pairs.Count, "Only two records was expected");
        }
Пример #2
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();
        }