zuf
zuf
CC#
Created by zuf on 2/15/2023 in #help
❔ EF Core Query filters and DbContext lifetime
Hey! To my project, Using jwt's which contains the users id, after normal auth etc run a middleware runs that will pull data from the database (injecting a DbContext) and create a Identity and add it to the HttpContext. Now my DbContext is using query filters, those query filters are configured by data from the HttpContext which was modified by my middleware. The issue is that since my scoped DbContext was already injected in the middleware it was configured with HttpContext data before it was modified with the extra information. Whats the solution here? I would prefer keeping my DbContext scoped, but the only solution I found so far would be changing it to transient. The query filter reads from properties in my DbContext (using a factory etc), would it be bad practice to manually assign the values I got from the database in my injected DbContext?
5 replies
CC#
Created by zuf on 10/13/2022 in #help
EF Core & Repository pattern
Our app was initially built using MongoDB and we implemented a repository layer over it. We then migrated to postgres & EF Core and basically kept the repository layer in the initial migration phase. Now I understand its more of an anti-pattern & we are ready to move away from it, what is the "norm" alternative? Instead of having repositories build extension methods? Like this? (very basic example from a blog post)
public static class MyLinqExtension
{
public static IQueryable<int> MyOrder
(this IQueryable<int> queryable, bool ascending)
{
return ascending
? queryable.OrderBy(num => num)
: queryable.OrderByDescending(num => num);
}
}
public static class MyLinqExtension
{
public static IQueryable<int> MyOrder
(this IQueryable<int> queryable, bool ascending)
{
return ascending
? queryable.OrderBy(num => num)
: queryable.OrderByDescending(num => num);
}
}
Mostly referring to Patrick's post here https://discord.com/channels/143867839282020352/536023005164470303/550428647719174363 which started the discussion in our team. Biggest concern is wanting to re use queries in other services etc.
10 replies
CC#
Created by zuf on 10/12/2022 in #help
Dynamic settings (auto update)
Hey! To put it very basic, I have a static class in my project Constants with a static property Settings my settings property is a class with a lot of properties such as connection strings and other relevant settings for our app. The goal is to update this entire class dynamically during run time. To put it very simple, can I just register a worker that pulls the new settings every x minutes and just replaces the Settings object in my Constants class or am I going to run into thread safety issues?
12 replies