Menu

One Endpoint Filter, Six Fewer Guard Clauses
📰

One Endpoint Filter, Six Fewer Guard Clauses

DEV Community: dotnet·Sukhpinder Singh·4 days ago
#LwLCt8yK
#devtodotnet#devto#dev#tenant#orders
Reading 0:00
15s threshold

I was adding a third route to a tenant-scoped group last week and caught myself typing six lines I'd already typed twice that afternoon. Read the X-Tenant-Id header, bail with a 400 if it's missing or obviously junk, otherwise carry on. Three handlers, three copies of the same guard. The fourth route I was about to add would've made four.

That's the kind of duplication that costs nothing until it costs a lot. The lines compile. The tests pass. Then some Tuesday a teammate adds a route to the group, doesn't notice the unwritten rule that every handler here checks the tenant first, and now one endpoint serves data with no tenant scope at all. Nothing throws. No red in the logs. It just quietly skips the check that every sibling route performs.

Here's roughly where my group had landed. I'll show two of the handlers; imagine the guard repeated across all of them.

var manual = app . MapGroup ( "/manual" ) ;

manual . MapGet ( "/orders" , ( HttpContext http ) => { if ( !…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.