C
C#13mo ago
linqisnice

❔ accessing collections via principal entity vs directly on dbset

what is generally preferred if i want the accommodations of a listing (WITH resource ownership check included, so firstasync is intentional here)? s Doing this:
var listing = await _dbContext.Listings
.AsSplitQuery()
.Include(x => x.Accommodations).ThenInclude(x => x.Inventory)
.FirstAsync(x => x.Id == request.ListingId && x.Host.UserId == currentUserId);

var accommodations = listing.Accommodations... // just an example
var listing = await _dbContext.Listings
.AsSplitQuery()
.Include(x => x.Accommodations).ThenInclude(x => x.Inventory)
.FirstAsync(x => x.Id == request.ListingId && x.Host.UserId == currentUserId);

var accommodations = listing.Accommodations... // just an example
or actually querying accommodations directly? I find that the above seems better, since it will immediately return the request if the user does not have access. whereas if we query accommodations, it's going to reutnr an empty list and then continue I guess my question pertains in general to resource ownership and accessing a collection items via prinicipal entity vs directly on the dbset. My strategy is generally to use firstasync since if the user is not the owner of the listing, they have likely tampered with the inspector or some such to finagle the resource
5 Replies
TravestyOfCode
TravestyOfCode13mo ago
I include the relations if I'm going to need them instead of separately. Do you need to differentiate between a ListingId not being found and the UserId not being the currentUserId? FirstAsync will throw an exception if the it doesn't find a match on the whole condition. Also, I tend to use SingleOrDefault when querying where I know there should be only one or none.
linqisnice
linqisnice13mo ago
@TofCodesingle needs to check the entire collection to ensure there are no other records, so performance wise first/firstordefault is faster the idea is for firstasync to either throw an exception if listingId doesn't exist or if the resource does not belong to the user trying to access it, because the only way listingId is even retrieved by the user is if it exists, so it should always exist
TravestyOfCode
TravestyOfCode13mo ago
Do you only need to get the Accommodations and Inventory and don't care about the Listings?
linqisnice
linqisnice13mo ago
well they belong to the listing, and it'st for resource ownership check as well
Accord
Accord13mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts
✅ How to deploy gRPC Server + Client via dockerI wish to have a gRPC server and multiple clients connected to said server. In my current setup, I h❔ How to "mute and resume" in RiderWhen I have an exception breakpoint for all CLR exceptions, when the debugger halts for an exception❔ Best way to set up a websocket and API to access the data?I'm working on a website that gathers data through a websocket on the backend, using raw UDP packets❔ Blazor WebassemblyTrying to put an image into a canvas so I can use coordinates to place images in different spots. Tr❔ Log into minecraft with xbox/microsoft apiI was wondering how to authenticate a Minecraft account with Microsofts new account system and if th❔ optimizationSincere question about optimization here... The RenderFirstTab() method is called in an infinite lo❔ How to instantiate derived class when base class requires args?Hey, I'll post the code below but I don't think it'll be necessary as I'm pretty sure I know what th❔ Not sure should I leave the automatic table alteration (postgres EF Core 3.0 to EF Core 6.0)You can see on the picture that EF Core has added some non intentional AlterColumn entries. I guess ❔ Writing VS Extension to Support Language with TextMate Grammar FileHello, I am trying to write extension to support BNF(Backus–Naur form) metalanguage files. I am try❔ Change trigger behavior from code behind(!)Hello again. I am still making a chess game using wpf&wcf. And the problem is that entire board is g