Ejemplo n.º 1
0
        /// <summary>
        /// Gets a property's value
        /// </summary>
        public static object GetValue(object obj, string propertyName)
        {
            if (obj is IDwarf)
            {
                var type = DwarfHelper.DeProxyfy(obj);

                if (!Cfg.PropertyExpressions[type].ContainsKey(propertyName))
                {
                    throw new ApplicationException(type.Name + " doesn't have a property: " + propertyName);
                }

                return(Cfg.PropertyExpressions[type][propertyName].GetValue(obj));
            }
            else
            {
                var type = obj.GetType();
                var pi   = type.GetProperty(propertyName);

                if (pi == null)
                {
                    throw new ApplicationException(type.Name + " doesn't have a property: " + propertyName);
                }

                return(pi.GetValue(obj, null));
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Sets a property's value
        /// </summary>
        public static void SetValue(object obj, string propertyName, object value)
        {
            if (obj is IDwarf)
            {
                var type = DwarfHelper.DeProxyfy(obj);

                if (!Cfg.PropertyExpressions[type].ContainsKey(propertyName))
                {
                    throw new ApplicationException(type.Name + " doesn't have a property: " + propertyName);
                }

                var ep = Cfg.PropertyExpressions[type][propertyName];

                if (ep.CanWrite)
                {
                    ep.SetValue(obj, value);
                }
            }
            else
            {
                var type = obj.GetType();
                var pi   = type.GetProperty(propertyName);

                if (pi == null)
                {
                    throw new ApplicationException(type.Name + " doesn't have a property: " + propertyName);
                }

                if (pi.CanWrite)
                {
                    pi.SetValue(obj, value, null);
                }
            }
        }
Ejemplo n.º 3
0
        public static ExpressionProperty GetProperty(Type type, string propertyName)
        {
            DwarfHelper.DeProxyfy(ref type);

            return(Cfg.PropertyExpressions[type].ContainsKey(propertyName)
                       ? Cfg.PropertyExpressions[type][propertyName]
                       : null);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Returns true if the object contains faulty Foreign Key values
        /// </summary>
        private static IEnumerable <string> FaultyForeignKeys(IDwarf obj)
        {
            foreach (var pi in Cfg.FKProperties[DwarfHelper.DeProxyfy(obj)])
            {
                var att = DwarfPropertyAttribute.GetAttribute(pi.ContainedProperty);

                if (!att.IsNullable && (pi.GetValue(obj) == null || !((IDwarf)pi.GetValue(obj)).IsSaved))
                {
                    yield return(pi.Name);
                }
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Stores an AuditLog object for the ongoing transaction
        /// </summary>
        public static IAuditLog Logg <T>(IDwarf obj, AuditLogTypes auditLogType, params AuditLogEventTrace[] auditUpdateEvents)
        {
            var type = DwarfHelper.DeProxyfy(obj);

            if (type.Implements <IAuditLogless>() || type.Implements <IAuditLog>())
            {
                return(null);
            }

            var al = new AuditLog
            {
                ClassType    = type.Name,
                AuditLogType = auditLogType,
                UserName     = DwarfContext <T> .GetConfiguration().UserService.CurrentUser != null ? DwarfContext <T> .GetConfiguration().UserService.CurrentUser.UserName : string.Empty,
                TimeStamp    = DateTime.Now,
                ObjectValue  = obj.ToString(),
            };

            if (!type.Implements <ICompositeId>())
            {
                al.ObjectId = obj.Id.ToString();
            }
            else
            {
                foreach (var ep in DwarfHelper.GetPKProperties(type))
                {
                    al.ObjectId += string.Format("[{0}: {1}]", ep.Name, ep.GetValue(obj));
                }
            }

            if (auditLogType != AuditLogTypes.Created)
            {
                al.AuditDetails = "<?xml version=\"1.0\"?><Properties>";

                foreach (var auditUpdateEvent in auditUpdateEvents)
                {
                    al.AuditDetails += auditUpdateEvent.ToXml().ToString();
                }

                al.AuditDetails += "</Properties>";
            }

            DwarfContext <T> .GetDatabase().Insert <T, AuditLog>(al);

            return(al);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Helper method for the proxy classes's MSIL generated foreign key methods
        /// </summary>
        protected TY GetProperty <TY>(string propertyName, ref TY backingField, ref bool isAccessed) where TY : Dwarf <TY>, new()
        {
            if (isAccessed)
            {
                return(backingField);
            }

            var orgValue = GetOriginalValue(propertyName);

            if (orgValue != null)
            {
                backingField = (TY)Cfg.LoadExpressions[DwarfHelper.DeProxyfy(typeof(TY))]((Guid)orgValue);
            }

            isAccessed = true;

            return(backingField);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Dwarf comparison is done via the Id property
        /// </summary>
        public override bool Equals(object obj)
        {
            if (obj is IDwarf)
            {
                var type = DwarfHelper.DeProxyfy(GetType());

                if (type != DwarfHelper.DeProxyfy(obj))
                {
                    return(false);
                }

                if (!type.Implements <ICompositeId>())
                {
                    //Should both objects not be stored they'll both have null as Id, thus we check all the properties to find out if they might be the same object
                    //We also dubbelcheck all unique properties (if existant) to make sure they're NOT equal
                    if (!Id.HasValue && !((IDwarf)obj).Id.HasValue)
                    {
                        var propertiesMatch = DwarfHelper.GetDBProperties(type).Where(ep => ep.GetValue(this) != null && ep.GetValue(obj) != null).All(pi => pi.GetValue(this).Equals(pi.GetValue(obj)));

                        var uniqueProps = DwarfHelper.GetUniqueDBProperties <T>(type).ToList();

                        if (uniqueProps.Any())
                        {
                            var uniquePropertiesMatch = uniqueProps.All(ep => ep.GetValue(this) == null ? ep.GetValue(obj) == null : ep.GetValue(this).Equals(ep.GetValue(obj)));

                            return(uniquePropertiesMatch && propertiesMatch);
                        }

                        return(propertiesMatch);
                    }

                    return(Id == ((IDwarf)obj).Id);
                }

                return(Cfg.PKProperties[type].All(ep => ep.GetValue(this) == null ? ep.GetValue(obj) == null : ep.GetValue(this).Equals(ep.GetValue(obj))));
            }

            return(false);
        }
Ejemplo n.º 8
0
 /// <summary>
 /// Returns the database object for the supplied typeq
 /// </summary>
 internal static IDatabase GetDatabase(Type type)
 {
     return(Cfg.Databases[DwarfHelper.DeProxyfy(type).Assembly]);
 }
Ejemplo n.º 9
0
        internal static IEnumerable <ExpressionProperty> GetProperties(Type type)
        {
            DwarfHelper.DeProxyfy(ref type);

            return(Cfg.PropertyExpressions[type].Values);
        }
Ejemplo n.º 10
0
 /// <summary>
 /// Returns true if the supplied object contains a property by the specified name
 /// </summary>
 public static bool HasProperty(Type type, string propertyName)
 {
     return(Cfg.PropertyExpressions[DwarfHelper.DeProxyfy(type)].ContainsKey(propertyName));
 }
Ejemplo n.º 11
0
        /// <summary>
        /// Restores the object's properties to their original values
        /// </summary>
        public void Refresh()
        {
            //Fetch the original values from the database (bypass the cache...)
            var originalObject = DwarfContext <T> .GetDatabase().SelectReferencing <T>(new QueryBuilder().Select <T>().From <T>().Where(this, Cfg.PKProperties[DwarfHelper.DeProxyfy(this)]), false, true).FirstOrDefault();

            if (originalObject != null)
            {
                foreach (var ep in DwarfHelper.GetDBProperties(GetType()))
                {
                    SetOriginalValue(ep.Name, ep.GetValue(originalObject));
                }
            }

            //Else should we throw an exception since the object has been deleted from the DB?

            Reset();
        }