C
C#10mo 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
not guilty
not guilty10mo ago
why are you not injecting the context
S-IERRA
S-IERRA10mo ago
using a context factory instead
public class EntityFrameworkFactory : IDesignTimeDbContextFactory<EntityFrameworkContext>
public class EntityFrameworkFactory : IDesignTimeDbContextFactory<EntityFrameworkContext>
Angius
Angius10mo ago
Yeah but why
S-IERRA
S-IERRA10mo ago
absolutely no clue was told to do this have been doing it since any idea why injecting oculd be better?
Angius
Angius10mo ago
Why use more code when less code do trick?
S-IERRA
S-IERRA10mo ago
this is less code tho xd its maybe 3-5 line difference but still it makes no difference truly and really
Angius
Angius10mo ago
You could remove await using var context = ContextFactory.CreateDbContext();
S-IERRA
S-IERRA10mo ago
ye true
Jimmacle
Jimmacle10mo ago
>design time that's not intended to be used for your actual application
S-IERRA
S-IERRA10mo ago
what is it inteded for in that case?
Jimmacle
Jimmacle10mo ago
it's so EF tools can create a dbcontext assuming your normal app startup isn't available
S-IERRA
S-IERRA10mo 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
Saber10mo ago
create some action filter attribute or middleware that can check those type of things for you
S-IERRA
S-IERRA10mo 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
Accord10mo 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.