[英]Avoid client evaluation while maintain clean code in EF Core 3
I need to write more complicated queries to my database.我需要向我的数据库编写更复杂的查询。 It is easily possible to maintain clean code with client evaluation on, but I am dealing with a lot of data and need to keep my backend quick.
通过客户端评估很容易维护干净的代码,但我正在处理大量数据并且需要保持我的后端快速。
Customer entity is nice example of it.客户实体就是一个很好的例子。 This is how my query looks like now:
这就是我的查询现在的样子:
public class CustomerFilter : AbstractProjectFilter
{
public CustomerFilter(string query, bool includeNotSet, ICollection<string> customers) :
base(e => (customers == null)
|| (includeNotSet && e.Customer == null)
|| (customers.Contains("Company") && e.Customer.Type == CustomerType.Company &&
EF.Functions.Like((((CustomerCompany)e.Customer).Name + " " + ((CustomerCompany)e.Customer).Crn + " " + ((CustomerCompany)e.Customer).VatRegNo), "%" + query + "%"))
|| (customers.Contains("Person") && e.Customer.Type == CustomerType.Person &&
EF.Functions.Like(((CustomerPerson)e.Customer).Forename + " " + ((CustomerPerson)e.Customer).Surname, "%" + query + "%"))
|| (customers.Contains("Evidence") && e.Customer.Type == CustomerType.InEvidence &&
EF.Functions.Like(e.Customer.EvidenceName, "%" + query + "%"))
)
{
}
}
With client evaluation on, this would be much cleaner, because I would be able to use this method to create name string based on customer derived type with extension method (I am intentionally avoiding virtual methods) like this and use it to make my query short and clean:启用客户端评估后,这会更加简洁,因为我可以使用这种方法来创建基于客户派生类型的名称字符串,并使用扩展方法(我故意避免使用虚拟方法),并使用它来缩短我的查询和清洁:
public static string NameString(this Customer customer)
{
if (customer.IsObjectNull())
return string.Empty;
return customer.Type switch
{
CustomerType.InEvidence => ((CustomerInEvidence) customer).EvidenceName,
CustomerType.Person => (((CustomerPerson) customer).Forename + " " +
((CustomerPerson) customer).Surname),
CustomerType.Company => ((CustomerCompany) customer).Name,
_ => throw new ArgumentOutOfRangeException()
};
}
My question: Is there a way to tell the database how to handle methods without the need to fetch data from it?我的问题:有没有办法告诉数据库如何处理方法而不需要从中获取数据? Some configuration of Fluent API I am missing?
我缺少 Fluent API 的一些配置?
It would also be nice if I would be able to use these "methods" during sorting with Linq.Dynamic like this:如果我能够在使用 Linq.Dynamic 进行排序时使用这些“方法”,那就太好了,如下所示:
var orderedCustomers = await customers.OrderBy("*string_created_by_some_method* ASC").ToListAsync();
Is there a way to tell the database how to handle methods without the need to fetch data from it?
有没有办法告诉数据库如何处理方法而不需要从中获取数据? Some configuration of Fluent API I am missing?
我缺少 Fluent API 的一些配置?
There isn't any straightforward way, this way is exactly what Expression
is!没有什么直截了当的方式,这就是
Expression
的方式!
The only way that you can talk to database without fetching data is using Expressions
.在不获取数据的情况下与数据库通信的唯一方法是使用
Expressions
。 it is not very straight but can do everything that you need.它不是很直,但可以做你需要的一切。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.