Exemple #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();
        }
Exemple #2
0
        public void ClearCollectionTest()
        {
            DateTime dt1 = new DateTime(2017, 03, 13);
            var      taskDoneCollection = new CompositeKeyCollection <UserType, string, int>();

            taskDoneCollection.Add(new UserType(dt1), "Developers", 10);
            taskDoneCollection.Add(new UserType(dt1), "Managers", 12);

            int currentCount  = taskDoneCollection.Count;
            int expectedCount = 0;

            Assert.AreNotEqual(expectedCount, currentCount);

            taskDoneCollection.Clear();
            Assert.AreEqual(expectedCount, taskDoneCollection.Count);
        }
Exemple #3
0
        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);
        }
Exemple #4
0
        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);
        }
Exemple #5
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");
        }
Exemple #6
0
        public void ContainsKeyAndValuesTest()
        {
            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);

            bool containsKey   = taskDoneCollection.ContainsKey(new UserType(dt1), "Managers");
            bool containsValue = taskDoneCollection.ContainsValue(12);

            Assert.IsTrue(containsKey);
            Assert.IsTrue(containsValue);

            bool NotExistedKey   = taskDoneCollection.ContainsKey(new UserType(dt1), "Analytics");
            bool NotExistedValue = taskDoneCollection.ContainsValue(15);

            Assert.IsFalse(NotExistedKey);
            Assert.IsFalse(NotExistedValue);
        }
Exemple #7
0
        public void AddingItemsToCompositeKeyCollectionTest()
        {
            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.Add(new UserType(todayDt.AddDays(1)), "Developers", 12);
            taskDoneCollection.Add(new UserType(todayDt.AddDays(1)), "Managers", 10);
            taskDoneCollection.Add(new UserType(todayDt.AddDays(1)), "Analiytics", 4);

            const int alreadyAddedRecords = 6;

            Assert.AreEqual(alreadyAddedRecords, taskDoneCollection.Count);
        }