简体   繁体   English

避免客户端评估,同时在 EF Core 3 中保持干净的代码

[英]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.它不是很直,但可以做你需要的一切。

You can see one example of it here你可以在这里看到一个例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 EF Core Any in Any客户端评估 - EF Core Any in Any client side evaluation EF Core 异步客户端评估 - EF Core async client side evaluation EF core 3嵌套组通过限制客户端评估后 - EF core 3 nested group by after restricting client evaluation 带有布尔异步调用的 EF Core 3.1 客户端评估问题 - EF Core 3.1 Client Side Evaluation Issue With Boolean Async Call 清洁代码问题:EF Core 参数化要包含的属性 - Clean code question: EF Core parametrize which properties to include 当最上面的投影包含方法调用时,EF Core 客户端评估不起作用 - EF Core client evaluation doesn't work, when top most projection contains method call EF Core 3.1 - 如何使用 InMemory Provider 检测客户端评估错误? - EF Core 3.1 - how to detect client-side evaluation errors using the InMemory Provider? EF Core 3.x - 简单 LINQ with Include 无法翻译,客户评价 - EF Core 3.x - simple LINQ with Include can not be translated, client evaluation 如何修改基于表达式的过滤器以避免在 Entity Framework Core 3.0 中进行客户端评估 - How to modify expression-based filters to avoid client-side evaluation in Entity Framework Core 3.0 如何确定 EF 核心中的自定义服务器评估? - How to determine custom server evaluation in EF core?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM