C
C#15mo ago
S-IERRA

❔ EntityFramework Code cleanup

I have lots of controllers like this, is there anyway I could clean this up? for one I don't want to be calling the firstordefaultasync each time I have an ID parameter possibly there is somew ay to implicitly convert it while carrying context for performance sake? Also I was thinking of moving the logic to the Logic layer But I'm not sure how I'd do validation there and return it, for example. if a user has missing permissions
return Unauthorized();
return Unauthorized();
[HttpPost("{serverId:guid}/view-logs")]
public async Task<IActionResult> ViewLogs([FromClaim(ChatClaims.UserId)] Guid userId, Guid serverId)
{
await using var context = ContextFactory.CreateDbContext();

if (await context.Users.FirstOrDefaultAsync(x => x.Id == userId) is not { } user)
return Unauthorized();

if (await context.Servers.FirstOrDefaultAsync(x => x.Id == serverId) is not { } server)
return Unauthorized();

context.Servers.Remove(server);

return Ok();
}

[HttpPost("{serverId:guid}/create-invite")]
public async Task<IActionResult> CreateInvite([FromClaim(ChatClaims.UserId)] Guid userId, Guid serverId)
{
return Ok();
}
[HttpPost("{serverId:guid}/view-logs")]
public async Task<IActionResult> ViewLogs([FromClaim(ChatClaims.UserId)] Guid userId, Guid serverId)
{
await using var context = ContextFactory.CreateDbContext();

if (await context.Users.FirstOrDefaultAsync(x => x.Id == userId) is not { } user)
return Unauthorized();

if (await context.Servers.FirstOrDefaultAsync(x => x.Id == serverId) is not { } server)
return Unauthorized();

context.Servers.Remove(server);

return Ok();
}

[HttpPost("{serverId:guid}/create-invite")]
public async Task<IActionResult> CreateInvite([FromClaim(ChatClaims.UserId)] Guid userId, Guid serverId)
{
return Ok();
}
16 Replies
Omnissiah
Omnissiah15mo ago
why are you not injecting the context
S-IERRA
S-IERRAOP15mo ago
using a context factory instead
public class EntityFrameworkFactory : IDesignTimeDbContextFactory<EntityFrameworkContext>
public class EntityFrameworkFactory : IDesignTimeDbContextFactory<EntityFrameworkContext>
Angius
Angius15mo ago
Yeah but why
S-IERRA
S-IERRAOP15mo ago
absolutely no clue was told to do this have been doing it since any idea why injecting oculd be better?
Angius
Angius15mo ago
Why use more code when less code do trick?
S-IERRA
S-IERRAOP15mo ago
this is less code tho xd its maybe 3-5 line difference but still it makes no difference truly and really
Angius
Angius15mo ago
You could remove await using var context = ContextFactory.CreateDbContext();
S-IERRA
S-IERRAOP15mo ago
ye true
Jimmacle
Jimmacle15mo ago
>design time that's not intended to be used for your actual application
S-IERRA
S-IERRAOP15mo ago
what is it inteded for in that case?
Jimmacle
Jimmacle15mo ago
it's so EF tools can create a dbcontext assuming your normal app startup isn't available
S-IERRA
S-IERRAOP15mo ago
I see yeah that isnt ideal then I ll switch over to injected db context in that case Any clue what I could do regarding the if statements?
Saber
Saber15mo ago
create some action filter attribute or middleware that can check those type of things for you
S-IERRA
S-IERRAOP15mo ago
I see, wouldnt that be very inefficient though? Youd have to essentially check every possible route paramater well, every possible object that can be searched via id
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise 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