예제 #1
0
        public void InsertOrUpdateReadId <TKeyProperties, TValueProperties>(
            IEnumerable <TEntityInterface> newItems,
            Expression <Func <TEntityInterface, TKeyProperties> > keySelector,
            Expression <Func <TEntityInterface, TValueProperties> > valueSelector)
        {
            var keyPropertiesHandler   = new PropertySelectorExpression <TEntityInterface, TKeyProperties>(keySelector);
            var valuePropertiesHandler = new PropertySelectorExpression <TEntityInterface, TValueProperties>(valueSelector);

            foreach (var newItem in newItems)
            {
                var filterOld            = keyPropertiesHandler.BuildComparisonPredicate(newItem);
                TEntityInterface oldItem = Reflection.Where(this.Query(), filterOld).ToList().SingleOrDefault();

                if (oldItem == null)
                {
                    _logger.Trace(() => "Creating " + EntityName + " " + keyPropertiesHandler.ToString(newItem) + ".");
                    Save(new[] { newItem }, null, null);
                }
                else
                {
                    newItem.ID = oldItem.ID;

                    var  compareValue = valuePropertiesHandler.BuildComparisonPredicate(newItem);
                    bool same         = compareValue.Compile().Invoke(oldItem);
                    if (!same)
                    {
                        // Copy value from newItem to oldItem (instead of simply saving newItem) to keep the old value of properties that are not covered with the given interfaces.
                        valuePropertiesHandler.Assign(oldItem, newItem);
                        Save(null, new[] { oldItem }, null);
                        _logger.Trace(() => "Updating " + EntityName + " " + keyPropertiesHandler.ToString(newItem) + ".");
                    }
                    else
                    {
                        _logger.Trace(() => "Already exists " + EntityName + " " + keyPropertiesHandler.ToString(newItem) + ".");
                    }
                }
            }
        }
예제 #2
0
        public void InsertOrReadId <TProperties>(
            IEnumerable <TEntityInterface> newItems,
            Expression <Func <TEntityInterface, TProperties> > keySelector)
        {
            var keySelectorHandler = new PropertySelectorExpression <TEntityInterface, TProperties>(keySelector);

            foreach (var newItem in newItems)
            {
                var  filterLoad = keySelectorHandler.BuildComparisonPredicate(newItem);
                Guid id         = Reflection.Where(this.Query(), filterLoad).Select(e => e.ID).FirstOrDefault();

                if (id == default(Guid))
                {
                    _logger.Trace(() => "Creating " + EntityName + " " + keySelectorHandler.ToString(newItem) + ".");
                    Save(new[] { newItem }, null, null);
                }
                else
                {
                    _logger.Trace(() => "Already exists " + EntityName + " " + keySelectorHandler.ToString(newItem) + ".");
                    newItem.ID = id;
                }
            }
        }