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()); } }
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)); } }
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); } }
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()); }
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))); }
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)); } }