Exemple #1
0
        /// <summary>
        /// TODO, need a serious refactoring...
        /// </summary>
        public virtual void SetValue <TTarget>(object target, object source, PropertyPath targetPropertyPath, Mapper mapper, IService service, params Expression <Func <TTarget, object> >[] collectionsOwningItems)
            where TTarget : class
        {
            Asserts <ArgumentNullException> .IsNotNull(target);

            Asserts <ArgumentNullException> .IsNotNull(source);

            Asserts <ArgumentNullException> .IsNotNull(targetPropertyPath);

            Asserts <ArgumentNullException> .IsNotNull(mapper);

            Asserts <ArgumentNullException> .IsNotNull(collectionsOwningItems);

            Asserts <InvalidOperationException> .IsTrue(CanConvert());

            var  targetFirstPropertyInfo = targetPropertyPath.FirstPropertyInfo;
            var  targetLastPropertyInfo  = targetPropertyPath.LastPropertyInfo;
            bool nullInChain             = false;
            var  sourceValue             = GetValue(source, propertyInfos_, out nullInChain);

            if (nullInChain) //Accomodate nulls
            {
                if (targetPropertyPath.IsReferenceId())
                {
                    DoSetValue(target, targetFirstPropertyInfo, 0);
                }
                //else ignore
            }
            else
            {
                //The target object chain has been walked thru, recreated by GetInstance
                if (service != null)
                {
                    if (targetPropertyPath.IsReference())
                    {
                        if (sourceValue == null)
                        {
                            DoSetValue(target, targetFirstPropertyInfo, null);
                        }
                        else
                        {
                            int id = Convert.ToInt32(sourceValue);
                            Requires <InvalidKeyException> .GreaterOrEqual(id, -1);

                            if (id == -1)
                            {
                                //Ignore
                                return;
                            }
                            if (id == 0)
                            {
                                DoSetValue(target, targetFirstPropertyInfo, null);
                            }
                            if (id > 0)
                            {
                                var reference = service.GetPocoById(targetFirstPropertyInfo.PropertyType, id);
                                Requires <InvalidOperationException> .IsNotNull(reference);

                                DoSetValue(target, targetFirstPropertyInfo, reference);
                            }
                        }
                    }
                    else if (targetPropertyPath.IsGenericList())
                    {
                        //TODO add a test where the conversion of collection is not x => x

                        var refetchedTarget = service.GetPocoById(target.GetType(), GetId(target));
                        var targetValue     = GetValue(target, targetPropertyPath.propertyInfos_, out nullInChain);

                        var modelList     = targetValue as IList;
                        var viewModelList = sourceValue as IList;

                        var clonedModelList = new ArrayList(modelList);
                        foreach (var item in clonedModelList)
                        {
                            var viewModelListItem = GetMatchingItem(viewModelList, item);
                            if (viewModelListItem == null)
                            {
                                CallRemove(modelList, item);
                                if (collectionsOwningItems.Any(x => (ExpressionHelper.GetExpressionText(x) == targetLastPropertyInfo.Name)))
                                {
                                    CallRemoveFromPersistence(service, item);
                                }
                            }
                            else
                            {
                                CallMap(mapper, viewModelListItem, item, service);
                            }
                        }
                        foreach (var newItem in viewModelList)
                        {
                            if (GetId(newItem) != 0)
                            {
                                continue;
                            }

                            var mappedItem = CreateListItemInstanceOf(targetLastPropertyInfo);
                            CallMap(mapper, newItem, mappedItem, service);
                            CallAdd(modelList, mappedItem);
                        }
                    }
                    else
                    {
                        var targetInstance = GetInstance(target, targetPropertyPath);
                        DoSetValue(targetInstance, targetLastPropertyInfo, conversion_(sourceValue));
                    }
                    service.Modify(target as PocoBase);
                    //Log.Verbose("SetValue applied for {0} to {1}", targetPropertyPath.Path, target.ToString());
                }
                if (service == null)
                {
                    var targetInstance = GetInstance(target, targetPropertyPath);
                    if (targetPropertyPath.IsGenericList())
                    {
                        //This replaces the collection
                        var collectionInstance = sourceValue == null ? null : CreateCollection(sourceValue, targetLastPropertyInfo, mapper, service);
                        DoSetValue(targetInstance, targetLastPropertyInfo, conversion_(collectionInstance));
                        //TODO add a test where the conversion of collection is not x => x
                    }
                    else
                    {
                        DoSetValue(targetInstance, targetLastPropertyInfo, conversion_(sourceValue));
                    }
                }
            }
        }
Exemple #2
0
        public void Remove(PropertyPath targetPropertyPath)
        {
            Asserts <ArgumentNullException> .IsNotNull(targetPropertyPath);

            map_.Remove(targetPropertyPath);
        }