nox7
nox7
CC#
Created by nox7 on 4/24/2024 in #help
Mutex? ASP Core - Only Allowing a Method to Be Run One at a Time
I have a scenario where I want all requests to an endpoint to have the ability to only be run one at a time. This method is asynchronous. The situation: When a client of ours logs into their dashboard, it shows a "marketing tips" blog section which pulls blog articles from a 3rd party. However, I don't need every request to hit the 3rd party list API for those articles - every client will see the same article. I want the HTTP request to list and cache those articles to only ever be running one at a time. In essence - If blogs need to be fetched from third party, do not let other processes run this request - Else, let processes run this request as often as they want (it will get the blogs from a local cache in this case) Will a Mutex work for this case?
2 replies
CC#
Created by nox7 on 4/15/2024 in #help
EF Core - Runtime-Based "OR" "AND" Comparison in Where()
I am allowing users on a web application to create filters on a queried table. It will generate an array of "filters" in defined order. They will be able to define if the next comparison is connected to the previous via "OR" or "AND" On the back-end, I will iterate over each "Filter" in the array of filters, adding more .Where() calls to the IQueryable. However, how can I make create an IQueryable that can properly chain these filters so that they are concatenated by "OR" or "AND" appropriately from the previous filter? E.g. User submits two filters for two columns. They could be linked as Column1 = "ss" OR Column2 = "DD" Or they could submit it as Column1 = "ss" AND Column2 = "DD"
8 replies
CC#
Created by nox7 on 4/3/2024 in #help
ASP Core 8 - Custom 404 Handling
No description
41 replies
CC#
Created by nox7 on 3/29/2024 in #help
EF Core - Joining Two Similar Tables Before In-Memory-Buffering Into One DTO
Because of how Stripe's API stores two specific objects (Subscription and SubscriptionSchedule), I have to have two EF Core models for those. However, they essentially have almost all the same columns. I want to present these as a single data set to my clients - orderable and sortable as if they were just one data set. How can I query both of those models in my DbContext and then combine them into a single Queryable result set as a SubscriptionDto - without triggering the in-memory buffering. I want to be able to order them/paginate them later on as a single result set so I don't want them to be buffered into memory until they are combined into a single Dto.
36 replies
CC#
Created by nox7 on 2/27/2024 in #help
[EF Core/LINQ] Paginated Eager-Loaded One-to-Many
I can't seem to find (maybe my Googling is bad) the proper way - if it's possible - to limit the returns of an eager-loaded one-to-many collection.
var form = DbContext.Forms
.Find(1)
.Include(form => form.Submission);
var form = DbContext.Forms
.Find(1)
.Include(form => form.Submission);
In my case a form could have millions of them - is it possible to limit the data fetched via .Include()? Similar to how Take() and Skip() work on the main query?
5 replies
CC#
Created by nox7 on 2/16/2024 in #help
✅ Pattern for PATCH API Parameter Existence Check
[ASP Core 8, .NET 8] I am developing some PATCH endpoints to update existing objects. I want my endpoint to only modify properties of the object that were set in the request - but null can only be a valid value to set. My problem seems to be that if I pass a URL parameter or payload with a parameter set, but it is blank, ASP is saying it is null. For example
[HttpPatch]
public IActionResult SomeMethod(
[FromForm] string? someProperty
){}
[HttpPatch]
public IActionResult SomeMethod(
[FromForm] string? someProperty
){}
With a payload
someProperty=
someProperty=
Then I would expect a blank string "". Instead, ASP gives me null. What pattern should I develop to make a proper PATCH endpoint that only utilizes parameters actually set in the payload?
2 replies
CC#
Created by nox7 on 2/9/2024 in #help
✅ ASP Core Attribute Logic Help
I need assistance with structuring my controller attribute logic (they're authorization attributes) in ASP Core 2 - I am using .NET 8. I have the following objects (for this purpose): ClientAccount, Dashboard, and StripeAccount. When a client accesses a route, I need to - Verify they are logged in - Verify they are accessing a Dashboard that exists - Verify they are accessing a Dashboard they have authorization to view - Verify, for some routes, the Dashboard has a connected StripeAccount My idea was to add these attributes to the controllers
[RequireClientAccount]
[RequireDashboard]
[RequireClientHasAccessToDashboard]
[RequireClientAccount]
[RequireDashboard]
[RequireClientHasAccessToDashboard]
Because some routes don't need the 3rd one. How can I approach this without each route having to run duplicate database calls? As in, RequireDashboard and RequireClientHasAccessToDashboard would both need to re-fetch the current Dashboard from the HttpContext since routes don't have any set order they run in - so they can't rely on one already having fetched the Dashboard object? Would a scoped object with static properties work to store if an object has already been fetched? Because attributes have no defined order, I am duplicating Db queries in each attribute to perform the same operation at the moment.
6 replies