2

How can I get Method Under One interface like this:

My service classes:

Module A

public class CollectorA : ICollector
{
    public string CollectSomething()
    {
        //Do something
    }
}

Module B

public class CollectorB : ICollector
{
    public string CollectSomething()
    {
        //Do something
    }
}

Module C

public class CollectorC : ICollector
{
    public string CollectSomething()
    {
        //Do something
    }
}

My ICollector interface:

public interface ICollector
{
    string CollectSomething();
}

My IModuleInitializer:

public interface IModuleInitializer
{
    void Init(IServiceCollection serviceCollection);
}

Under Every Service I have ModuleInitializer like this

public void Init(IServiceCollection serviceCollection)
{
    serviceCollection.AddTransient<ICollector, CollectorA>();
    serviceCollection.AddTransient<ICollector, CollectorB>();
    serviceCollection.AddTransient<ICollector, CollectorC>();
}

How can I access for example in win console application this methods over this interface ?

My main class :

static void Main(string[] args)
{
    //Get String Values each module and show on console ..
}
5
  • After the service provider has been built you can use serviceProvider.GetServices<ICollector>() extension method. Note it is GetServices (plural). It would return IEnumerable<ICollector> containing all the registered implementations. Commented Jul 4, 2017 at 11:22
  • Can you share some code here ? @Nkosi Commented Jul 4, 2017 at 11:44
  • I made an assumption about what you wanted but your question is still a bit unclear. What are you trying to do exactly? Commented Jul 4, 2017 at 12:09
  • For example my methodA doing sum of two number (2+4),methodB doing (3+5) ... I want call all of them in main application Commented Jul 4, 2017 at 12:15
  • I think the better implementation is to use the Factory Pattern or Strategy pattern no ? Commented Jul 4, 2017 at 13:09

1 Answer 1

1

Assuming the following implementation

pubic class ModuleInitializer : IModuleInitializer{
    public void Init(IServiceCollection serviceCollection) {
        serviceCollection.AddTransient<ICollector, CollectorA>();
        serviceCollection.AddTransient<ICollector, CollectorB>();
        serviceCollection.AddTransient<ICollector, CollectorC>();
    }
}

and the other classes in the original question.

After the service provider has been built you can use serviceProvider.GetServices<ICollector>() extension method. Note it is GetServices (plural). It would return IEnumerable<ICollector> containing all the registered implementations.

The following example shows how to use the DI framework

static void Main(string[] args) {
    var services = new ServiceCollection();
    var moduleInitializer = new ModuleInitializer();
    moduleInitializer.Init(services);
    IServiceProvider serviceProvider = services.BuildServiceProvider();

    //Get String Values each module and show on console ..
    var collectors = serviceProvider.GetServices<ICollector>();
    foreach(ICollector collector in collectors) {
        Console.WriteLine(collector.CollectSomething());
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

One question more :) All working but i m trying Add.Transient<...,...>(); automatically . In short i dont want use addTransient every module implementation. Any help thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.