[SOLVED] Optimal way of declaring DI objects

Issue

I’ve got a .net API and my Program.cs has started to become quite bloated with the following:

.
.
.
//core mappers
builder.Services.AddSingleton<IAppData_Mapper, AppData_Mapper>();
builder.Services.AddSingleton<IUserData_Mapper, UserData_Mapper>();

//pylon export services
builder.Services.AddTransient<IPylonItemExportService, PylonItemExportService>();
builder.Services.AddTransient<IPylonContactExportService, PylonContactExportService>();

//pylon repositories
builder.Services.AddSingleton<IPylonContactRepository, PylonContactRepository>();
builder.Services.AddSingleton<IPylonConnectionRepository, PylonConnectionRepository>();
builder.Services.AddSingleton<IPylonItemRepository, PylonItemRepository>();
builder.Services.AddSingleton<IPylonContactExportRepository, PylonContactExportRepository>();
builder.Services.AddSingleton<IPylonItemExportRepository, PylonItemExportRepository>();
builder.Services.AddSingleton<IPylonDocumentRepository, PylonDocumentRepository>();
builder.Services.AddSingleton<IPylonCentLineRepository, PylonCentLineRepository>();
builder.Services.AddSingleton<IPylonComEntryRepository, PylonComEntryRepository>();
builder.Services.AddSingleton<IPylonSupplierRepository, PylonSupplierRepository>();
builder.Services.AddSingleton<IPylonCustomerRepository, PylonCustomerRepository>();

//pylon sync services
builder.Services.AddTransient<IPylonItemSyncService, PylonItemSyncService>();
builder.Services.AddTransient<IPylonContactSyncService, PylonContactSyncService>();
builder.Services.AddTransient<IPylonMeasurementunitSyncService, PylonMeasurementunitSyncService>();
builder.Services.AddTransient<IPylonDocEntrySyncService, PylonDocEntrySyncService>();
builder.Services.AddTransient<IPylonCentLineSyncService, PylonCentLineSyncService>();
builder.Services.AddTransient<IPylonCommEntrySyncService, PylonCommEntrySyncService>();
builder.Services.AddTransient<IPylonCustomerSyncService, PylonCustomerSyncService>();
builder.Services.AddTransient<IPylonSupplierSyncService, PylonSupplierSyncService>();
.
.
.

What would be the optimal way to move these outside of my program.cs? Ideally i’d like for them to be in a seperate class that will be called via the Program.cs

  • Can I pass the builder to a static method that will add the services?
  • Can I create the array of services and do something like Services.AddRange?

I’m fairly new to the DI architecture on .net and any guidelines would be grately appreciated!

Solution

To answer your first question you can move the service registration to another class by creating a new class and having the method take in the builder and register services then return or create an extension method. Creating an extension method is slightly "neater" and you can do it like so.

public static class MyServiceExtension
{
    public static IServiceCollection AddMyServices(this IServiceCollection services)
    {
        // add your services


        return services;
    }
}

Now in your Program.cs all you’ll do is.

builder.Services.AddMyServices();

Answered By – John Nyingi

Answer Checked By – Candace Johnson (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *