Search code examples
c#.net-coredependency-injection.net-8.0

Dependency Injection with multiple interface and partial class


I have a class which is common for all my applications. Initially I was having different class and different class files for each functionality like DBContext, SendEmail, DataCache. It work fine if I register each of them separately. But these are common for more than 40 applications. Then I thought to have one single partial class and multiple interfaces. The reason to have one single class is to have one single registration of the interface

This is my common interface. Other interfaces will have their own methods

public interface ICommon : IInterface1, IInterface2, Interface3
{
  public string M1();
  public string M2();
}
public interface IInterface1
{
  public string M3();
}

public interface IInterface2
{
  public string M4();
}

public interface IInterface3
{
  public string M5();
}

In my single class file

public partial class common : ICommon
{
   public string M1()
   {
   }
   
   public string M2()
   {
   }
}

then extension of this partial class as need to add other methods too from rest of the interfaces(1,2,3)

public partial class : ICommon
{
  public string M3()
   {
   }
}
public partial class : ICommon
{
  public string M4()
  {
  }
}
public partial class : ICommon
{
  public string M5()
  {
  }
}

Then in my application, in program.cs file, I am registering the interface common

builder.Services.AddScoped<ICommon,Common>();

But I am getting error as "System.AggregateException: 'Some services are not able to be constructed (Error while validating the service descriptor "

Any help would be appreciated


Solution

  • If you want to have a single line of DI config that sets up lots of your common DI, and use that across many API projects, then here's a suggestion.

    Have a Project which is your Common dependencies. Something like Api.CommonDependencies if that helps. In there, define a static extension method like this:

    public static class ServiceCollectionExtensionsForCommonApi
    {
        public static IServiceCollection AddCommonApiDependencies(this IServiceCollection services)
        {
            services.AddScoped<ISendEmail, MyEmailSender>();
            services.AddScoped<ICacheData, MyCacheImplementation>();
            services.AddScoped<IDBContext, MyDbContext();
            return services;
        }
    }
    

    And then in each of your 40 API projects, you can reference that Api.CommonDependencies project, and use them in your Startup or Program.cs (depending what type of API project they are).

    e.g. if it were a .NET 8 Isolated Functions API project:

    var host = new HostBuilder()
        .ConfigureFunctionsWorkerDefaults()
        .ConfigureServices(services =>
        {
            services
                .AddCommonApiDependencies() // your extension method
                .AddHttpClient() // or whatever else you need
    //etc
        })
        .Build();
    
    host.Run();