Example #1
0
        public void Check_That_Custom_Comparer_Factory_Returns_Publisher_Comparer()
        {
            var customComparerFactory = new CustomComparerFactory();

            ICustomComparer comparer = customComparerFactory.GetComparer(typeof(Publisher));

            Assert.IsInstanceOfType(comparer, typeof(PublisherComparer));
        }
Example #2
0
 /// <summary>
 /// Adds a specific comparer for the givenn sort expression.
 /// </summary>
 /// <param name="sortExpression"></param>
 /// <param name="comparer"></param>
 public void AddCustomSort(string sortExpression, ICustomComparer <T> comparer)
 {
     if (_sortDictionary.ContainsKey(sortExpression))
     {
         _sortDictionary[sortExpression] = comparer;
     }
     else
     {
         _sortDictionary.Add(sortExpression, comparer);
     }
 }
Example #3
0
 public static void Sort(ICustomComparer icomparer, int[][] array)
 {
     for (int i = 0; i < array.Length; i++)
     {
         for (int j = 0; j < array.Length; j++)
         {
             if (icomparer.Comparer(array[i], array[j]) == -1)
             {
                 Swap(array, i, j);
             }
         }
     }
 }
        /// <summary>
        /// Sort a matrix of integers
        /// </summary>
        /// <param name="matrix">Sortable matrix</param>
        /// <param name="compare"></param>
        public static void Sorting(int[][] matrix, ICustomComparer compare)
        {
            if (matrix == null || compare == null)
            {
                throw new ArgumentNullException();
            }

            for (int i = 0; i < matrix.Length - 1; i++)
            {
                for (int j = i + 1; j < matrix.Length; j++)
                {
                    if (compare.CompareArrays(matrix[i], matrix[j]) > 0)
                    {
                        Swap(ref matrix[i], ref matrix[j]);
                    }
                }
            }
        }
        /// <summary>
        /// Method that implements binary search in sorted array of generic type items
        /// </summary>
        /// <typeparam name="T">T parameter type</typeparam>
        /// <param name="array">Sorted array of T type items</param>
        /// <param name="key">Item to search</param>
        /// <param name="comparer">Instance of IComparer interface type</param>
        /// <returns>Number of position of wanted element in given array</returns>
        public static int BinarySearchGeneric <T>(this T[] array, T key, ICustomComparer <T> comparer)
        {
            InputValidation(array, key, comparer.Compare);

            if (comparer is null)
            {
                if (key is IComparable && array[0] is IComparable)
                {
                    return(BinarySearchGeneric(array, key, Comparer <T> .Default.Compare));
                }
                else
                {
                    throw new ArgumentException(nameof(comparer));
                }
            }

            return(BinarySearchGeneric(array, key, comparer.Compare));
        }
        public void Synchronize(TNorthwindEntity northwindEntity)
        {
            DbSet <TEntity> tEntityDbSet = _gameStoreDbContext.Set <TEntity>();

            if (northwindEntity.IsProxy())
            {
                northwindEntity = northwindEntity.UnProxy(_northwindDbContext);
            }

            var entityFromNorthwind = Mapper.Map <TEntity>(northwindEntity);

            string northwindIdPropertyName = typeof(TNorthwindEntity).IdentifierPropertyName();
            var    northwindEntityId       = (int)northwindEntity.GetPropValue(northwindIdPropertyName);

            TEntity gameStoreEntity = tEntityDbSet
                                      .FirstOrDefault(x => x.NorthwindId == northwindEntityId);

            if (gameStoreEntity == null)
            {
                tEntityDbSet.Add(entityFromNorthwind);
            }
            else
            {
                if (gameStoreEntity.IsProxy())
                {
                    gameStoreEntity = gameStoreEntity.UnProxy(_gameStoreDbContext);
                }

                string       gameStoreIdPropertyName = typeof(TEntity).IdentifierPropertyName();
                PropertyInfo idPropertyInfo          = typeof(TEntity).GetProperty(gameStoreIdPropertyName);
                var          gameStoreEntityId       = (int)gameStoreEntity.GetPropValue(gameStoreIdPropertyName);
                idPropertyInfo.SetValue(entityFromNorthwind, gameStoreEntityId, null);

                ICustomComparer comparer = _comparerFactory.GetComparer(typeof(TEntity));

                if (!comparer.AreEqual(gameStoreEntity, entityFromNorthwind))
                {
                    _gameStoreDbContext.Entry(gameStoreEntity).CurrentValues.SetValues(entityFromNorthwind);
                }
            }
        }
Example #7
0
        public static bool AreSimilar <T>(T first, T second)
        {
            //Return true for both references null or same
            if (first == null || second == null || object.ReferenceEquals(first, second))
            {
                return(true);
            }

            //Return false for different types
            if (!first.GetType().Equals(second.GetType()))
            {
                return(false);
            }

            //Compare directly values for primitive and string types
            if ((first.GetType().IsPrimitive || (typeof(string).Equals(second.GetType()))))
            {
                Compare = compfactory.CompareObject("Primitive");
                return(Compare.compare <T>(first, second));
            }

            //compare arrays
            if (first.GetType().IsArray&& second.GetType().IsArray)
            {
                Compare = compfactory.CompareObject("Array");
                return(Compare.compare <T>(first, second));
            }

            else
            {
                if (first.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public).Length > 0)
                {
                    Compare = compfactory.CompareObject("Class");
                    return(Compare.compare <T>(first, second));
                }
            }
            return(true);
        }
        public void Synchronize(Order_Detail orderDetail)
        {
            DbSet <OrderItem>    gameStoreDbSet = _gameStoreDbContext.Set <OrderItem>();
            DbSet <Order_Detail> northwindDbSet = _northwindDbContext.Set <Order_Detail>();

            if (orderDetail.IsProxy())
            {
                orderDetail = orderDetail.UnProxy(_northwindDbContext);
            }

            var northwindOrderItem = Mapper.Map <OrderItem>(orderDetail);

            OrderItem gameStoreOrderItem = gameStoreDbSet
                                           .FirstOrDefault(x => x.NorthwindOrderId == orderDetail.OrderID &&
                                                           x.NorthwindProductId == orderDetail.ProductID);

            if (gameStoreOrderItem == null)
            {
                _gameStoreDbContext.Set <OrderItem>().Add(northwindOrderItem);
            }
            else
            {
                if (gameStoreOrderItem.IsProxy())
                {
                    gameStoreOrderItem = gameStoreOrderItem.UnProxy(_gameStoreDbContext);
                }

                northwindOrderItem.OrderItemId = gameStoreOrderItem.OrderItemId;

                ICustomComparer comparer = _comparerFactory.GetComparer(typeof(OrderItem));

                if (!comparer.AreEqual(gameStoreOrderItem, northwindOrderItem))
                {
                    _gameStoreDbContext.Entry(gameStoreOrderItem).CurrentValues.SetValues(northwindOrderItem);
                }
            }
        }
        public ICustomComparer CompareObject(object type)
        {
            ICustomComparer customobj = null;

            switch (type)
            {
            case "Primitive":
                customobj = new PrimitiveTypeComparer();
                break;

            case "Array":
                customobj = new ArrayComparer();
                break;

            case "IList":
                customobj = new ListComparer();
                break;

            case "Class":
                customobj = new ReferenceTypeComparer();
                break;
            }
            return(customobj);
        }
Example #10
0
        public ICustomComparer GetComparer(Type type)
        {
            ICustomComparer result = _customComparers.ContainsKey(type) ? _customComparers[type]() : null;

            return(result);
        }