I need the ASP.Net Core dependency injection to pass some parameters to the constructor of my GlobalRepository class which implements the ICardPaymentRepository interface.
The parameters are for configuration and come from the config file and the database, and I don't want my class to go and reference the database and config itself.
I think the factory pattern is the best way to do this but I can't figure out the best way to use a factory class which itself has dependencies on config and database.
My startup looks like this currently:
public class Startup { public IConfiguration _configuration { get; } public IHostingEnvironment _environment { get; } public Startup(IConfiguration configuration, IHostingEnvironment environment) { _configuration = configuration; _environment = environment; } public void ConfigureServices(IServiceCollection services) { services.AddScoped<IDbRepository, DbRepository>(); var connection = _configuration.GetConnectionString("DbConnection"); services.Configure<ConnectionStrings>(_configuration.GetSection("ConnectionStrings")); services.AddDbContext<DbContext>(options => options.UseSqlServer(connection)); services.AddScoped<ICardPaymentRepository, GlobalRepository>(); ... } public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IRFDbRepository rFDbRepository) { ... } }
The GlobalRepository constructor looks like this:
public GlobalRepository(string mode, string apiKey) { }
How do I now pass the mode from configuration and the apiKey from the DbRepository into the constructor from Startup?
Factory pattern can be called as a tool to implement DI. Dependency injection can be implemented in many ways like DI using constructors, using mapping xml files etc. That's true that Factory Pattern is one of the way implementing Dependency Injection.
The factory method design pattern is a creational design pattern, very common, and used in many software development projects. It is very similar to the Factory Design Pattern but in the Factory Method Design Pattern, you specify which type of object that you would like to return.
ASP.NET Core supports the dependency injection (DI) software design pattern, which is a technique for achieving Inversion of Control (IoC) between classes and their dependencies. For more information specific to dependency injection within MVC controllers, see Dependency injection into controllers in ASP.NET Core.
Dependency Injection is the design pattern that helps us to create an application which loosely coupled. This means that objects should only have those dependencies that are required to complete tasks.
Use the factory delegate overload when registering the repository
//... string mode = "get value from config"; services.AddScoped<ICardPaymentRepository, GlobalRepository>(sp => { IDbRepository repo = sp.GetRequiredService<IDbRepository>(); string apiKey = repo.GetApiKeyMethodHere(); return new GlobalRepository(mode, apiKey); }); //...
Alternative using ActivatorUtilities.CreateInstance
//... string mode = "get value from config"; services.AddScoped<ICardPaymentRepository>(sp => { IDbRepository repo = sp.GetRequiredService<IDbRepository>(); string apiKey = repo.GetApiKeyMethodHere(); return ActivatorUtilities.CreateInstance<GlobalRepository>(sp, mode, apiKey); }); //...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With