Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

212
Views
.NET Core Dependency Injection how to handle multiple objects

As the title says I have a .NET Core application that I am trying to convert over to and take advantage of the built in Microsoft Dependency Injection.

I have an object and a base class for the object, call it CommunicationBase and Communicator. When my app starts up and reads the configuration file, I can have N number of objects to instantiate.

Previously, before switching to Dependency Injection, somewhere in my startup routine, where I read the configuration file, I would have a List<CommunicationBase> variable that I would instantiate and add Communicator objects to and at the same time, set some of the base properties, which changed based on how many were in my configuration and each ones properties in config.

How would I achieve this with DI?

I understand that in my services, I would register the type so it can be injected into other class constructors.

For example, services.AddTransient<CommunicationBase, Communicator>(); but as I understand it, this just registers the types with DI. I can inject it into a class and have a random instance of one of them.

How would I then have N number of instances and be able to set properties of each one as I create the instance?

Or, is this a scenario where DI is not necessary or won't work and I need to just do it the way I was doing it before?

Thanks!

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

I would slightly modify approach shown here. So I would define some enum that would then be used to decide what instance to return.

Sample classes setup and the enum:

public enum CommuniationType
{
    False, True, Other,
}

public abstract class CommunicationBase
{
    public CommunicationBase(CommuniationType communiationType)
    {
        CommuniationType = communiationType;
    }

    public bool IsConnected { get; set; }
    
    public CommuniationType CommuniationType { get; protected set; }
}

public class Communicator : CommunicationBase
{
    public Communicator(CommuniationType communiationType) : base(communiationType) { }
}

Now, in the place where you have access to service collection (e.g. in ASP.NET the place would be Stratup.RegisterServices method) you define your objects of concrete class and register them, as in the sample code below (at the bottom, there are also test classes using CommunicationBase object for testing puproses):

public class Program
{
    static void Main(string[] args)
    {
        var serviceCollection = new ServiceCollection();
        
        SetupNObjects(serviceCollection);

        serviceCollection.AddTransient<CommunicationBaseServiceResolver>(serviceProvider => communicationType =>
        {
            var implementations = serviceProvider.GetServices<CommunicationBase>();
            return implementations.First(x => x.CommuniationType == communicationType);
        });

        serviceCollection.AddScoped<FalseTestClass>();
        serviceCollection.AddScoped<TrueTestClass>();

        var serviceProvider = serviceCollection.BuildServiceProvider();

        var f = serviceProvider.GetService<FalseTestClass>();
        var t = serviceProvider.GetService<TrueTestClass>();
    }
    
    // Here you should take care of registering objects, after reading config.
    // That would be best place to do that.
    static void SetupNObjects(ServiceCollection serviceCollection)
    {
        var comFalse = new Communicator(CommuniationType.False);
        comFalse.IsConnected = false;

        var comTrue = new Communicator(CommuniationType.True);
        comTrue.IsConnected = true;

        serviceCollection.AddScoped<CommunicationBase>((serviceProvider) => comFalse);
        serviceCollection.AddScoped<CommunicationBase>((serviceProvider) => comTrue);
    }
}

public class FalseTestClass
{
    private readonly CommunicationBase communication;

    public FalseTestClass(CommunicationBaseServiceResolver resolver)
    {
        communication = resolver(CommuniationType.False);
    }
}

public class TrueTestClass
{
    private readonly CommunicationBase communication;
    
    public TrueTestClass(CommunicationBaseServiceResolver resolver)
    {
        communication = resolver(CommuniationType.True);
    }
}
over 4 years ago · Santiago Trujillo Report

0

Firstly do you need to has clear the differences between Transient, Scoped, Singleton lifetime. To understand how works with the list of Communicator objects that will be read from your configuration file.

One approuch to resolve your question is

  1. Create an interface ICommunicatorList with one method to get a List, i mean you can envolve the list of communicators.
  2. Create a clase that inherits from ICommunicatorList (for example called CommunicatorList), with a private field for your list of Communicators. On the constructor method set your private field with the list of communicator, o here you can receive like a parameter from the section of the config file to iterate and full your private field.
  3. on this class implement your code to return the list of communicators.
  4. Now, in your startups file you can now create the service services.AddTransient< ICommunicatorList>(x => new CommunicatorList(parameters));
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!