예제 #1
0
        private static AuditProperty CreateAuditProperty(MetaDataMember dataMember, ModifiedMemberInfo modifiedMemberInfo, object entity, AuditEntity auditEntity)
        {
            var auditProperty = new AuditProperty();

            try
            {
                auditProperty.MetaDataMember = dataMember;
                auditProperty.Name           = dataMember.Member.Name;

                Type underlyingType = GetUnderlyingType(dataMember.Type);
                auditProperty.Type = underlyingType.FullName;

                if (auditProperty.Type == _binaryType || dataMember.IsDeferred)
                {
                    return(auditProperty);
                }

                if (auditEntity.Action == AuditAction.Update && modifiedMemberInfo.Member != null)
                {
                    auditProperty.Current  = GetValue(modifiedMemberInfo.Member, underlyingType, modifiedMemberInfo.CurrentValue, entity);
                    auditProperty.Original = GetValue(modifiedMemberInfo.Member, underlyingType, modifiedMemberInfo.OriginalValue, entity);
                    return(auditProperty);
                }

                var value = GetValue(dataMember.Member, underlyingType, dataMember.MemberAccessor.GetBoxedValue(entity), entity);
                if (value == null)
                {
                    return(null); // ignore null properties?
                }
                if (auditEntity.Action == AuditAction.Delete)
                {
                    auditProperty.Original = value;
                }
                else
                {
                    auditProperty.Current = value;
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.Message);
                if (auditEntity.Action == AuditAction.Delete)
                {
                    auditProperty.Original = "{error}";
                }
                else
                {
                    auditProperty.Current = "{error}";
                }
            }

            return(auditProperty);
        }
예제 #2
0
        private static void AddAssociationProperties(MetaType metaType, ITable table, object entity, AuditEntity auditEntity)
        {
            foreach (var association in metaType.Associations)
            {
                if (association.IsMany || HasNotAuditedAttribute(association.ThisMember.Member))
                {
                    continue;
                }

                // keys can contain multiple columns.
                // if none of them are found in the change list, skip.
                bool foundMember = false;

                foreach (var keyMember in association.ThisKey)
                {
                    if (!auditEntity.Properties.Contains(keyMember.Name))
                    {
                        continue;
                    }

                    var property = auditEntity.Properties[keyMember.Name];
                    property.IsForeignKey = association.IsForeignKey;
                    foundMember           = true;
                }

                if (!foundMember)
                {
                    continue;
                }

                var auditProperty = new AuditProperty();

                try
                {
                    var thisMember = association.ThisMember;
                    auditProperty.Name          = thisMember.Name;
                    auditProperty.Type          = thisMember.Type.FullName;
                    auditProperty.IsAssociation = true;
                    auditProperty.ForeignKey    = string.Join(",", association.ThisKey.Select(k => k.Name).ToArray());

                    var displayMember = GetDisplayMember(association.OtherType);

                    //this will get the fkey entity with out causing a load
                    object currentChildEntity = thisMember.DeferredValueAccessor.GetBoxedValue(entity);
                    object currentValue       = GetAssociationValue(association, displayMember, currentChildEntity, table, entity);

                    //if there is nothing set for the fkey on insert and delete, skip
                    if (auditEntity.Action != AuditAction.Update && currentValue == null)
                    {
                        continue;
                    }

                    if (auditEntity.Action == AuditAction.Delete)
                    {
                        auditProperty.Original = currentValue;
                    }
                    else
                    {
                        auditProperty.Current = currentValue ?? _nullText;
                    }

                    if (auditEntity.Action == AuditAction.Update && table != null)
                    {
                        // get original value for updates
                        object original = table.GetOriginalEntityState(entity);
                        if (original != null)
                        {
                            //this will get the fkey entity with out causing a load
                            object originalChildEntity = thisMember.DeferredValueAccessor.GetBoxedValue(original);
                            auditProperty.Original = GetAssociationValue(association, displayMember, originalChildEntity, table, original) ?? _nullText;
                        }
                    }
                }
                catch (Exception ex)
                {
                    Trace.TraceError(ex.Message);
                    if (auditEntity.Action == AuditAction.Delete)
                    {
                        auditProperty.Original = "{error}";
                    }
                    else
                    {
                        auditProperty.Current = "{error}";
                    }
                }

                auditEntity.Properties.Add(auditProperty);
            } // foreach
        }