0

I'm new to use Dependency Injection. When I use Dependency Injection to build my database helper to connect the database. I inject the parameter in Program.cs. Just like this:

builder.Services.AddSingleton<IElasticSearchHelper, ElasticSearchHelper>(x =>
    new ElasticSearchHelper(url,user, password));

And I want to make another helper to connect another database to do something different. If I don't use injection. I will do like this.

ElasticSearchHelper eshelper_1 = new ElasticSearchHelper(url_1,user, password);

ElasticSearchHelper eshelper_2 = new ElasticSearchHelper(url_2,user, password);

But I'm not sure how to use Dependency Injection to make like this.

And I think how to solve this. Maybe I create another implementation to inject.

builder.Services.AddSingleton<IElasticSearchHelper, ElasticSearchHelper>(x =>
    new ElasticSearchHelper(url_1,user, password));
builder.Services.AddSingleton<IElasticSearchHelper, ElasticSearchHelper_01>(x =>
    new ElasticSearchHelper_01(url_2,user, password));

or maybe I should create an IElasticSearchFactoryHelper to include multiple ElasticSearchHelper instances?

Steven
  • 166,672
  • 24
  • 332
  • 435
  • _"And I want to make another helper to connect another database to do something different"_ - would not it make sense to us another interface here? – Guru Stron Mar 17 '23 at 06:44
  • You say I make another interface to implement ? – Savitar Mar 17 '23 at 06:49
  • Yes, for example. It depends on what you mean by "something different" – Guru Stron Mar 17 '23 at 06:50
  • I'm sorry. "something different" mean I use this ElasticSearchHelper to Insert data to this database.But also I have to Get some data from another database. – Savitar Mar 17 '23 at 06:54
  • But their Insert or Get function.I wrote them all in ElasticSearchHelper – Savitar Mar 17 '23 at 06:56
  • Based on this description I would split them in two interface-implementation pairs (possibly with some base parts shared). But without seeing actual code it is hard to tell. – Guru Stron Mar 17 '23 at 07:03
  • Wait. um You mean I make two Interface But use one Implement? – Savitar Mar 17 '23 at 07:05
  • No, I mean two interfaces with each one having it's own implemenation. – Guru Stron Mar 17 '23 at 07:10
  • In one of my requests, I need to deal with databases placed in two different addresses, so I need to create two Helpers. In one request, the first database adds a new data and the second database also needs to add a new data. The first database is used to process orders, while the second database is used to store logs. But for me, they all have the same logic, but they are added to different Database with different tables – Savitar Mar 17 '23 at 07:13
  • I would say that while their logic **seems** to be the same - from business standpoint it looks to be very different and personally I would even make them having different contracts working with different types (i.e. something like `LogRecord` for logging one and `Order` for the other) – Guru Stron Mar 17 '23 at 07:16
  • Based on what you said, I should be doing my own interface and implementation for two businesses. I think I should have misunderstood the use of dependency injection – Savitar Mar 17 '23 at 07:35
  • I would not say that this is about DI misunderstanding per se, but about design approaches in general. Again maybe your approach is a valid one, without more context it is hard to tell, but from what I have seen for now - using different interfaces seems to be a way to go. – Guru Stron Mar 17 '23 at 07:42
  • At last,I make a IElasticSearchClientFactoryHelper that I Inject the paramters of List. "builder.Services.AddSingleton(x => new ElasticSearchClientFactory(elasticConfigs));" like this.And I can have one implements for two different client – Savitar Mar 20 '23 at 08:31

1 Answers1

2

When i understand you correctly you just want to have 2 instances (with different paramters) at the same time. This is quite easy to achieve with DI Frameworks which allow registration by name (Autofaq for examle => See here).

But with a little resolver you can even do it with the MS DI Framework. Here is an example => Resolve By Name (MS)

Viper
  • 2,216
  • 1
  • 21
  • 41