How to bundle services and repository in ASP.NET Core 6?

Viewed 27

I haved created a simple ASP.NET Core 6 mvc app. Then Here's my Program.cs

enter image description here

I would like to know if ever I have to many services and repositories, can I bundle service and repo and call it here?

1 Answers

Yes, to do so, you need to have a new class called for example AddRepositories or AddServices which will be looking something like that:

public static IServiceCollection AddRepositories(this IServiceCollection services)
    {
        services.AddScoped<IClientRepository, ClientRepository>();
        services.AddScoped<IProductRepository, ProductRepository>();
        return services;
    }

then in Program.cs you need to attach that bundle by adding this line:

builder.Services.AddRepositories();
Related