Example #1
0
        private Type GetRuntimeType(MappingContext <T> ctx)
        {
            string strRuntimeInfo;

            if (ctx.Inputs.TryGetValue(EntityBaseKeys.RuntimeInfo, out strRuntimeInfo))
            {
                if (!DisambiguateRuntimeInfo)
                {
                    return(RuntimeInfo.FromFormValue(strRuntimeInfo)?.EntityType);
                }
                else
                {
                    RuntimeInfo runtimeInfo = strRuntimeInfo.Split(',')
                                              .Select(r => RuntimeInfo.FromFormValue(r))
                                              .OrderBy(a => !a.ToLite().RefersTo((Entity)(object)ctx.Value))
                                              .FirstEx();

                    return(runtimeInfo?.EntityType);
                }
            }
            else
            {
                return(ctx.Value?.GetType());
            }
        }
Example #2
0
        protected internal virtual ModifiableEntity ExtractEntity(ControllerBase controller, string prefix)
        {
            NameValueCollection form = controller.ControllerContext.HttpContext.Request.Form;

            var state = form[TypeContextUtilities.Compose(prefix, EntityBaseKeys.EntityState)];

            if (state.HasText())
            {
                return(Navigator.Manager.DeserializeEntity(state));
            }


            var key = TypeContextUtilities.Compose(prefix, EntityBaseKeys.RuntimeInfo);

            RuntimeInfo runtimeInfo = RuntimeInfo.FromFormValue(form[key]);

            if (runtimeInfo == null)
            {
                throw new ArgumentNullException("{0} not found in form request".FormatWith(key));
            }

            if (runtimeInfo.EntityType.IsEntity() && !runtimeInfo.IsNew)
            {
                return(Database.Retrieve(runtimeInfo.EntityType, runtimeInfo.IdOrNull.Value));
            }
            else
            {
                return(new ConstructorContext(controller).ConstructUntyped(runtimeInfo.EntityType));
            }
        }
Example #3
0
        public virtual T GetEntity(MappingContext <T> ctx)
        {
            string strRuntimeInfo;

            if (!ctx.Inputs.TryGetValue(EntityBaseKeys.RuntimeInfo, out strRuntimeInfo))
            {
                return(ctx.Value); //I only have some ValueLines of an Entity (so no Runtime, Id or anything)
            }
            RuntimeInfo runtimeInfo;

            if (!DesambiguateRuntimeInfo)
            {
                runtimeInfo = RuntimeInfo.FromFormValue(strRuntimeInfo);
            }
            else
            {
                runtimeInfo = strRuntimeInfo.Split(',')
                              .Select(r => RuntimeInfo.FromFormValue(r))
                              .OrderBy(a => !a?.ToLite().RefersTo((Entity)(object)ctx.Value))
                              .FirstEx();
            }

            if (runtimeInfo == null)
            {
                return(null);
            }

            if (typeof(T).IsEmbeddedEntity())
            {
                if (runtimeInfo.IsNew || ctx.Value == null)
                {
                    return(new ConstructorContext(ctx.Controller).Construct <T>());
                }

                return(ctx.Value);
            }
            else if (typeof(T).IsModelEntity())
            {
                if (runtimeInfo.IsNew || ctx.Value == null)
                {
                    return(new ConstructorContext(ctx.Controller).Construct <T>());
                }

                return(ctx.Value);
            }
            else
            {
                Entity identifiable = (Entity)(ModifiableEntity)ctx.Value;

                var result = GetEntity(ctx.Controller, runtimeInfo, identifiable);

                if (result is Entity && runtimeInfo.Ticks != null)
                {
                    ((Entity)(ModifiableEntity)result).ticks = runtimeInfo.Ticks.Value;
                }

                return(result);
            }
        }
Example #4
0
        protected internal virtual Lite <T> ExtractLite <T>(ControllerBase controller, string prefix)
            where T : class, IEntity
        {
            NameValueCollection form        = controller.ControllerContext.HttpContext.Request.Form;
            RuntimeInfo         runtimeInfo = RuntimeInfo.FromFormValue(form[TypeContextUtilities.Compose(prefix, EntityBaseKeys.RuntimeInfo)]);

            if (!runtimeInfo.IdOrNull.HasValue)
            {
                throw new ArgumentException("Could not create a Lite without an Id");
            }

            return((Lite <T>)runtimeInfo.ToLite());
        }
Example #5
0
        public Lite <S> Change(MappingContext <Lite <S> > ctx)
        {
            string strRuntimeInfo;

            if (!ctx.Inputs.TryGetValue(EntityBaseKeys.RuntimeInfo, out strRuntimeInfo)) //I only have some ValueLines of an Entity (so no Runtime, Id or anything)
            {
                return(TryModifyEntity(ctx, ctx.Value));
            }

            RuntimeInfo runtimeInfo = RuntimeInfo.FromFormValue(strRuntimeInfo);

            Lite <S> lite = (Lite <S>)ctx.Value;

            if (runtimeInfo == null)
            {
                return(null);
            }

            if (runtimeInfo.IsNew)
            {
                if (lite != null && lite.EntityOrNull != null && lite.EntityOrNull.IsNew)
                {
                    return(TryModifyEntity(ctx, lite));
                }

                return(TryModifyEntity(ctx, (Lite <S>)((Entity) new ConstructorContext(ctx.Controller).ConstructUntyped(runtimeInfo.EntityType)).ToLiteFat()));
            }

            if (lite == null)
            {
                return(TryModifyEntity(ctx, (Lite <S>)Database.RetrieveLite(runtimeInfo.EntityType, runtimeInfo.IdOrNull.Value)));
            }

            if (runtimeInfo.IdOrNull.Value == lite.IdOrNull && runtimeInfo.EntityType == lite.EntityType)
            {
                return(TryModifyEntity(ctx, lite));
            }

            return(TryModifyEntity(ctx, (Lite <S>)Database.RetrieveLite(runtimeInfo.EntityType, runtimeInfo.IdOrNull.Value)));
        }
Example #6
0
        private static T GetEntity(ControllerBase controller, RuntimeInfo runtimeInfo, Entity identifiable)
        {
            if (runtimeInfo.IsNew)
            {
                if (identifiable != null && identifiable.IsNew)
                {
                    return((T)(ModifiableEntity)identifiable);
                }
                else
                {
                    return(new ConstructorContext(controller).Construct <T>());
                }
            }

            if (identifiable != null && runtimeInfo.IdOrNull == identifiable.IdOrNull && runtimeInfo.EntityType == identifiable.GetType())
            {
                return((T)(ModifiableEntity)identifiable);
            }
            else
            {
                return((T)(ModifiableEntity)Database.Retrieve(runtimeInfo.EntityType, runtimeInfo.IdOrNull.Value));
            }
        }