Beispiel #1
0
 /// <summary>
 /// Convert an <see cref="EntityInfo"/> "SaveMap" with
 /// one or more <see cref="IEntitySaveMapper"/> instances.
 /// </summary>
 /// <remarks>
 /// See <see cref="EntitySaveMapper{STYPE, CTYPE}.ConvertBeforeSaveMap "/> for details
 /// </remarks>
 public static SaveMap ConvertBeforeSaveMap(
     this SaveMap saveMap,
     EntityInfoCreator entityInfoCreator,
     BeforeSaveEntityDelegate beforeSaveEntity,
     params IEntitySaveMapper[] saveMappers)
 {
     foreach (var saveMapper in saveMappers)
     {
         saveMapper.ConvertBeforeSaveMap(saveMap, entityInfoCreator, beforeSaveEntity);
     }
     return(saveMap);
 }
Beispiel #2
0
        /// <summary>
        /// Convert the SaveMap <see cref="EntityInfo"/> entries of the <see cref="CType"/>
        /// into EntityInfo entries for the <see cref="SType"/>.
        /// </summary>
        /// <param name="saveMap">
        /// The "SaveMap" passed into the <see cref="ContextProvider.BeforeSaveEntities"/> method.
        /// </param>
        /// <param name="entityInfoCreator">
        /// Function that creates a new <see cref="EntityInfo"/> for a
        /// given entity and optional <see cref="EntityState"/>.
        /// See <see cref="ContextProvider.CreateEntityInfo"/>.
        /// </param>
        /// <param name="beforeSaveEntity">
        /// Optional function to validate an individual entity before it can save;
        /// see <see cref="ContextProvider.BeforeSaveEntity"/>"/>;
        /// </param>
        /// <remarks>
        /// Updates the "SaveMap" by converting those <see cref="EntityInfo"/> objects
        /// constructed from the JSON "saveBundle" in terms of the client's entity type
        /// into corresponding EntityInfos expressed in terms of the server entity type.
        /// Converts with the <see cref="MapEntityToServer"/>.
        /// <para>
        /// Call it inside your <see cref="ContextProvider.BeforeSaveEntities"/>
        /// override or delegate method.
        /// </para>
        /// </remarks>
        public SaveMap ConvertBeforeSaveMap(
            SaveMap saveMap,
            EntityInfoCreator entityInfoCreator,
            BeforeSaveEntityDelegate beforeSaveEntity = null)
        {
            List <EntityInfo> cGroup, sGroup;

            if (saveMap.TryGetValue(cType, out cGroup))
            {
                saveMap.Remove(cType); // don't save CType entities
            }
            else
            {
                return(saveMap); // this CType is not in the saveMap
            }

            if (!saveMap.TryGetValue(sType, out sGroup))
            {
                sGroup = new List <EntityInfo>();
                saveMap.Add(sType, sGroup);
            }

            foreach (var cEntityInfo in cGroup)
            {
                var sEntity = MapEntityToServer(cEntityInfo);
                if (sEntity == null)
                {
                    continue;
                }

                var mappedEntityInfo = entityInfoCreator(sEntity, cEntityInfo.EntityState);
                mappedEntityInfo.OriginalValuesMap = MapOriginalValues(cEntityInfo.OriginalValuesMap);
                mappedEntityInfo.AutoGeneratedKey  = MapAutoGeneratedKey(sEntity, cEntityInfo.AutoGeneratedKey);
                mappedEntityInfo.ForceUpdate       = cEntityInfo.ForceUpdate;

                // TODO: Fix this deficiency
                // Unfortunately, UnmappedValuesMap is "protected internal" right now so can't copy
                //mappedEntityInfo.UnmappedValuesMap = entityInfo.UnmappedValuesMap;

                if (beforeSaveEntity == null || beforeSaveEntity(mappedEntityInfo))
                {
                    sGroup.Add(mappedEntityInfo);
                }
            }
            return(saveMap);
        }