使用 ASP.Net 在数据库模型中进行转换是个好主意吗

is it a good idea to do transformation in a database model with ASP.Net

我们是一个小型开发团队。 我们在 ASP.NET 进行开发,并且开始使用通用控制器和服务。

我们的目标是为重复的事情提供可靠的方法。

我们问自己的是,在数据模型中进行一些转换以允许我们重用我们知道有效的函数是否是个好主意?

示例:我们有一个组合框,我们想要管理显示和搜索。它总是相同的和多余的。

这是我的class

[Table("stage.Test")]
public partial class Test : IBaseEntity, ICombobox
{

    public virtual Product Product { get; set; }
    public  string nom { get; set; }
    public  string  prenom { get; set; }
    public  string title { get; set; }

    [NotMapped]
    public virtual string AffichageCombobox => nom + prenom;
    [NotMapped]
    public virtual string TexteRecherche => Product.Gabarit.Description;

 }

如您所见,我有两列带有标签 [NotMapped]。这些是界面中的列 ICombobox

    public interface ICombobox
{
    string AffichageCombobox { get;}
    string TexteRecherche { get; }
}

这是我使用我的两列之一重定向到其他列的第一项服务。 [我们使用模型中的列“AffichageCombobox”]

        public List<ComboboxViewModel> GetComboboxViewModel(int? id, bool actifseulement, string text)
    {
        var query = _requestDatabaseService.GetComboboxQuery<T>(id, actifseulement, text);
        var list = query.Select(table => new ComboboxViewModel
        {
            Id = table.Id,
            AffichageCombobox = table.DateHFin == null ? table.AffichageCombobox : table.AffichageCombobox + " (inactif)"
        }).ToList();

        return list;
    }   

这是 RequestDatabaseService [我们使用模型中的“TexteRecherche”列]

        public List<T> GetComboboxQuery<T>(int? id, bool actifseulement, string text) where T : class, IBaseEntity, ICombobox
    {
        text = text.ToLower();

        var list = _dbContext.Set<T>()
            .If(id.HasValue,
                q => q.Where(x => x.Id == id))
            .If(actifseulement,
                q => q.Where(x => x.DateHFin == null))
            .If(text != "",
                q => q.Where(x => x.TexteRecherche.ToLower() == text))
            .ToList();
        return list;
    }

如您所见,我正在使用一个界面来添加列以重定向到我的数据模型的正确列,以避免覆盖我的两列方法。

这是个好主意,好做法吗?

如果我们想做泛型函数,但列的调用方式不同,您认为最佳做法是什么?

谢谢!

你的解决方案有很多弱点

  1. 您已扩展模型以处理特定的 UI 案例。在我看来这是不好的做法。
  2. 您的虚拟属性将无法在 LINQ 查询中使用。 EF 仅翻译 Expression 因为它无法查看已编译的 属性 主体。

我们在这里可以做的是简化构建此类组合框的过程。我已经定义了一组可以在这种情况下重复使用的扩展。凭记忆写的,如有错误请见谅

如何使用:

假设 GetComboboxViewModel 不是通用的 class

public List<ComboboxViewModel> GetComboboxViewModel(int? id, bool actifseulement, string text)
{
    // uncover DbContext. All that we need is IQueryable<Test>
    var ctx = _requestDatabaseService.GetContext();
    var query = ctx.Test.AsQueryable();

    var comboItems = query
        .FilterItems(id, actifseulement)
        .GetComboboxQuery(text, e => e.Product.Gabarit.Description, e => e.nom + e.prenom)
        .ToList();

    return comboItems;
}

想想这个解决方案,是的,我们可以在某处注册一对 Lmbdas Dictionary<Type, (LambdaExpression: searchProp, LambdaExpression: displayProp)> 并在上面动态构建调用。

实现:

public static class QueryableExtensions
{ 
    // more simlified version for filtering
    public static IQueryable<T> WhereIf(this IQueryable<T> query, bool condition, Expression<Func<T, bool>> predicate)
    {
        return condition ? query.Where(predicate) : query;
    }

    // handy function for filtering
    public static IQueryable<T> FilterItems<T>(this IQueryable<T> query, int? id, bool onlyActive) 
        where T : IBaseEntity
    {
        query = query
            .WhereIf(id.HasValue, x => x.Id == id)
            .WhereIf(onlyActive, x => x.DateHFin == null)

        return query;
    }

    // dynamic generation of filtering and projection
    public static IQueryable<ComboboxViewModel> GetComboboxQuery<T>(this IQueryable<T> query, string text, Expression<Func<T, string>> searchProp, Expression<Func<T, string>> dsiplayProp) 
        where T : IBaseEntity
    {
        if (!string.IsNullOrEmpty(text))
        {
            text = text.ToLower();

            // defining search pattern
            // this also extension point, you may use here `Contains` or FullText search functions
            Expression<Func<string, string, bool>> filterFunc = (s, t) => s.ToLower() == t;

            // reusing parameter from searchProp lambda
            var param = searchProp.Parameters[0];

            // applying pattern to searchprop
            var filterBody = ExpressionReplacer.GetBody(filterFunc, searchProp.Body, Expression.Constant(text));

            // applying generated filter
            var filterPredicate = Expression.Lambda<Func<T, bool>>(filterBody, param);
            query = query.Where(filterPredicate);
        }

        // defining template for Select
        Expression<Func<T, string, ComboboxViewModel>> createTemplate = (entity, dp) => new ComboboxViewModel
        {
            Id = entity.Id,
            AffichageCombobox = entity.DateHFin == null ? dp : dp + " (inactif)"
        };

        // reusing parameter from dsiplayProp lambda
        var entityParam = dsiplayProp.Parameters[0];

        // injecting dsiplayProp into createTemplate
        var selectBody = ExpressionReplacer.GetBody(createTemplate, entityParam, dsiplayProp.Body);

        var selectLambda = Expression.Lambda<Func<T, ComboboxViewModel>>(selectBody, entityParam);

        // applying projection
        var comboQuery = query.Select(selectLambda);
        return comboQuery;
    }

    // helper class for correcting expressions
    class ExpressionReplacer : ExpressionVisitor
    {
        readonly IDictionary<Expression, Expression> _replaceMap;

        public ExpressionReplacer(IDictionary<Expression, Expression> replaceMap)
        {
            _replaceMap = replaceMap ?? throw new ArgumentNullException(nameof(replaceMap));
        }

        public override Expression Visit(Expression exp)
        {
            if (exp != null && _replaceMap.TryGetValue(exp, out var replacement))
                return replacement;
            return base.Visit(exp);
        }

        public static Expression Replace(Expression expr, Expression toReplace, Expression toExpr)
        {
            return new ExpressionReplacer(new Dictionary<Expression, Expression> { { toReplace, toExpr } }).Visit(expr);
        }

        public static Expression Replace(Expression expr, IDictionary<Expression, Expression> replaceMap)
        {
            return new ExpressionReplacer(replaceMap).Visit(expr);
        }

        public static Expression GetBody(LambdaExpression lambda, params Expression[] toReplace)
        {
            if (lambda.Parameters.Count != toReplace.Length)
                throw new InvalidOperationException();

            return new ExpressionReplacer(Enumerable.Range(0, lambda.Parameters.Count)
                .ToDictionary(i => (Expression) lambda.Parameters[i], i => toReplace[i])).Visit(lambda.Body);
        }
    }
}

嗯,写完这个例子后,我想,使用LINQKit可以大大简化它。如果您有兴趣,请 post 另一个关于 LINQKit 用法的答案,