Some (hopefully helpful) extensions for IServiceCollection
$ dotnet add package LSL.ServiceCollectionsA library providing various extensions to IServiceCollection
At present it contains:
TryAdd for all lifetimes (all methods return the original IServiceCollection for further chaining)More in-depth documentation can be found here
var services = new ServiceCollection()
.FluentlyTryAdd(s => s
.Singleton<TestClass>()
// This will not be done as we have already
// added it
.Singleton<TestClass>()
// This will be added
.Singleton<ITestClass, TestClass>()
// This will not be done as we have already
// added it
.Singleton<ITestClass, TestClass>()
// This will be added
.Transient<TestClass2>()
// This will not be done as we have already
// added it (as a transient)
.Scoped<TestClass2>()
// This will not be done as we have already
// added it
.Descriptor(ServiceDescriptor.Singleton(
typeof(ITestClass),
typeof(TestClass)))
);
// services will only have:
// `TestClass`, `TestClass2` and `ITestClass` registered