Exemple #1
0
        private static Dictionary <string, PropertyData> WriteProperties(EntityChange entityChange)
        {
            var propertiesData = new Dictionary <string, PropertyData>();

            var entityClass = EntityRefUtils.GetClass(entityChange.EntityRef);

            foreach (var prop in entityChange.PropertyChanges)
            {
                var pi = entityClass.GetProperty(prop.PropertyName);

                // special handling of extended properties collections
                // note that we need to check pi != null because it may represent a field-access "property"
                // which has no corresponding .NET property
                if (pi != null && AttributeUtils.HasAttribute <ExtendedPropertiesCollectionAttribute>(pi))
                {
                    var extendedProps = WriteExtendedProperties(prop, entityChange.ChangeType);
                    foreach (var extendedProp in extendedProps)
                    {
                        propertiesData.Add(extendedProp.Key, extendedProp.Value);
                    }
                }
                else
                {
                    var propertyData = WriteProperty(prop.OldValue, prop.NewValue, entityChange.ChangeType);
                    propertiesData.Add(prop.PropertyName, propertyData);
                }
            }
            return(propertiesData);
        }
Exemple #2
0
        /// <summary>
        /// Obtains a data-contract instance representing the specified change-set, suitable for
        /// transmission and/or serialization to an audit log.
        /// </summary>
        /// <param name="operationName"></param>
        /// <param name="changeSet"></param>
        /// <returns></returns>
        public static ChangeSetData WriteChangeSet(string operationName, IEnumerable <EntityChange> changeSet)
        {
            var actionData = from entityChange in changeSet
                             select new ActionData
            {
                Type              = entityChange.ChangeType.ToString(),
                Class             = EntityRefUtils.GetClass(entityChange.EntityRef).FullName,
                OID               = EntityRefUtils.GetOID(entityChange.EntityRef).ToString(),
                Version           = EntityRefUtils.GetVersion(entityChange.EntityRef),
                ChangedProperties = WriteProperties(entityChange)
            };

            return(new ChangeSetData {
                Operation = StringUtilities.EmptyIfNull(operationName), Actions = actionData.ToList()
            });
        }
        /// <summary>
        /// Loads the specified entity into this context.
        /// </summary>
        public Entity Load(EntityRef entityRef, EntityLoadFlags flags)
        {
            try
            {
                // use a proxy if EntityLoadFlags.Proxy is specified and EntityLoadFlags.CheckVersion is not specified (CheckVersion overrides Proxy)
                var useProxy = (flags & EntityLoadFlags.CheckVersion) == 0 && (flags & EntityLoadFlags.Proxy) == EntityLoadFlags.Proxy;
                var entity   = (Entity)Load(EntityRefUtils.GetClass(entityRef), EntityRefUtils.GetOID(entityRef), useProxy);

                // check version if necessary
                if ((flags & EntityLoadFlags.CheckVersion) == EntityLoadFlags.CheckVersion && !EntityRefUtils.GetVersion(entityRef).Equals(entity.Version))
                {
                    throw new EntityVersionException(EntityRefUtils.GetOID(entityRef), null);
                }

                return(entity);
            }
            catch (ObjectNotFoundException hibernateException)
            {
                // note that we will only get here if useProxy == false in the above block
                // if the entity is proxied, verification of its existence is deferred until the proxy is realized
                throw new EntityNotFoundException(hibernateException);
            }
        }