Подтвердить что ты не робот

Полиморфная модель Взаимозаменяемые деревья выражений

Я пытаюсь выяснить способ структурирования своих данных, чтобы он был совместимым с моделью. Моя проблема в том, что я должен создать фильтр запросов, который может представлять несколько выражений в данных.

Например:

x = > (x.someProperty == true & x.someOtherProperty == false) || x.UserId == 2

x = > (x.someProperty & x.anotherProperty) || (x.userId == 3 & x.userIsActive)

Я создал эту структуру, которая представляет все выражения в порядке, мой вопрос, как я могу сделать это так, чтобы свойство Model Bindable

public enum FilterCondition
{
    Equals,
}

public enum ExpressionCombine
{
    And = 0,
    Or
}

public interface IFilterResolver<T>
{
    Expression<Func<T, bool>> ResolveExpression();
}

public class QueryTreeNode<T> : IFilterResolver<T>
{
    public string PropertyName { get; set; }
    public FilterCondition FilterCondition { get; set; }
    public string Value { get; set; }
    public bool isNegated { get; set; }

    public Expression<Func<T, bool>> ResolveExpression()
    {
        return this.BuildSimpleFilter();
    }
}

//TODO: rename this class
public class QueryTreeBranch<T> : IFilterResolver<T>
{
    public QueryTreeBranch(IFilterResolver<T> left, IFilterResolver<T> right, ExpressionCombine combinor)
    {
        this.Left = left;
        this.Right = right;
        this.Combinor = combinor;
    }

    public IFilterResolver<T> Left { get; set; }
    public IFilterResolver<T> Right { get; set; }
    public ExpressionCombine Combinor { get; set; }

    public Expression<Func<T, bool>> ResolveExpression()
    {
        var leftExpression = Left.ResolveExpression();
        var rightExpression = Right.ResolveExpression();

        return leftExpression.Combine(rightExpression, Combinor);
    }
}

Мои левые правые члены просто должны быть разрешены к IResolvable, но привязка к модели привязывается только к конкретным типам. Я знаю, что могу написать настраиваемое связующее устройство, но я бы предпочел просто создать структуру, которая работает.

Я знаю, что могу передать json как решение, но, как требование, я не могу

Есть ли способ, которым я могу усовершенствовать эту структуру, чтобы она все еще представляла все простое выражение, будучи моделью Bindable? или есть простой способ применить эту структуру так, чтобы она работала с привязкой к модели?

ИЗМЕНИТЬ На всякий случай, когда кто-то задается вопросом, мой выражающий строитель имеет белый список выражений членов, который он фильтрует. Работа с динамической фильтрацией Я просто ищу способ привязки этой структуры, естественно, чтобы мой контроллер мог взять QueryTreeBranch или принять структуру, которая точно представляет одни и те же данные.

public class FilterController
{
     [HttpGet]
     [ReadRoute("")]
     public Entity[]  GetList(QueryTreeBranch<Entity> queryRoot)
     {
         //queryRoot no bind :/
     }
}

В настоящее время IFilterResolver имеет 2 реализации, которые необходимо выбирать динамически на основе данных, переданных

Я ищу решение, наиболее подходящее для рамки WebApi/MVC. Предпочтительный, который НЕ требует, чтобы я адаптировал вход к другой структуре, чтобы генерировать мое выражение

4b9b3361

Ответ 1

На первый взгляд вы можете разделить логику фильтрации на DTO, которая содержит дерево выражений, не зависящее от типа сущности, и генератор, зависящий от типа Expression<Func<T, bool>>. Таким образом, мы можем избежать создания универсального и полиморфного DTO, что вызывает трудности.

Можно заметить, что вы использовали полиморфизм (2 реализации) для IFilterResolver<T>, потому что вы хотите сказать, что каждый node дерева фильтрации является либо листом, либо ветвью (это также называется disjoint union).

Model

Хорошо, если эта определенная реализация вызывает пропеты, попробуйте другой:

public class QueryTreeNode
{
    public NodeType Type { get; set; }
    public QueryTreeBranch Branch { get; set; }
    public QueryTreeLeaf Leaf { get; set; }
}

public enum NodeType
{
    Branch, Leaf
}

Конечно, для такой модели вам понадобится проверка.

Итак, node является либо ветвью, либо листом (я немного упростил лист здесь):

public class QueryTreeBranch
{
    public QueryTreeNode Left { get; set; }
    public QueryTreeNode Right { get; set; }
    public ExpressionCombine Combinor { get; set; }
}

public class QueryTreeLeaf
{
    public string PropertyName { get; set; }
    public string Value { get; set; }
}

public enum ExpressionCombine
{
    And = 0, Or
}

DTO выше не так удобно создавать из кода, поэтому для генерации этих объектов можно использовать следующий класс:

public static class QueryTreeHelper
{
    public static QueryTreeNode Leaf(string property, int value)
    {
        return new QueryTreeNode
        {
            Type = NodeType.Leaf,
            Leaf = new QueryTreeLeaf
            {
                PropertyName = property,
                Value = value.ToString()
            }
        };
    }

    public static QueryTreeNode Branch(QueryTreeNode left, QueryTreeNode right)
    {
        return new QueryTreeNode
        {
            Type = NodeType.Branch,
            Branch = new QueryTreeBranch
            {
                Left = left,
                Right = right
            }
        };
    }
}

Просмотр

Не должно быть проблем с привязкой такой модели (ASP.Net MVC в порядке с рекурсивными моделями, см. этот вопрос). Например. после фиктивных представлений (поместите их в папку \Views\Shared\EditorTemplates).

Для ветки:

@model WebApplication1.Models.QueryTreeBranch

<h4>Branch</h4>
<div style="border-left-style: dotted">
    @{
        <div>@Html.EditorFor(x => x.Left)</div>
        <div>@Html.EditorFor(x => x.Right)</div>
    }
</div>

Для листа:

@model WebApplication1.Models.QueryTreeLeaf

<div>
    @{
        <div>@Html.LabelFor(x => x.PropertyName)</div>
        <div>@Html.EditorFor(x => x.PropertyName)</div>
        <div>@Html.LabelFor(x => x.Value)</div>
        <div>@Html.EditorFor(x => x.Value)</div>
    }
</div>

Для node:

@model WebApplication1.Models.QueryTreeNode

<div style="margin-left: 15px">
    @{
        if (Model.Type == WebApplication1.Models.NodeType.Branch)
        {
            <div>@Html.EditorFor(x => x.Branch)</div>
        }
        else
        {
            <div>@Html.EditorFor(x => x.Leaf)</div>
        }
    }
</div>

Использование образца:

@using (Html.BeginForm("Post"))
{
    <div>@Html.EditorForModel()</div>
}

контроллер

Наконец, вы можете реализовать генератор выражений, беря фильтрацию DTO и тип T, например. из строки:

public class SomeRepository
{
    public TEntity[] GetAllEntities<TEntity>()
    {
        // Somehow select a collection of entities of given type TEntity
    }

    public TEntity[] GetEntities<TEntity>(QueryTreeNode queryRoot)
    {
        return GetAllEntities<TEntity>()
            .Where(BuildExpression<TEntity>(queryRoot));
    }

    Expression<Func<TEntity, bool>> BuildExpression<TEntity>(QueryTreeNode queryRoot)
    {
        // Expression building logic
    }
}

Затем вы вызываете его из контроллера:

using static WebApplication1.Models.QueryTreeHelper;

public class FilterController
{
    [HttpGet]
    [ReadRoute("")]
    public Entity[]  GetList(QueryTreeNode queryRoot, string entityType)
    {
        var type = Assembly.GetExecutingAssembly().GetType(entityType);
        var entities = someRepository.GetType()
            .GetMethod("GetEntities")
            .MakeGenericMethod(type)
            .Invoke(dbContext, queryRoot);
    }

    // A sample tree to test the view
    [HttpGet]
    public ActionResult Sample()
    {
        return View(
            Branch(
                Branch(
                    Leaf("a", 1),
                    Branch(
                        Leaf("d", 4),
                        Leaf("b", 2))),
                Leaf("c", 3)));
    }
}

UPDATE:

Как обсуждалось в комментариях, лучше иметь один класс модели:

public class QueryTreeNode
{
    // Branch data (should be null for leaf)
    public QueryTreeNode LeftBranch { get; set; }
    public QueryTreeNode RightBranch { get; set; }

    // Leaf data (should be null for branch)
    public string PropertyName { get; set; }
    public string Value { get; set; }
}

... и один шаблон редактора:

@model WebApplication1.Models.QueryTreeNode

<div style="margin-left: 15px">
    @{
        if (Model.PropertyName == null)
        {
            <h4>Branch</h4>
            <div style="border-left-style: dotted">
                <div>@Html.EditorFor(x => x.LeftBranch)</div>
                <div>@Html.EditorFor(x => x.RightBranch)</div>
            </div>
        }
        else
        {
            <div>
                <div>@Html.LabelFor(x => x.PropertyName)</div>
                <div>@Html.EditorFor(x => x.PropertyName)</div>
                <div>@Html.LabelFor(x => x.Value)</div>
                <div>@Html.EditorFor(x => x.Value)</div>
            </div>
        }
    }
</div>

Опять же, этот способ требует большой проверки.

Ответ 2

Для универсального класса вы должны использовать собственное связующее вещество данных.

Смотрите предыдущий question, который имел аналогичную потребность в предыдущей версии с использованием веб-форм и Документация по Microsoft.

Другой вариант - передать сериализованную версию класса.

Ответ 3

Я создал интерфейсное связующее, которое работает вне стандартного ComplexTypeModelBinder

//Redefine IModelBinder so that when the ModelBinderProvider Casts it to an 
//IModelBinder it uses our new BindModelAsync
public class InterfaceBinder : ComplexTypeModelBinder, IModelBinder
{
    protected TypeResolverOptions _options;
    //protected Dictionary<Type, ModelMetadata> _modelMetadataMap;
    protected IDictionary<ModelMetadata, IModelBinder> _propertyMap;
    protected ModelBinderProviderContext _binderProviderContext;

    protected InterfaceBinder(TypeResolverOptions options, ModelBinderProviderContext binderProviderContext, IDictionary<ModelMetadata, IModelBinder> propertyMap) : base(propertyMap)
    {
        this._options = options;
        //this._modelMetadataMap = modelMetadataMap;
        this._propertyMap = propertyMap;
        this._binderProviderContext = binderProviderContext;
    }

    public InterfaceBinder(TypeResolverOptions options, ModelBinderProviderContext binderProviderContext) :
        this(options, binderProviderContext, new Dictionary<ModelMetadata, IModelBinder>())
    {
    }

    public new Task BindModelAsync(ModelBindingContext bindingContext)
    {
        var propertyNames = bindingContext.HttpContext.Request.Query
            .Select(x => x.Key.Trim());

        var modelName = bindingContext.ModelName;
        if (false == string.IsNullOrEmpty(modelName))
        {
            modelName = modelName + ".";
            propertyNames = propertyNames
                .Where(x => x.StartsWith(modelName, StringComparison.OrdinalIgnoreCase))
                .Select(x => x.Remove(0, modelName.Length));
        }

        //split always returns original object if empty
        propertyNames = propertyNames.Select(p => p.Split('.')[0]);
        var type = ResolveTypeFromCommonProperties(propertyNames, bindingContext.ModelType);

        ModelBindingResult result;
        ModelStateDictionary modelState;
        object model;
        using (var scope = CreateNestedBindingScope(bindingContext, type))
        {
             base.BindModelAsync(bindingContext);
            result = bindingContext.Result;
            modelState = bindingContext.ModelState;
            model = bindingContext.Model;
        }

        bindingContext.ModelState = modelState;
        bindingContext.Result = result;
        bindingContext.Model = model;

        return Task.FromResult(0);
    }

    protected override object CreateModel(ModelBindingContext bindingContext)
    {
        return Activator.CreateInstance(bindingContext.ModelType);
    }

    protected NestedScope CreateNestedBindingScope(ModelBindingContext bindingContext, Type type)
    {
        var modelMetadata = this._binderProviderContext.MetadataProvider.GetMetadataForType(type);

        //TODO: don't create this everytime this should be cached
        this._propertyMap.Clear();
        for (var i = 0; i < modelMetadata.Properties.Count; i++)
        {
            var property = modelMetadata.Properties[i];
            var binder = this._binderProviderContext.CreateBinder(property);
            this._propertyMap.Add(property, binder);
        }

        return bindingContext.EnterNestedScope(modelMetadata, bindingContext.ModelName, bindingContext.ModelName, null);
    }

    protected Type ResolveTypeFromCommonProperties(IEnumerable<string> propertyNames, Type interfaceType)
    {
        var types = this.ConcreteTypesFromInterface(interfaceType);

        //Find the type with the most matching properties, with the least unassigned properties
        var expectedType = types.OrderByDescending(x => x.GetProperties().Select(p => p.Name).Intersect(propertyNames).Count())
            .ThenBy(x => x.GetProperties().Length).FirstOrDefault();

        expectedType = interfaceType.CopyGenericParameters(expectedType);

        if (null == expectedType)
        {
            throw new Exception("No suitable type found for models");
        }

        return expectedType;
    }

    public List<Type> ConcreteTypesFromInterface(Type interfaceType)
    {
        var interfaceTypeInfo = interfaceType.GetTypeInfo();
        if (interfaceTypeInfo.IsGenericType && (false == interfaceTypeInfo.IsGenericTypeDefinition))
        {
            interfaceType = interfaceTypeInfo.GetGenericTypeDefinition();
        }

        this._options.TypeResolverMap.TryGetValue(interfaceType, out var types);
        return types ?? new List<Type>();
    }
}

Затем вам понадобится поставщик привязки модели:

public class InterfaceBinderProvider : IModelBinderProvider
{
    TypeResolverOptions _options;

    public InterfaceBinderProvider(TypeResolverOptions options)
    {
        this._options = options;
    }
    public IModelBinder GetBinder(ModelBinderProviderContext context)
    {
        if (!context.Metadata.IsCollectionType &&
            (context.Metadata.ModelType.GetTypeInfo().IsInterface ||
             context.Metadata.ModelType.GetTypeInfo().IsAbstract) &&
            (context.BindingInfo.BindingSource == null ||
            !context.BindingInfo.BindingSource
            .CanAcceptDataFrom(BindingSource.Services)))
        {
            return new InterfaceBinder(this._options, context);
        }

        return null;
    }
}

а затем вы введете в свои услуги вложение:

var interfaceBinderOptions = new TypeResolverOptions();

interfaceBinderOptions.TypeResolverMap.Add(typeof(IFilterResolver<>), 
    new List<Type> { typeof(QueryTreeNode<>), typeof(QueryTreeBranch<>) });
var interfaceProvider = new InterfaceBinderProvider(interfaceBinderOptions);

services.AddSingleton(typeof(TypeResolverOptions), interfaceBinderOptions);

services.AddMvc(config => {
    config.ModelBinderProviders.Insert(0, interfaceProvider);
});

Затем вы настроите свои контроллеры таким образом

public MessageDTO Get(IFilterResolver<Message> foo)
{
    //now you can resolve expression etc...
}