Exemple #1
0
        static Dictionary <string, string> SerializeQuery(object item)
        {
            var result = new Dictionary <string, string>();

            void serialize(object container, string key, object value)
            {
                if (value is IEntity entity)
                {
                    if (TransientEntityAttribute.IsTransient(entity.GetType()))
                    {
                        foreach (var pp in GetSerializableProperties(entity.GetType()))
                        {
                            serialize(value, key + "." + pp.Name, pp.GetValue(entity));
                        }
                    }
                    else
                    {
                        result[key] = entity.GetId().ToStringOrEmpty();
                    }
                }
                else
                {
                    result[key] = value.ToStringOrEmpty();
                }
            }

            foreach (var p in item.GetType().GetProperties())
            {
                var key   = p.Name.ToLower().Replace("_", ".");
                var value = p.GetValue(item);
                serialize(item, key, value);
            }

            return(result);
        }
        static IEnumerable <(PropertyInfo MainInfo, PropertyInfo DatabaseProp)> FilterProperties(PropertyInfo[] infos)
        {
            var nonCalculated = infos.Except(p => CalculatedAttribute.IsCalculated(p) || p.GetSetMethod() == null);
            var nonOverriden  = nonCalculated.Except(p => p.GetGetMethod() != p.GetGetMethod().GetBaseDefinition());
            var nonTransient  = nonOverriden.Except(p => TransientEntityAttribute.IsTransient(p.PropertyType));

            var associations = nonTransient.Where(p => p.IsAssociation());
            var rest         = nonTransient.Except(associations);

            var ids = new List <PropertyInfo>();

            PropertyInfo getIdFor(PropertyInfo info)
            {
                var result = rest.FirstOrDefault(p => p.Name == info.Name.WithSuffix("Id"));

                ids.Add(result);
                return(result);
            }

            foreach (var prop in associations)
            {
                yield return(prop, getIdFor(prop));
            }

            foreach (var prop in rest.Except(ids))
            {
                yield return(prop, null);
            }
        }
        public static IEnumerable <Type> SearchForPossibleTypes(Type baseType, bool mustFind)
        {
            IEnumerable <Type> result;

            if (baseType == null || baseType == typeof(Entity))
            {
                result = GetDomainEntityTypes();
            }
            else if (baseType.IsInterface)
            {
                result =
                    Database.AssemblyProviderFactories.Except(f => f.Value.SupportsPolymorphism())
                    .Select(a => a.Key).Where(a => a.References(baseType.Assembly)).Concat(baseType.Assembly)
                    .SelectMany(a => a.GetExportedTypes())
                    .Where(t => t.Implements(baseType)).ToList();
            }
            else
            {
                result = baseType.Assembly.GetExportedTypes().Where(t => t.GetParentTypes().Contains(baseType)).Union(new[] { baseType });
            }

            result = result
                     // Not transient objects:
                     .Where(t => !TransientEntityAttribute.IsTransient(t))
                     // No abstract or interface:
                     .Where(t => !t.IsAbstract && !t.IsInterface)
                     // Unless the type is marked non-persistent:
                     .Where(t => PersistentAttribute.IsTypePersistent(t))
                     // Leaf nodes first (most concrete):
                     .OrderByDescending(t => t.GetParentTypes().Count());

            result = result.Except(new[] { typeof(IApplicationEvent) }).ToArray();

            if (result.None())
            {
                if (baseType != null && mustFind)
                {
                    throw new ArgumentException("No type in the current application domain can be the implementation of the type " +
                                                baseType.FullName + ".");
                }

                else if (mustFind)
                {
                    throw new ArgumentException("No type in the current application domain implements Entity.");
                }
            }


            return(result);
        }
        protected virtual bool IsRelevant(Type type)
        {
            if (MappedType != null && MappedType != type)
            {
                return(false);
            }
            if (MappedAssembly != type.Assembly)
            {
                return(false);
            }
            if (TransientEntityAttribute.IsTransient(type))
            {
                return(false);
            }

            return(true);
        }
Exemple #5
0
        static Type[] GetDrivedClasses(Type type)
        {
            var result = type.Assembly.GetTypes().Where(t => t.IsA(type) && t != type && !TransientEntityAttribute.IsTransient(t)).ToArray();

            Array.Sort(result, new TypeComparer(type));

            return(result);
        }