private object GetPropertyChainValue(EntityRecord record, string propertyChain)
        {
            var rec = record;

            using (rec.Session.ElevateRead()) {
                if (!propertyChain.Contains('.'))
                {
                    return(rec.GetValue(propertyChain));
                }
                var    props   = propertyChain.SplitNames('.');
                var    currRec = rec;
                object result  = null;
                foreach (var prop in props)
                {
                    result = currRec.GetValue(prop);
                    if (result is EntityBase)
                    {
                        currRec = EntityHelper.GetRecord(result);
                    }
                    else
                    {
                        return(result); // stop as soon as we reach non-entity
                    }
                }
                return(result);
            } //using
        }     //method
Beispiel #2
0
        /// <summary>Sets a property value of an entity. </summary>
        /// <param name="entity">The entity instance.</param>
        /// <param name="propertyName">The property name.</param>
        /// <param name="value">The property value.</param>
        public static void SetProperty(object entity, string propertyName, object value)
        {
            var rec    = EntityHelper.GetRecord(entity);
            var member = rec.EntityInfo.GetMember(propertyName, throwIfNotFound: true);

            rec.SetValue(member, value);
        }
Beispiel #3
0
        public static void RefreshEntity(object entity)
        {
            var rec = EntityHelper.GetRecord(entity);

            Util.Check(rec != null, "Object is not an entity ({0}) - failed to retrieve entity record.", entity);
            rec.Reload();
        }
Beispiel #4
0
        /// <summary>Returns a value of the entity property.</summary>
        /// <param name="entity">The entity instance.</param>
        /// <param name="propertyName">The property name.</param>
        /// <returns>The value of the property.</returns>
        public static object GetProperty(object entity, string propertyName)
        {
            var rec   = EntityHelper.GetRecord(entity);
            var value = rec[propertyName];

            return(value);
        }
Beispiel #5
0
        /// <summary>Returns a value of the entity property.</summary>
        /// <param name="entity">The entity instance.</param>
        /// <param name="propertyName">The property name.</param>
        /// <returns>The value of the property.</returns>
        public static object GetProperty(object entity, string propertyName)
        {
            var rec    = EntityHelper.GetRecord(entity);
            var member = rec.EntityInfo.GetMember(propertyName, throwIfNotFound: true);
            var value  = rec.GetValue(member);

            return(value);
        }
Beispiel #6
0
        public static void RefreshEntity(object entity)
        {
            var rec = EntityHelper.GetRecord(entity);

            Util.Check(rec != null, "Object is not an entity ({0}) - failed to retrieve entity record.", entity);
            rec.Reload(disableCache: true);
            rec.ClearTransientValues();
        }
        public static ClientFault ValidateEntity(this OperationContext context, object entity, bool condition, string faultCode,
                                                 string propertyName, object invalidValue, string message, params object[] args)
        {
            if (condition)
            {
                return(null);
            }
            var rec   = EntityHelper.GetRecord(entity);
            var fault = rec.AddValidationError(faultCode, message, args, propertyName, invalidValue);

            return(fault);
        }
        private void MarkTargetAsModified(EntityRecord record)
        {
            var rec    = record;
            var target = rec.GetValue(HostMember);

            if (target == null)
            {
                return;
            }
            var targetRec = EntityHelper.GetRecord(target);

            if (targetRec.Status == EntityStatus.Loaded)
            {
                targetRec.Status = EntityStatus.Modified;
            }
        }
Beispiel #9
0
        public static ClientFault ValidateEntity(this OperationContext context, object entity, bool condition, string faultCode,
                                                 string propertyName, object invalidValue, string message, params object[] args)
        {
            if (condition)
            {
                return(null);
            }
            var rec     = EntityHelper.GetRecord(entity);
            var recPath = rec.EntityInfo.EntityType.Name + "/" + rec.PrimaryKey.ToString();
            var fault   = new ClientFault()
            {
                Code = faultCode, Tag = propertyName, Message = Util.SafeFormat(message, args), Path = recPath
            };

            if (invalidValue != null)
            {
                fault.Parameters["InvalidValue"] = invalidValue.ToString();
            }
            rec.AddValidationFault(fault);
            return(fault);
        }
Beispiel #10
0
        // TODO: reimplement with offset stored in Claims

        /*
         * /// <summary>Returns user local time. The timezone offset value is in the user Principal and must be assigned before calling this method.</summary>
         * /// <param name="context">Operation context, must have user sesssion established.</param>
         * /// <param name="utcDateTime">Optional, UTC date/time to convert. If missing, function converts current UTC time.</param>
         * /// <returns>User local time.</returns>
         * public static DateTime GetUserLocalTime(this OperationContext context, DateTime? utcDateTime = null) {
         * Util.Check(context.UserSession != null, "No user session established, cannot infer user local time.");
         * var utc = utcDateTime == null ? context.App.TimeService.UtcNow : utcDateTime.Value;
         * var shifted = utc.AddMinutes(context.UserSession.TimeZoneOffsetMinutes);
         * var local = new DateTime(shifted.Year, shifted.Month, shifted.Day, shifted.Hour, shifted.Minute, shifted.Second, DateTimeKind.Unspecified);
         * return local;
         * }
         */

        public static TEntity NewCopyOf <TEntity>(this IEntitySession session, TEntity entity, bool copyPK = false) where TEntity : class
        {
            var copy       = session.NewEntity <TEntity>();
            var entityInfo = EntityHelper.GetRecord(entity).EntityInfo;

            foreach (var member in entityInfo.Members)
            {
                switch (member.Kind)
                {
                case EntityMemberKind.Column:
                    if (member.Flags.IsSet(EntityMemberFlags.PrimaryKey) && !copyPK)
                    {
                        continue;
                    }
                    var value = EntityHelper.GetProperty(entity, member.MemberName);
                    EntityHelper.SetProperty(copy, member.MemberName, value);
                    break;
                    // do not copy entity references or lists
                }
            }
            return(copy);
        }
Beispiel #11
0
        /// <summary>Retrieves a list of child entities for a given parent entity.</summary>
        /// <typeparam name="TParentEntity">Parent entity type, the type of a reference property of the child entity. </typeparam>
        /// <typeparam name="TEntity">Child entity type.</typeparam>
        /// <param name="session">Entity session.</param>
        /// <param name="parent">Parent entity.</param>
        /// <param name="parentRefProperty">Optional. The name of the property of child entity that references the parent entity. Use it when
        /// there is more than one such property.</param>
        /// <returns>A list of child entities that reference the parent entity.</returns>
        public static IList <TEntity> GetChildEntities <TParentEntity, TEntity>(this IEntitySession session,
                                                                                TParentEntity parent, string parentRefProperty = null)
            where TEntity : class
        {
            var entSession = (EntitySession)session;

            Util.Check(parent != null, "GetChildEntities<{0},{1}>: parent parameter may not be null.", typeof(TParentEntity), typeof(TEntity));
            var parentRec = EntityHelper.GetRecord(parent);

            Util.Check(parentRec != null, "GetChildEntities<{0},{1}>: parent parameter ({2}) is not an entity.", typeof(TParentEntity), typeof(TEntity), parent);
            var parentEntity = parentRec.EntityInfo;
            var childEntity  = entSession.GetEntityInfo(typeof(TEntity));

            Util.Check(childEntity != null, "Entity {0} not registered with the entity model.", typeof(TEntity));
            EntityMemberInfo member;

            if (string.IsNullOrEmpty(parentRefProperty))
            {
                var members = childEntity.Members.FindAll(m => m.DataType == typeof(TParentEntity));
                Util.Check(members.Count > 0, "Failed to select child entities. Reference to entity {0} not found on entity {1}.", typeof(TParentEntity), typeof(TEntity));
                Util.Check(members.Count < 2, "Failed to select child entities. More than one reference to entity {0} found on entity {1}. Explicitly specify property to use.",
                           typeof(TParentEntity), typeof(TEntity));
                member = members[0];
            }
            else
            {
                member = childEntity.GetMember(parentRefProperty);
                Util.Check(member != null, "Failed to select child entities. Reference to entity {0} not found on entity {1}.", typeof(TParentEntity), typeof(TEntity));
                var refOk = member.Kind == MemberKind.EntityRef && member.ReferenceInfo.ToKey.Entity == parentEntity;
                Util.Check(refOk, "Failed to select child entities. Property {0} on entity {1} does not reference entity {2}.",
                           parentRefProperty, typeof(TEntity), typeof(TParentEntity));
            }
            //actually retrieve entities
            var records = entSession.GetChildRecords(parentRec, member);

            return(entSession.ToEntities <TEntity>(records));
        }
Beispiel #12
0
        /// <summary>Associates a custom tag (free-form object) with an entity. </summary>
        /// <param name="entity">The entity.</param>
        /// <param name="tag">A tag to attach.</param>
        /// <remarks>Custom tags are free-form objects that can be attached to entities. Application
        /// is free to use the tags any way it needs. They are not used by the framework, and are
        /// reserved to use by client application code.</remarks>
        public static void SetCustomTag(object entity, object tag)
        {
            var rec = EntityHelper.GetRecord(entity);

            rec.CustomTag = tag;
        }
Beispiel #13
0
        /// <summary>Returns custom tag (free-form object) associated with the entity. </summary>
        /// <param name="entity">The entity.</param>
        /// <returns>Tag object.</returns>
        /// <remarks>Custom tags are free-form objects that can be attached to entities. Application
        /// is free to use the tags any way it needs. They are not used by the framework, and are
        /// reserved to use by client application code.</remarks>
        public static object GetCustomTag(object entity)
        {
            var rec = EntityHelper.GetRecord(entity);

            return(rec.CustomTag);
        }
Beispiel #14
0
        /// <summary>Sets a property value of an entity. </summary>
        /// <param name="entity">The entity instance.</param>
        /// <param name="propertyName">The property name.</param>
        /// <param name="value">The property value.</param>
        public static void SetProperty(object entity, string propertyName, object value)
        {
            var rec = EntityHelper.GetRecord(entity);

            rec[propertyName] = value;
        }