Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 782
Secundus Praxi Avatar asked Oct 20 '25 14:10

Secundus Praxi


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();
like image 169
Kostarkis Avatar answered Oct 23 '25 09:10

Kostarkis