// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using Microsoft.AspNet.Mvc.ModelBinding; namespace Microsoft.AspNet.Mvc { /// /// Default implementation for . /// Provides a expression based way to provide include properties. /// /// The target model Type. public class DefaultPropertyBindingPredicateProvider : IPropertyBindingPredicateProvider where TModel : class { private static readonly Func _defaultFilter = (context, propertyName) => true; /// /// The prefix which is used while generating the property filter. /// public virtual string Prefix { get { return string.Empty; } } /// /// Expressions which can be used to generate property filter which can filter model /// properties. /// public virtual IEnumerable>> PropertyIncludeExpressions { get { return null; } } /// public virtual Func PropertyFilter { get { if (PropertyIncludeExpressions == null) { return _defaultFilter; } // We do not cache by default. return GetPredicateFromExpression(PropertyIncludeExpressions); } } private Func GetPredicateFromExpression( IEnumerable>> includeExpressions) { var expression = ModelBindingHelper.GetIncludePredicateExpression(Prefix, includeExpressions.ToArray()); return expression.Compile(); } } }