menes-dotnet/Menes

Extension method for adding the implementation and interface of a service

carmeleve opened this issue · 1 comments

public static IServiceCollection AddSingletonWithInterface<TInterface,TImplementation>(this IServiceCollection services)
  where TImplementation : TInterface
{
   services.AddSingleton<TImplementation>();
   services.AddSingleton<TInterface, TImplementation>(s => s.GetRequiredService<TImplementation>());
   return services;
}

(Something like that- it was code written in a github comment which is rarely exemplary :) )