Beispiel #1
0
        public static IEnumerable <EntityField> GetFields(Type t)
        {
            return(fields.GetOrAdd(t, (t2) =>
            {
                List <EntityField> result = new List <EntityField>();

                foreach (Type iface in t.GetInterfaces())
                {
                    if (Reflection.Utility.HasAttribute <EntityFieldsInterfaceAttribute>(iface))
                    {
                        foreach (PropertyInfo ifaceProperty in iface.GetProperties())
                        {
                            EntityFieldAttribute efa = Reflection.Utility.GetAttribute <EntityFieldAttribute>(ifaceProperty);
                            if (efa != null)
                            {
                                PropertyInfo typeProperty = t.GetProperty(ifaceProperty.Name);
                                result.Add(new EntityField(efa, typeProperty));
                            }
                        }
                    }
                }

                foreach (PropertyInfo typeProperty in t.GetProperties())
                {
                    EntityFieldAttribute efa = Reflection.Utility.GetAttribute <EntityFieldAttribute>(typeProperty);
                    if (efa != null)
                    {
                        result.Add(new EntityField(efa, typeProperty));
                    }
                }

                return result;
            }));
        }
        public void TestIsRequiredOnCreateGetterThrowsInvalidOperationExceptionIfNull()
        {
            try
            {
                var unit = new EntityFieldAttribute()
                {
                    Name           = "unit test",
                    SerializedName = "unit test",
                    //IsRequiredOnCreate = false,
                    IsRequiredOnDelete = false,
                    IsAllowedOnUpdate  = false,
                    IsAllowedOnCreate  = false,
                    IsRequiredOnUpdate = false
                };
                var fail = unit.IsRequiredOnCreate;
                Assert.Fail("Expected InvalidOperationException not thrown.");
            }
            catch (InvalidOperationException)
            {
                //expected
            }

            var unitPass = new EntityFieldAttribute()
            {
                Name               = "unit test",
                SerializedName     = "unit test",
                IsRequiredOnCreate = false,
                IsRequiredOnDelete = false,
                IsAllowedOnUpdate  = false,
                IsAllowedOnCreate  = false,
                IsRequiredOnUpdate = false
            };
            var succeed = unitPass.IsRequiredOnCreate;
        }
        public EntityField(EntityFieldAttribute attribute, PropertyInfo property)
        {
            this.Property = property;

            if (!string.IsNullOrEmpty(attribute.Name))
            {
                this.Name = attribute.Name;
            }
            else
            {
                this.Name = property.Name;
            }

            isEntityReference = new Lazy <bool>(() =>
            {
                if (Property.PropertyType.GetInterfaces().Contains(typeof(IEntityReference)))
                {
                    return(true);
                }
                if (IsList && PropertyFirstGenericTypeArgument.GetInterfaces().Contains(typeof(IEntityReference)))
                {
                    return(true);
                }
                return(false);
            });

            isEntity = new Lazy <bool>(() =>
            {
                if (Property.PropertyType.GetInterfaces().Contains(typeof(IEntity)))
                {
                    return(true);
                }
                if (IsList && PropertyFirstGenericTypeArgument.GetInterfaces().Contains(typeof(IEntity)))
                {
                    return(true);
                }
                return(false);
            });
        }
Beispiel #4
0
        public void TestConstruction()
        {
            var unit = new EntityFieldAttribute(typeof(Lead), "ut", "ut", true, true, true, true, true);

            Assert.AreEqual(typeof(Lead), unit.BelongsTo);
            Assert.AreEqual("ut", unit.Name);
            Assert.AreEqual("ut", unit.SerializedName);
            Assert.IsTrue(unit.IsRequiredOnCreate);
            Assert.IsTrue(unit.IsRequiredOnUpdate);
            Assert.IsTrue(unit.IsRequiredOnDelete);
            Assert.IsTrue(unit.IsAllowedOnCreate);
            Assert.IsTrue(unit.IsAllowedOnUpdate);

            var unit2 = new EntityFieldAttribute(typeof(Lead), "ut2", "ut2", false, false, false, false, false);

            Assert.AreEqual(typeof(Lead), unit2.BelongsTo);
            Assert.AreEqual("ut2", unit2.Name);
            Assert.AreEqual("ut2", unit2.SerializedName);
            Assert.IsFalse(unit2.IsRequiredOnCreate);
            Assert.IsFalse(unit2.IsRequiredOnUpdate);
            Assert.IsFalse(unit2.IsRequiredOnDelete);
            Assert.IsFalse(unit2.IsAllowedOnCreate);
            Assert.IsFalse(unit2.IsAllowedOnUpdate);
        }
Beispiel #5
0
            /// <summary>
            /// Configure entity
            /// </summary>
            /// <param name="entityType">Entity type</param>
            internal static void ConfigureEntity(Type entityType)
            {
                if (entityType == null)
                {
                    return;
                }
                var typeGuid = entityType.GUID;

                if (EntityConfigurations.ContainsKey(typeGuid) || !((entityType.GetCustomAttributes(typeof(EntityAttribute), false)?.FirstOrDefault()) is EntityAttribute entityAttribute))
                {
                    return;
                }
                IEnumerable <MemberInfo> memberInfos = new List <MemberInfo>(0);

                memberInfos = memberInfos.Union(entityType.GetProperties(BindingFlags.Public | BindingFlags.Instance));
                memberInfos = memberInfos.Union(entityType.GetFields(BindingFlags.Public | BindingFlags.Instance));
                string objectName = string.IsNullOrWhiteSpace(entityAttribute.ObjectName) ? entityType.Name : entityAttribute.ObjectName;

                if (!EntityConfigurations.TryGetValue(typeGuid, out EntityConfiguration entityConfig))
                {
                    entityConfig = new EntityConfiguration();
                }
                entityConfig.Structure = entityAttribute.Structure;
                //table name
                if (string.IsNullOrWhiteSpace(entityConfig.TableName))
                {
                    entityConfig.TableName = objectName;
                }
                //fields
                List <EntityField> allFields   = new List <EntityField>();
                List <string>      primaryKeys = new List <string>();

                //cache keys
                List <string> cacheKeys       = new List <string>();
                List <string> cachePrefixKeys = new List <string>();
                List <string> cacheIgnoreKeys = new List <string>();

                List <string>      mustQueryFields   = new List <string>();
                List <EntityField> editFields        = new List <EntityField>();
                List <EntityField> queryEntityFields = new List <EntityField>();
                string             versionField      = null;
                string             refreshDateField  = null;

                foreach (var member in memberInfos)
                {
                    var fieldName    = member.Name;
                    var fieldRole    = FieldRole.None;
                    var propertyName = fieldName;
                    EntityFieldAttribute entityFieldAttribute = (member.GetCustomAttributes(typeof(EntityFieldAttribute), false)?.FirstOrDefault()) as EntityFieldAttribute;
                    fieldRole = entityFieldAttribute?.Role ?? FieldRole.None;
                    fieldName = entityFieldAttribute?.Name ?? fieldName;
                    Type memberType = null;
                    if (member is PropertyInfo propertyInfo)
                    {
                        memberType = propertyInfo.PropertyType;
                    }
                    if (member is FieldInfo fieldInfo)
                    {
                        memberType = fieldInfo.FieldType;
                    }
                    var propertyField = new EntityField()
                    {
                        FieldName      = fieldName,
                        PropertyName   = propertyName,
                        QueryFormat    = entityFieldAttribute?.QueryFormat ?? string.Empty,
                        CacheRole      = entityFieldAttribute?.CacheRole ?? FieldCacheRole.None,
                        IsDisableEdit  = entityFieldAttribute?.DisableEdit ?? false,
                        IsDisableQuery = entityFieldAttribute?.DisableQuery ?? false,
                        IsPrimaryKey   = (fieldRole & FieldRole.PrimaryKey) != 0,
                        IsRefreshDate  = (fieldRole & FieldRole.RefreshDate) != 0,
                        IsVersion      = (fieldRole & FieldRole.Version) != 0,
                        IsLevel        = (fieldRole & FieldRole.Level) != 0,
                        IsSort         = (fieldRole & FieldRole.Sort) != 0,
                        IsParent       = (fieldRole & FieldRole.Parent) != 0,
                        IsDisplayName  = (fieldRole & FieldRole.Display) != 0,
                        DataType       = memberType,
                        DbTypeName     = entityFieldAttribute?.DbTypeName,
                        MaxLength      = entityFieldAttribute?.MaxLength ?? 0,
                        IsFixedLength  = entityFieldAttribute?.IsFixedLength ?? false,
                        IsRequired     = entityFieldAttribute?.IsRequired ?? false,
                        Comment        = entityFieldAttribute?.Description ?? string.Empty
                    };

                    //value provider
                    var valueProvider = GetValueProvider(entityType, member);
                    if (valueProvider != null)
                    {
                        propertyField.ValueProvider = valueProvider;
                    }

                    allFields.Add(propertyField);

                    if (propertyField.IsPrimaryKey)
                    {
                        primaryKeys.Add(propertyName);
                    }
                    else
                    {
                        if ((propertyField.CacheRole & FieldCacheRole.CacheKey) != 0)
                        {
                            cacheKeys.Add(propertyName);
                        }
                        if ((propertyField.CacheRole & FieldCacheRole.CacheKeyPrefix) != 0)
                        {
                            cachePrefixKeys.Add(propertyName);
                        }
                        if ((propertyField.CacheRole & FieldCacheRole.Ignore) != 0)
                        {
                            cacheIgnoreKeys.Add(propertyName);
                        }
                    }
                    if (propertyField.IsVersion)
                    {
                        versionField = propertyName;
                    }
                    if (propertyField.IsRefreshDate)
                    {
                        refreshDateField = propertyName;
                    }

                    //relation config
                    var relationAttributes = member.GetCustomAttributes(typeof(EntityRelationAttribute), false);
                    if (relationAttributes.IsNullOrEmpty())
                    {
                        continue;
                    }
                    if (entityConfig.RelationFields.IsNullOrEmpty())
                    {
                        entityConfig.RelationFields = new Dictionary <Guid, Dictionary <string, string> >();
                    }
                    foreach (var attrObj in relationAttributes)
                    {
                        if (!(attrObj is EntityRelationAttribute relationAttr) || relationAttr.RelationType == null || string.IsNullOrWhiteSpace(relationAttr.RelationField))
                        {
                            continue;
                        }
                        var relationTypeId = relationAttr.RelationType.GUID;
                        entityConfig.RelationFields.TryGetValue(relationTypeId, out var values);
                        values ??= new Dictionary <string, string>();
                        if (values.ContainsKey(propertyName))
                        {
                            continue;
                        }
                        values.Add(propertyName, relationAttr.RelationField);
                        entityConfig.RelationFields[relationTypeId] = values;
                    }
                }
                allFields = allFields.OrderByDescending(c => c.IsPrimaryKey).ThenByDescending(c => cacheKeys.Contains(c.PropertyName)).ToList();
                var allFieldDict = new Dictionary <string, EntityField>(allFields.Count);

                foreach (var field in allFields)
                {
                    allFieldDict[field.PropertyName] = field;
                    var mustQueryField = field.IsPrimaryKey || field.IsVersion;
                    if (mustQueryField)
                    {
                        mustQueryFields.Add(field.PropertyName);
                    }
                    if (mustQueryField || !field.IsDisableQuery)
                    {
                        queryEntityFields.Add(field);
                    }
                    if (!field.IsDisableEdit)
                    {
                        editFields.Add(field);
                    }
                }
                entityConfig.Comment           = entityAttribute.Description ?? string.Empty;
                entityConfig.PrimaryKeys       = primaryKeys;
                entityConfig.AllFields         = allFieldDict;
                entityConfig.VersionField      = versionField;
                entityConfig.RefreshDateField  = refreshDateField;
                entityConfig.CacheKeys         = cacheKeys;
                entityConfig.CachePrefixKeys   = cachePrefixKeys;
                entityConfig.CacheIgnoreKeys   = cacheIgnoreKeys;
                entityConfig.QueryFields       = queryEntityFields.Select(c => c.PropertyName).ToList();
                entityConfig.QueryEntityFields = queryEntityFields;
                entityConfig.EditFields        = editFields;
                entityConfig.MustQueryFields   = mustQueryFields;
                entityConfig.PredicateType     = typeof(Func <,>).MakeGenericType(entityType, booleanType);
                entityConfig.EntityType        = entityType;
                EntityConfigurations[typeGuid] = entityConfig;
            }