public override void Apply(PropertyConfiguration edmProperty, StructuralTypeConfiguration structuralTypeConfiguration, Attribute attribute, ODataConventionModelBuilder model) { if (edmProperty == null) { throw Error.ArgumentNull("edmProperty"); } if (!edmProperty.AddedExplicitly) { OrderByAttribute orderByAttribute = attribute as OrderByAttribute; ModelBoundQuerySettings querySettings = edmProperty.QueryConfiguration.GetModelBoundQuerySettingsOrDefault(); if (querySettings.OrderByConfigurations.Count == 0) { querySettings.CopyOrderByConfigurations(orderByAttribute.OrderByConfigurations); } else { foreach (var property in orderByAttribute.OrderByConfigurations.Keys) { querySettings.OrderByConfigurations[property] = orderByAttribute.OrderByConfigurations[property]; } } if (orderByAttribute.OrderByConfigurations.Count == 0) { querySettings.DefaultEnableOrderBy = orderByAttribute.DefaultEnableOrderBy; } } }
private IList <string> GetOrderByPropertiesFromType <T, TKey>() { IList <string> properties = new List <string>(); Type t = typeof(T); Type tkey = typeof(TKey); MetadataTypeAttribute[] metadataAttributes = (MetadataTypeAttribute[])t.GetCustomAttributes(typeof(MetadataTypeAttribute), true); foreach (var pi in t.GetProperties()) { if (pi.PropertyType == tkey && OrderByAttribute.IsDefined(pi, typeof(OrderByAttribute))) { properties.Add(pi.Name); } else { foreach (var attribute in metadataAttributes) { t = attribute.MetadataClassType; var otherpi = t.GetProperty(pi.Name); if (otherpi != null && pi.PropertyType == tkey && OrderByAttribute.IsDefined(otherpi, typeof(OrderByAttribute))) { properties.Add(pi.Name); break; } } } } return(properties); }
/// <summary> /// Set whether the $orderby can be applied on those properties of this structural type. /// </summary> /// <param name="edmTypeConfiguration">The structural type to configure.</param> /// <param name="model">The edm model that this type belongs to.</param> /// <param name="attribute">The <see cref="Attribute"/> found on this type.</param> public override void Apply(StructuralTypeConfiguration edmTypeConfiguration, ODataConventionModelBuilder model, Attribute attribute) { if (edmTypeConfiguration == null) { throw Error.ArgumentNull("edmTypeConfiguration"); } if (model == null) { throw Error.ArgumentNull("model"); } if (!edmTypeConfiguration.AddedExplicitly) { OrderByAttribute orderByAttribute = attribute as OrderByAttribute; /* * ModelBoundQuerySettings querySettings = * edmTypeConfiguration.QueryConfiguration.GetModelBoundQuerySettingsOrDefault(); * if (querySettings.OrderByConfigurations.Count == 0) * { * querySettings.CopyOrderByConfigurations( * orderByAttribute.OrderByConfigurations); * } * else * { * foreach (var property in orderByAttribute.OrderByConfigurations.Keys) * { * querySettings.OrderByConfigurations[property] = * orderByAttribute.OrderByConfigurations[property]; * } * } * * if (orderByAttribute.OrderByConfigurations.Count == 0) * { * querySettings.DefaultEnableOrderBy = * orderByAttribute.DefaultEnableOrderBy; * } */ } }
public string Build() { if (!_isSelectSet) { Select(); } OrderByAttribute attribute = null; var orderBy = TableType.GetProperties().FirstOrDefault(pi => (attribute = pi.GetAttribute <OrderByAttribute>()) != null); if (orderBy != null) { _stringBuilder.Append(" ORDER BY "); _stringBuilder.Append(orderBy.Name); if (attribute.Direction == ListSortDirection.Descending) { _stringBuilder.Append(" DESC"); } } return(_stringBuilder.ToString()); }
/// <summary> /// Custom ModelMedataProvider used to store the SearchFilterAttribute and OrderByAttribute /// in ModelMetadata AdditionValues /// ref: http://bradwilson.typepad.com/blog/2010/01/why-you-dont-need-modelmetadataattributes.html /// </summary> /// <param name="attributes"></param> /// <param name="containerType"></param> /// <param name="modelAccessor"></param> /// <param name="modelType"></param> /// <param name="propertyName"></param> /// <returns></returns> protected override ModelMetadata CreateMetadata(IEnumerable <Attribute> attributes, Type containerType, Func <object> modelAccessor, Type modelType, string propertyName) { ModelMetadata metadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName); SearchFilterAttribute searchFilterAttribute = attributes.OfType <SearchFilterAttribute>().FirstOrDefault(); if (searchFilterAttribute != null) { metadata.AdditionalValues.Add(Globals.SearchFilterAttributeKey, searchFilterAttribute); } OrderByAttribute orderByAttribute = attributes.OfType <OrderByAttribute>().FirstOrDefault(); if (orderByAttribute != null) { metadata.AdditionalValues.Add(Globals.OrderByAttributeKey, orderByAttribute); } return(metadata); }
/// <summary> /// Custom ModelMedataProvider used to store the SearchFilterAttribute and OrderByAttribute /// in ModelMetadata AdditionValues /// ref: http://bradwilson.typepad.com/blog/2010/01/why-you-dont-need-modelmetadataattributes.html /// </summary> /// <param name="attributes"></param> /// <param name="containerType"></param> /// <param name="modelAccessor"></param> /// <param name="modelType"></param> /// <param name="propertyName"></param> /// <returns></returns> protected override ModelMetadata CreateMetadata(IEnumerable <Attribute> attributes, Type containerType, Func <object> modelAccessor, Type modelType, string propertyName) { //Если у нас тип контейнера фильтров, то имя проперти надо сброить if ( attributes.Any( attr => attr is System.ComponentModel.DataAnnotations.DataTypeAttribute && (attr as System.ComponentModel.DataAnnotations.DataTypeAttribute).CustomDataType == "FilterContainer") ) { propertyName = string.Empty; containerType = null; } ModelMetadata metadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName); SearchFilterAttribute searchFilterAttribute = attributes.OfType <SearchFilterAttribute>().FirstOrDefault(); if (searchFilterAttribute != null) { metadata.AdditionalValues.Add(Globals.SearchFilterAttributeKey, searchFilterAttribute); } OrderByAttribute orderByAttribute = attributes.OfType <OrderByAttribute>().FirstOrDefault(); if (orderByAttribute != null) { metadata.AdditionalValues.Add(Globals.OrderByAttributeKey, orderByAttribute); } TypeConverterAttribute converterAttribute = attributes.OfType <TypeConverterAttribute>().FirstOrDefault(); if (converterAttribute != null) { metadata.AdditionalValues.Add(Globals.TypeConverterAttributeKey, converterAttribute); } return(metadata); }