Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Factory Pattern with ASP.NET Core Dependency Injection

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?

like image 289
fosbie Avatar asked Jan 10 '19 11:01

fosbie


People also ask

Does dependency injection use Factory Pattern?

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.

What is factory design pattern in .NET Core?

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.

Does .NET Core have dependency injection?

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.

How does dependency injection work .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.


1 Answers

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); });  //... 
like image 91
Nkosi Avatar answered Sep 27 '22 21:09

Nkosi