UltraWelfare
✅ Waiting on a task more efficiently
I've made this class for my application that needs to process some things in the background using a queue:
Is there anything to do to better optimize the while loop when no jobs are in the queue? I've done a simple 1s delay but I'd bet there's a better way of doing it..?
8 replies
Blazor Server not responding
I tried for the sake of laughs, to convert my blazor hybrid to blazor server...
However as expected it doesn't work. It compiles and runs, but opening the localhost url it doesn't respond (endless http loading)...
Any ideas on how to debug it:)?
4 replies
Primary constructor parameter is null after being run in a Task
The following snippet of code is responsible for starting a background service
This service is being registered with Microsoft DI as a singleton.
When the
StartExecuting
function is called, the callService
is correctly being passed.
However if I set a breakpoint inside the lambda of Task.Run, callService
is null there...
Any ideas?12 replies
Tips on error handling
I'm currently using OneOf to handle errors.
For example:
However that is kinda of mouthful. I could try to refactor it using OneOfBase, but there are cases where I need <None, NotFound> or <None, NotFound, GenericError> or really any combination...
Is there any way I can refactor it?
I'd like to avoid exceptions as this would migrate the problem to "try-catching" everything then `if - is" on the exceptions
7 replies
How to structure query functions in services
I have the following function in my service layer csharp:
PS: A category can have List<Item>
Sometimes I need
GetCategories()
to just be the Category
without any navigation properties included.
But also sometimes I need GetCategories()
to also include the Items
navigation property...
I'm thinking of having two functions with two separate dtos (one has only the category, the other one has the category + the items).
But I was wondering if there's a better way to do this22 replies
Organizing and naming different closely related Dtos
My current setup is as follows (I name my Dtos as 'Models'):
The order can be of different types (such as draft or receipt, and there's more but I'm keeping it simple).
But we also need a way to fetch multiple types at once (hence why a basic Order exists).
There is reason that there's a difference between all of them (some relationship constraints when orders are being transformed).
My main problem is the mess that is happening between the models.
The DraftOrderModel should expose a
List<ReceiptOrderModel>
. However when you receive the draft order, you don't want the -full- details of the receipt order (since they contain all the order items etc which is not needed).
You want a slimmer model of the receipt order that contains basic stuff such as its id, it's title and maybe its price...
But I'm not sure how to make this..? Should I make a new DraftOrderReceiptOrderShortModel
? That sounds obnoxious....
Should I make a nested class ReceiptOrderShortModel
inside DraftOrderModel
?
Should I make a general OrderShortModel
and reuse it whenever I want ?
I'm not sure how to continue without making it more of a mess:)47 replies
Interface using functional `Match` does not work
I'm using the OneOf library for discriminated unions
I have to use TypedResults in order to get OpenAPI to understand things.
For those unaware of the library
Match
works as public TResult Match<TResult>(Func<T0,TResult> f0, Func<T1,TResult> f1)
So it will either call the first lambda if CreateCustomerAsync
returns a CustomerResult
or the second lambda if it returns ValueNotFound
TypedResults.Created
and TypedResults.NotFound()
both extend the IResult
interface, so I hoped it would work but it seems like it doesn't
Cannot convert expression type 'Microsoft.AspNetCore.Http.IResult' to return type 'Microsoft.AspNetCore.Http.HttpResults.Results<Microsoft.AspNetCore.Http.HttpResults.Created<Support.Features.Customers.CreateCustomer.CreateCustomerResponse>,Microsoft.AspNetCore.Http.HttpResults.NotFound>'
62 replies
Simplifying creation of inherited classes
Basically we need to fetch Entity from the database and then map it into A or B depending on a field.
That means you'll have to repeat the field copies a lot. With more fields and more "A"-"B"-"C"-"D"s... this will become a nightmare to maintain. Any other ideas?
3 replies
❔ EFCore Interceptor problem
In the MS documentation I see they're using the SaveChanges interceptor as:
I'm trying to do the same but I get an error and I presume it's because of the value task:
The error is that it cannot convert result to
ValueTask...
. how does the MS example work though...3 replies
❔ Blazor and Auth - Identity
I'm using Blazor Hybrid (closer to Blazor WASM) for a desktop app therefore I've made my own custom "AuthenticationStateProvider" to specify my own logic.
As far as it seems like I can get the AuthenticationState which includes the ClaimsPrincipal alongside its Identity and claims.
I've put the account's username and id inside claims but they seem to only receive string.
It's a bit cumbersome to use claims to get the id, and then cast it to int (since int is my id type).
Is there any better way to do it ? I was thinking of getting my model directly from my custom authentication state provider (I have a variable
_currentUser
set), but i don't know if that's a good idea3 replies
❔ EFCore Change Tracker Confusion
Assuming a entity
Order
with a relationship to entity Table
Shouldn't the "sameOrder" be fetched from the tracker and do a single query for the table only? Since it's already being tracked..
Because in my application I'm seeing it fetches the whole order from the start...
Unless there's some kinda of rule about relationships that I couldn't find38 replies
❔ EFCore Expression Reuse Translations`
I have the following piece of projector:
Where
Order
is the EFCore entity and OrderModel
is a POCO.
Whenever I do the following : db.Orders.Select(Mapper.ProjectToOrderModel())
The correct SQL is generated by using the projection fields and not querying the whole columns.
This surprised me because ProjectToOrderTableModel
is a function and not an expression.
However things get confusing when we get into lists. Rewriting the above snippet trying to include a nested collection
This fails because the Expression of type System.Func
cannot be translated, which makes sense.
I cannot understand how it works for a singular model, but it cannot work on a collection... ? Is there any tracking bug, or is it a forbidden thing to do?!
P.S: I'm aware of the solution of implementing a public static Expression<Func<OrderItem, OrderItemModel>>
, I just don't understand why the above doesn't work17 replies
❔ Method either receive a disposable [context] or use a new one (EFCore - Blazor)
I'm in a blazor project where I wanna reuse some query functions.
Basically sometimes I either
1. Wanna use the method on itself where it should create and dispose a new context created by the contextFactory.
2. Wanna use the method inside another method that already has a context, and just wanna pass it so it doesn't create an unneccesary one.
What I currently thought of is this:
However I do not like the idea of manually disposing when a context is not passed.
Are there any other ideas on how to approach this?
3 replies