C#C
C#9mo 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>();


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


user controller:
    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
Was this page helpful?