我們是一個小型的開發團隊。我們在 ASP.NET 中開發,我們開始使用通用控制器和服務。
目標是為重復的事情提供可靠的方法。
我們問自己,在資料模型中進行一些轉換以允許我們重用我們知道正在作業的函式是否是個好主意?
例如:我們有一個組合框,我們想管理顯示和搜索。它總是相同和多余的。
這是我的課
[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;
}
如您所見,我正在使用一個介面來添加列以將正確的列重定向到我的資料模型,以避免覆寫我的兩列方法。
這是一個好主意,一個好的做法嗎?
如果我們想做泛型函式,但列的呼叫方式不同,您認為最佳實踐是什么?
謝謝!
uj5u.com熱心網友回復:
你的解決方案有很多弱點
- 您已經擴展了 Model 來處理特定的 UI 案例。在我看來,這是不好的做法。
- 您的虛擬屬性在 LINQ 查詢中不起作用。EF 翻譯只是
Expression因為它無法查看已編譯的屬性體。
我們在這里可以做的是簡化構建此類組合框的程序。我已經定義了可以在這種情況下重用的擴展集。抱歉,如果有一些錯誤,憑記憶寫的。
如何使用:
假設 GetComboboxViewModel 不在泛型類中
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;
}
考慮一下這個解決方案,是的,我們可以在某處注冊一對 LmbdaDictionary<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 generationg 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對其進行基本的簡化。如果您有興趣,將發布有關 LINQKit 用法的另一個答案,
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/387048.html
上一篇:如何使此影像正確顯示?
