コード例 #1
0
        /// inheritedDoc
        public async Task <DataTablesDisplayPropertyColumn> GetDisplayPropertyColumnAsync(Type rowType, string propName, string name = null)
        {
            var prop = propName == null
                ? null
                : ReflectionHelper.GetProperty(rowType, propName);

            var displayAttribute = prop != null
                ? prop.GetAttribute <DisplayAttribute>()
                : null;

            var caption = displayAttribute != null && !string.IsNullOrWhiteSpace(displayAttribute.Name)
                ? displayAttribute.Name
                : propName.ToFriendlyName();

            var dataTypeInfo = prop != null
                ? _metadataProvider.GetDataType(prop)
                : new DataTypeInfo(null);

            var column = new DataTablesDisplayPropertyColumn()
            {
                Name             = (propName ?? "").Replace('.', '_'),
                PropertyName     = propName,
                Caption          = caption,
                Description      = prop?.GetDescription(),
                StandardDataType = dataTypeInfo.DataType,
                DataFormat       = dataTypeInfo.DataFormat,

                #region backward compatibility, to be removed
                GeneralDataType = prop != null
                    ? EntityConfigurationLoaderByReflection.GetGeneralDataType(prop)
                    : (GeneralDataType?)null,
                CustomDataType = prop?.GetAttribute <DataTypeAttribute>()?.CustomDataType,
                #endregion
            };
            var entityConfig = prop?.DeclaringType.GetEntityConfiguration();
            var propConfig   = prop != null ? entityConfig?.Properties[prop.Name] : null;

            if (propConfig != null)
            {
                column.ReferenceListName      = propConfig.ReferenceListName;
                column.ReferenceListNamespace = propConfig.ReferenceListNamespace;
                if (propConfig.EntityReferenceType != null)
                {
                    column.EntityReferenceTypeShortAlias = propConfig.EntityReferenceType.GetEntityConfiguration()?.SafeTypeShortAlias;
                    column.AllowInherited = propConfig.PropertyInfo.HasAttribute <AllowInheritedAttribute>();
                }
            }

            if (prop == null)
            {
                var modelConfig = await _modelConfigurationProvider.GetModelConfigurationOrNullAsync(rowType.Namespace, rowType.Name);

                var propertyConfig = modelConfig.Properties.FirstOrDefault(p => p.Name == propName);
                if (propertyConfig != null)
                {
                    column.IsDynamic        = true;
                    column.StandardDataType = propertyConfig.DataType;
                    column.DataFormat       = propertyConfig.DataFormat;
                    column.Description      = propertyConfig.Description;
                    column.IsFilterable     = false;
                    column.IsSortable       = false;
                }
            }

            // Set FilterCaption and FilterPropertyName
            column.FilterCaption ??= column.Caption;
            column.FilterPropertyName ??= column.PropertyName;

            if (column.PropertyName == null)
            {
                column.PropertyName = column.FilterPropertyName;
                column.Name         = (column.PropertyName ?? "").Replace('.', '_');
            }
            column.Caption ??= column.FilterCaption;

            // Check is the property mapped to the DB. If it's not mapped - make the column non sortable and non filterable
            if (column.IsSortable && rowType.IsEntityType() && propName != null && propName != "Id")
            {
                var chain = propName.Split('.').ToList();

                var container = rowType;
                foreach (var chainPropName in chain)
                {
                    if (!container.IsEntityType())
                    {
                        break;
                    }

                    var containerConfig = container.GetEntityConfiguration();
                    var propertyConfig  = containerConfig.Properties.ContainsKey(chainPropName)
                        ? containerConfig.Properties[chainPropName]
                        : null;

                    if (propertyConfig != null && !propertyConfig.IsMapped)
                    {
                        column.IsFilterable = false;
                        column.IsSortable   = false;
                        break;
                    }

                    container = propertyConfig?.PropertyInfo.PropertyType;
                    if (container == null)
                    {
                        break;
                    }
                }
            }

            return(column);
        }
コード例 #2
0
 public static GeneralDataType GetGeneralDataType(Type containerType, string propertyName)
 {
     return(typeof(IEntity).IsAssignableFrom(containerType)
             ? EntityExtensions.GetGeneralPropertyType(containerType, propertyName)
             : EntityConfigurationLoaderByReflection.GetGeneralDataType(containerType.GetProperty(propertyName)));
 }