How ryan-h/dependencyinjection.servicefactory works
Project type and purpose
• This is a .NET Core solution containing:
• A class‐library “App.Extensions.DependencyInjection.ServiceFactory” providing an IServiceFactory<T> abstraction on top of Microsoft.Extensions.DependencyInjection.
• An “Example” ASP.NET-style console/web host demonstrating how to wire up and use the factory extension to select at runtime between different repository implementations (Cosmos, Couchbase) via configuration.
File structure
• Top‐level projects:
• App.Extensions.DependencyInjection.ServiceFactory (the DI‐factory library)
• Example (the demo application)
• Library project files:
• IServiceFactory.cs – factory interface
• ServiceCollectionExtensions.cs – AddFactory<TService, TFactory> extension
• Example project files:
• Models/RepositoryOptions.cs – options pattern model bound from appsettings.json
• Services/Interfaces – IExampleService, IExampleRepository
• Services/Factories – ExampleRepositoryFactory : IServiceFactory<IExampleRepository>
• Services/Repositories – CosmosExampleRepository, CouchbaseExampleRepository
• ExampleService – business‐logic service depending on IExampleRepository
• Startup.cs & Program.cs – host setup, configuration binding, DI registrations
Main components & their relationships
a. Configuration subsystem
• appsettings.json → bound to RepositoryOptions via IOptions<RepositoryOptions>
b. DI Container (Microsoft.Extensions.DependencyInjection)
• Registers ServiceFactory library’s AddFactory<T,TF>()
• Registers ExampleRepositoryFactory as factory for IExampleRepository
• Registers ExampleService as consumer of IExampleRepository
c. Service Factory layer
• IServiceFactory<IExampleRepository>.Create(provider) reads RepositoryOptions to decide which repository implementation to instantiate
d. Domain Service layer
• ExampleService calls IExampleRepository methods
e. Repository implementations
• CosmosExampleRepository → interacts with Cosmos DB
• CouchbaseExampleRepository → interacts with Couchbase cluster
f. External resources
• Cosmos DB endpoint
• Couchbase cluster