RacerDelux
RacerDelux
Explore posts from servers
CC#
Created by RacerDelux on 9/17/2024 in #help
Best Way To Load and Cache Scoped Data?
I am using MediatR, so scoped services essentially live the lifetime of the MediatR request. Given that, I want to load configuration from a database if not loaded (using Entity Framework), then cache the returned data, as it is used multiple times for the request. What is the best way to go about this? This is my current code, but this is blocking and I would think could cause performance issues. This particular example would remove 4 round trip database calls.
public class ScopedCachingService(DbContext context)
{
private Configurations[]? GlobalConfiguration { get; set; }

public Configurations[] GetGlobalConfigurationAsync()
{
return GlobalConfiguration ??= context.Configuration.OrderBy(c => c.ConfigKey).ToArray();
}
}
public class ScopedCachingService(DbContext context)
{
private Configurations[]? GlobalConfiguration { get; set; }

public Configurations[] GetGlobalConfigurationAsync()
{
return GlobalConfiguration ??= context.Configuration.OrderBy(c => c.ConfigKey).ToArray();
}
}
11 replies
CC#
Created by RacerDelux on 7/24/2024 in #help
Dynamic Order By Entity Framework
I have the following query:
context.Posts.Where(p => p.isActive).OrderBy(p => p.Name.StartsWith(searchText));
context.Posts.Where(p => p.isActive).OrderBy(p => p.Name.StartsWith(searchText));
I was asked to make it so that searchText can have multiple searches, IE: If I type in "Hello World", I should have the results ordered so that anything that starts with "Hello" or "World" appears first. But this needs to be dynamic in length. So the search text "Hello World Bob" would check for three items. In addition, I need to support partial matches. So "Hel Wor Bo" would at minimum return the same results as "Hello World Bob". How would I go about doing this? If I didn't need to worry about partial matches, I could just do p => searchText.Contains(p.Name). But I need to support partial queries.
16 replies