C
C#•2w ago
Bordin

.NET Api Setup

Hello all, I just need advice in the setup of my .net project. I was making it using the typical Unit of work (Repository) architecture where you have a unit of work & services that call unit of work which has a bunch of classes that are repositories. Anyway, a week ago, someone here advised that because i am using EntityFramework, Unit of work is a bad practice, or not recommended. So I decided to get rid of unit of work & services. And inject the IRepository class in my apis. Program.cs:
builder.Services.AddTransient<IUserRepository, UserRepository>();
builder.Services.AddTransient<IUserRepository, UserRepository>();
And this is the repository, i am not declaring dbcontext, it's injected as well
public class UserRepository(IHttpContextAccessor httpContextAccessor, OnlineShopContext myContext) : Repository<User>(httpContextAccessor, myContext), IUserRepository
public class UserRepository(IHttpContextAccessor httpContextAccessor, OnlineShopContext myContext) : Repository<User>(httpContextAccessor, myContext), IUserRepository
user controller:
public class UserController(
IUserRepository userRepository,
IHttpContextAccessor httpContextAccessor,
IConfiguration configuration) : Helper(httpContextAccessor, configuration)
public class UserController(
IUserRepository userRepository,
IHttpContextAccessor httpContextAccessor,
IConfiguration configuration) : Helper(httpContextAccessor, configuration)
This works fine but Question is, in unit of work, i had to dispose the context do i need to do that here
48 Replies
Unknown User
Unknown User•2w ago
Message Not Public
Sign In & Join Server To View
Angius
Angius•2w ago
The DI container handles the lifetime of injected services automatically
Unknown User
Unknown User•2w ago
Message Not Public
Sign In & Join Server To View
Bordin
BordinOP•2w ago
UserRepository.cs:
public class UserRepository(IHttpContextAccessor httpContextAccessor, OnlineShopContext myContext) : Repository<User>(httpContextAccessor, myContext), IUserRepository
{
public async Task<User?> GetUserById(int id)
{
return await myContext.Users.Where(u => u.Id == id).FirstOrDefaultAsync() ;
}

public async Task<User?> GetUserByEmail(string email)
{
try
{
return await myContext.Users.Where(u => u.Email == email).FirstOrDefaultAsync();

}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
}
}
public class UserRepository(IHttpContextAccessor httpContextAccessor, OnlineShopContext myContext) : Repository<User>(httpContextAccessor, myContext), IUserRepository
{
public async Task<User?> GetUserById(int id)
{
return await myContext.Users.Where(u => u.Id == id).FirstOrDefaultAsync() ;
}

public async Task<User?> GetUserByEmail(string email)
{
try
{
return await myContext.Users.Where(u => u.Email == email).FirstOrDefaultAsync();

}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
}
}
UserController.cs:
[Route("api/[controller]")]
[ApiController]
public class UserController(
IUserRepository userRepository,
IHttpContextAccessor httpContextAccessor,
IConfiguration configuration) : Helper(httpContextAccessor, configuration)
{
[HttpPost("Signup")]
public async Task<ActionResult> Signup(Signup loginResource)
{
some stuff
}
[HttpPost("SignIn")]
``public async Task<ActionResult> Signup(LoginResource loginResource){
var user = await userRepository.GetUserByEmail(loginResource.Email!);
some other stuff
}
}
[Route("api/[controller]")]
[ApiController]
public class UserController(
IUserRepository userRepository,
IHttpContextAccessor httpContextAccessor,
IConfiguration configuration) : Helper(httpContextAccessor, configuration)
{
[HttpPost("Signup")]
public async Task<ActionResult> Signup(Signup loginResource)
{
some stuff
}
[HttpPost("SignIn")]
``public async Task<ActionResult> Signup(LoginResource loginResource){
var user = await userRepository.GetUserByEmail(loginResource.Email!);
some other stuff
}
}
public async Task UpdateAsync(TEntity entity)
{
Context.Set<TEntity>().Update(entity);
await Context.SaveChangesAsync();
}
public async Task UpdateAsync(TEntity entity)
{
Context.Set<TEntity>().Update(entity);
await Context.SaveChangesAsync();
}
i should not use stuff like this? what do you mean I already have Context
Unknown User
Unknown User•2w ago
Message Not Public
Sign In & Join Server To View
Bordin
BordinOP•2w ago
I am passing it to Helper which gets me the UserId claim of the token
public Helper(IHttpContextAccessor httpContextAccessor)
{
try
{
var tokenForUser = httpContextAccessor.HttpContext!.User;
IpAddress = httpContextAccessor.HttpContext.Connection.RemoteIpAddress!.ToString();
if (tokenForUser.Identity?.IsAuthenticated == false)
{
return;
}
UserId = Convert.ToInt32(tokenForUser.Claims.FirstOrDefault(c => c.Type == "UserId")!.Value);
}
catch (Exception)
{
// ignored
}
}
public Helper(IHttpContextAccessor httpContextAccessor)
{
try
{
var tokenForUser = httpContextAccessor.HttpContext!.User;
IpAddress = httpContextAccessor.HttpContext.Connection.RemoteIpAddress!.ToString();
if (tokenForUser.Identity?.IsAuthenticated == false)
{
return;
}
UserId = Convert.ToInt32(tokenForUser.Claims.FirstOrDefault(c => c.Type == "UserId")!.Value);
}
catch (Exception)
{
// ignored
}
}
also does other stuff
Angius
Angius•2w ago
That's where extension methods are useful User is already in the controller too, btw
Unknown User
Unknown User•2w ago
Message Not Public
Sign In & Join Server To View
Bordin
BordinOP•2w ago
yeah exactly
Unknown User
Unknown User•2w ago
Message Not Public
Sign In & Join Server To View
Bordin
BordinOP•2w ago
yeah
Unknown User
Unknown User•2w ago
Message Not Public
Sign In & Join Server To View
Bordin
BordinOP•2w ago
Yeah
Unknown User
Unknown User•2w ago
Message Not Public
Sign In & Join Server To View
Bordin
BordinOP•2w ago
huh hang on leme tr
Unknown User
Unknown User•2w ago
Message Not Public
Sign In & Join Server To View
Bordin
BordinOP•2w ago
holy shit
Unknown User
Unknown User•2w ago
Message Not Public
Sign In & Join Server To View
Bordin
BordinOP•2w ago
I am mind blown rn
Unknown User
Unknown User•2w ago
Message Not Public
Sign In & Join Server To View
Bordin
BordinOP•2w ago
i have no idea
Unknown User
Unknown User•2w ago
Message Not Public
Sign In & Join Server To View
Bordin
BordinOP•2w ago
yeah it checks automatically
Unknown User
Unknown User•2w ago
Message Not Public
Sign In & Join Server To View
Bordin
BordinOP•2w ago
yeah
Unknown User
Unknown User•2w ago
Message Not Public
Sign In & Join Server To View
Bordin
BordinOP•2w ago
what?
Unknown User
Unknown User•2w ago
Message Not Public
Sign In & Join Server To View
Bordin
BordinOP•2w ago
//Authorization
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>
{

options.SaveToken = true;

options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = false,
ValidateAudience = false,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = builder.Configuration["JwtAuthentication:JwtIssuer"],
ValidAudience = builder.Configuration["JwtAuthentication:JwtAudience"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["JwtAuthentication:JwtKey"]!))
};
});
//Authorization
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>
{

options.SaveToken = true;

options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = false,
ValidateAudience = false,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = builder.Configuration["JwtAuthentication:JwtIssuer"],
ValidAudience = builder.Configuration["JwtAuthentication:JwtAudience"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["JwtAuthentication:JwtKey"]!))
};
});
this is what I ddi did* yeah, I didn't try it yet i am still setting up the project. But it should work fine I have a question concerning the setup Should I get rid of repository and use extension methods?
Unknown User
Unknown User•2w ago
Message Not Public
Sign In & Join Server To View
Bordin
BordinOP•2w ago
okay I still don't have any controllers to apply that still setting up the project
Unknown User
Unknown User•2w ago
Message Not Public
Sign In & Join Server To View
Bordin
BordinOP•2w ago
No not yet, ik how to use it ?
Unknown User
Unknown User•2w ago
Message Not Public
Sign In & Join Server To View
Bordin
BordinOP•2w ago
but userrepo is accessing data its on the data layer right? so it's a repo not a service
Unknown User
Unknown User•2w ago
Message Not Public
Sign In & Join Server To View
Bordin
BordinOP•2w ago
that's what someone else told me
Unknown User
Unknown User•2w ago
Message Not Public
Sign In & Join Server To View
Bordin
BordinOP•2w ago
orm?
Unknown User
Unknown User•2w ago
Message Not Public
Sign In & Join Server To View
Bordin
BordinOP•2w ago
aha
Unknown User
Unknown User•2w ago
Message Not Public
Sign In & Join Server To View
Bordin
BordinOP•2w ago
sooo i see i see
Unknown User
Unknown User•2w ago
Message Not Public
Sign In & Join Server To View
Bordin
BordinOP•2w ago
what about the extension methods? aren;t they the same as what i am doing? They seemed similar, almost the same, just more annoying
Unknown User
Unknown User•2w ago
Message Not Public
Sign In & Join Server To View
Bordin
BordinOP•2w ago
ah the code isn't mine i am working with someone and he has the project on private. I am very sorry 😦 It's fine if you need to go, Thank you for sharing your knowledge today. I am grateful
Unknown User
Unknown User•2w ago
Message Not Public
Sign In & Join Server To View

Did you find this page helpful?