/// <summary>
        /// config entity
        /// </summary>
        /// <param name="type">entity type</param>
        public static void ConfigEntity(Type type)
        {
            if (type == null)
            {
                return;
            }
            var typeGuid = type.GUID;

            if (AlreadConfigEntitys.Contains(typeGuid))
            {
                return;
            }
            AlreadConfigEntitys.Add(typeGuid);
            var entityAttribute = (type.GetCustomAttributes(typeof(EntityAttribute), false)?.FirstOrDefault()) as EntityAttribute;

            if (entityAttribute == null)
            {
                return;
            }
            var    propertys  = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            string objectName = entityAttribute.ObjectName.IsNullOrEmpty() ? type.Name : entityAttribute.ObjectName;

            if (!EntityConfigs.TryGetValue(typeGuid, out EntityConfig entityConfig))
            {
                entityConfig = new EntityConfig();
            }
            //table name
            if (entityConfig.TableName.IsNullOrEmpty())
            {
                entityConfig.TableName = objectName;
            }
            //fields
            List <EntityField> editFields       = new List <EntityField>();
            List <EntityField> queryFields      = new List <EntityField>();
            List <EntityField> allFields        = new List <EntityField>();
            List <EntityField> primaryKeys      = new List <EntityField>();
            EntityField        versionField     = null;
            EntityField        refreshDateField = null;

            foreach (var property in propertys)
            {
                var    name                 = property.Name;
                var    propertyName         = name;
                var    entityFieldAttribute = (property.GetCustomAttributes(typeof(EntityFieldAttribute), false)?.FirstOrDefault()) as EntityFieldAttribute;
                bool   disableEdit          = false;
                bool   isVersion            = false;
                bool   refreshDate          = false;
                bool   disableQuery         = false;
                bool   primaryKey           = false;
                string queryFormat          = string.Empty;
                if (entityFieldAttribute != null)
                {
                    if (!entityFieldAttribute.Name.IsNullOrEmpty())
                    {
                        name = entityFieldAttribute.Name;
                    }
                    disableEdit  = entityFieldAttribute.DisableEdit;
                    isVersion    = entityFieldAttribute.IsVersion;
                    refreshDate  = entityFieldAttribute.RefreshDate;
                    disableQuery = entityFieldAttribute.DisableQuery;
                    queryFormat  = entityFieldAttribute.QueryFormat;
                    primaryKey   = entityFieldAttribute.PrimaryKey;
                }
                var propertyField = new EntityField()
                {
                    FieldName    = name,
                    PropertyName = propertyName,
                    QueryFormat  = queryFormat
                };
                allFields.Add(propertyField);
                if (primaryKey)
                {
                    primaryKeys.Add(propertyField);
                }
                if (!disableQuery)
                {
                    queryFields.Add(propertyField);
                }
                if (!disableEdit)
                {
                    editFields.Add(propertyField);
                }
                if (isVersion)
                {
                    versionField = propertyField;
                }
                if (refreshDate)
                {
                    refreshDateField = propertyField;
                }

                //relation config
                var relationAttributes = property.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)
                {
                    EntityRelationAttribute relationAttr = attrObj as EntityRelationAttribute;
                    if (relationAttr == null || relationAttr.RelationType == null || string.IsNullOrWhiteSpace(relationAttr.RelationField))
                    {
                        continue;
                    }
                    var relationTypeId = relationAttr.RelationType.GUID;
                    entityConfig.RelationFields.TryGetValue(relationTypeId, out var values);
                    values = values ?? new Dictionary <string, string>();
                    if (values.ContainsKey(propertyName))
                    {
                        continue;
                    }
                    values.Add(propertyName, relationAttr.RelationField);
                    if (entityConfig.RelationFields.ContainsKey(relationTypeId))
                    {
                        entityConfig.RelationFields[relationTypeId] = values;
                    }
                    else
                    {
                        entityConfig.RelationFields.Add(relationTypeId, values);
                    }
                }
            }
            entityConfig.PrimaryKeys = primaryKeys;
            if (entityConfig.AllFields.IsNullOrEmpty())
            {
                entityConfig.AllFields = allFields;
            }
            if (entityConfig.EditFields.IsNullOrEmpty())
            {
                entityConfig.EditFields = editFields;
            }
            if (entityConfig.QueryFields.IsNullOrEmpty())
            {
                entityConfig.QueryFields = queryFields;
            }
            if (entityConfig.VersionField == null)
            {
                entityConfig.VersionField = versionField;
            }
            if (entityConfig.RefreshDateField == null)
            {
                entityConfig.RefreshDateField = refreshDateField;
            }
            ConfigEntity(type.GUID, entityConfig);
        }
示例#2
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))
            {
                return;
            }
            var entityAttribute = (entityType.GetCustomAttributes(typeof(EntityAttribute), false)?.FirstOrDefault()) as EntityAttribute;

            if (entityAttribute == null)
            {
                return;
            }
            var    propertys  = entityType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            string objectName = string.IsNullOrWhiteSpace(entityAttribute.ObjectName) ? entityType.Name : entityAttribute.ObjectName;

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

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

            List <EntityField> queryFields      = new List <EntityField>();
            EntityField        versionField     = null;
            EntityField        refreshDateField = null;

            foreach (var property in propertys)
            {
                var  name                          = property.Name;
                var  propertyName                  = name;
                var  entityFieldAttribute          = (property.GetCustomAttributes(typeof(EntityFieldAttribute), false)?.FirstOrDefault()) as EntityFieldAttribute;
                bool disableEdit                   = false;
                bool isVersion                     = false;
                bool refreshDate                   = false;
                bool disableQuery                  = false;
                bool primaryKey                    = false;
                EntityFieldCacheOption cacheOption = EntityFieldCacheOption.None;
                string queryFormat                 = string.Empty;
                if (entityFieldAttribute != null)
                {
                    if (!string.IsNullOrWhiteSpace(entityFieldAttribute.Name))
                    {
                        name = entityFieldAttribute.Name;
                    }
                    disableEdit  = entityFieldAttribute.DisableEdit;
                    isVersion    = entityFieldAttribute.IsVersion;
                    refreshDate  = entityFieldAttribute.RefreshDate;
                    disableQuery = entityFieldAttribute.DisableQuery;
                    queryFormat  = entityFieldAttribute.QueryFormat;
                    primaryKey   = entityFieldAttribute.PrimaryKey;
                    cacheOption  = entityFieldAttribute.CacheOption;
                }
                var propertyField = new EntityField()
                {
                    FieldName      = name,
                    PropertyName   = propertyName,
                    QueryFormat    = queryFormat,
                    CacheOption    = cacheOption,
                    IsDisableEdit  = disableEdit,
                    IsDisableQuery = disableQuery,
                    IsPrimaryKey   = primaryKey,
                    IsRefreshDate  = refreshDate,
                    IsVersion      = isVersion,
                    DataType       = property.PropertyType
                };
                allFields.Add(propertyField);
                if (primaryKey)
                {
                    primaryKeys.Add(propertyField);
                }
                else
                {
                    if ((cacheOption & EntityFieldCacheOption.CacheKey) != 0)
                    {
                        cacheKeys.Add(propertyField);
                    }
                    if ((cacheOption & EntityFieldCacheOption.CacheKeyPrefix) != 0)
                    {
                        cachePrefixKeys.Add(propertyField);
                    }
                    if ((cacheOption & EntityFieldCacheOption.Ignore) != 0)
                    {
                        cacheIgnoreKeys.Add(propertyField);
                    }
                }
                if (isVersion)
                {
                    versionField = propertyField;
                }
                if (refreshDate)
                {
                    refreshDateField = propertyField;
                }
                if (!disableQuery)
                {
                    queryFields.Add(propertyField);
                }

                //relation config
                var relationAttributes = property.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)
                {
                    EntityRelationAttribute relationAttr = attrObj as EntityRelationAttribute;
                    if (relationAttr == null || relationAttr.RelationType == null || string.IsNullOrWhiteSpace(relationAttr.RelationField))
                    {
                        continue;
                    }
                    var relationTypeId = relationAttr.RelationType.GUID;
                    entityConfig.RelationFields.TryGetValue(relationTypeId, out var values);
                    values = values ?? new Dictionary <string, string>();
                    if (values.ContainsKey(propertyName))
                    {
                        continue;
                    }
                    values.Add(propertyName, relationAttr.RelationField);
                    if (entityConfig.RelationFields.ContainsKey(relationTypeId))
                    {
                        entityConfig.RelationFields[relationTypeId] = values;
                    }
                    else
                    {
                        entityConfig.RelationFields.Add(relationTypeId, values);
                    }
                }
            }
            entityConfig.PrimaryKeys = primaryKeys;
            if (entityConfig.AllFields.IsNullOrEmpty())
            {
                entityConfig.AllFields = allFields;
            }
            if (entityConfig.VersionField == null)
            {
                entityConfig.VersionField = versionField;
            }
            if (entityConfig.RefreshDateField == null)
            {
                entityConfig.RefreshDateField = refreshDateField;
            }
            if (entityConfig.CacheKeys.IsNullOrEmpty())
            {
                entityConfig.CacheKeys = cacheKeys;
            }
            if (entityConfig.CachePrefixKeys.IsNullOrEmpty())
            {
                entityConfig.CachePrefixKeys = cachePrefixKeys;
            }
            if (entityConfig.CacheIgnoreKeys.IsNullOrEmpty())
            {
                entityConfig.CacheIgnoreKeys = cacheIgnoreKeys;
            }
            if (entityConfig.QueryFields.IsNullOrEmpty())
            {
                entityConfig.QueryFields = queryFields;
            }
            entityConfig.PredicateType = typeof(Func <,>).MakeGenericType(entityType, booleanType);
            ConfigureEntity(entityType.GUID, entityConfig);
        }