Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6073219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:14:50+00:00 2026-05-23T10:14:50+00:00

I register the Service & Provider with some IoC tool(e.g. StructreMap) public interface IProvider

  • 0

I register the Service & Provider with some IoC tool(e.g. StructreMap)

public interface IProvider
{
    void Load(int id);
}
public class Provider : IProvider
{
     public void Load(int id) {...} 
}
public interface IService
{
    void Load(int id);
}
public class Service : IService
{
    public Service(IProvider provider) { }
    public void Load(int id) { provider.Load(id); }
}

Now I have another provider

public class Provider2 : IProvider
{
    public void Load(int id) {...}
}

I want to register that provider(Provider2) and to get the instance of Service with Provider2 from IoC tool. The only way I can see to do this is to create Service2 that will inherit from IService2 and the last will inherit from IService. And now I can get instance of type IService2 that will initiate provider with Provider2.

public interface IService2 : IService
{ }
public class Service2 : IService2
{
    public Service2(IProvider2 provider) { }
    public void Load(int id) { provider.Load(id); }
}

My question is: for every new Provider I need to write new class for Service even if the functionality of the service should remain the same?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:14:50+00:00Added an answer on May 23, 2026 at 10:14 am

    No, you don’t need to do that.

    I would advice you to implement an abstract factory pattern for selecting the right implementation of provider depending on the value know at runtime.

    For an example please look here : http://alsagile.com/archive/2010/06/28/Abstract-Factory-and-IoC-Container-in-asp-net-MVC.aspx.

    It’s in MVC context but can be easily implemented in other scenarios.

    Example :

    Youd could register your service like this (pseudo code) :

    x.For<IService>().Use<Service>();    
    x.For<IProvider>().Use<Provider1>().Named("provider1");
    x.For<IProvider>().Use<Provider2>().Named("provider2");
    

    You need a way to inject the container in your Abstract factory. You could wrapp it for example like this:

    public interface IProviderFactory
    {    
        object GetInstance<IProvider>();    
        object GetNamedInstance<IProvider>(string key);
    }
    

    And the implementation :

    public class ProviderFactory : IProviderFactory
    {     
        private readonly Container _container     
        public ProviderFactory (Container container)     
        {          
            _container = container;     
        }     
        ...
    }
    

    And setup StructureMap to resolve dependencies to your abstract factory like that :

    x.For<IProviderFactory>.Use(new ProviderFactory(this));
    

    Then into your Service you should inject the provider factory like this

    public class Service : IService
    {
        private readonly IProviderFactory _providerFactory;
    
        public Service(IProviderFactory factory)
        {
            if (factory == null)
                throw new ArgumentNullException("factory", "providerfactory cannot be null");
            _proiderFactory = factory;
        }
    
        public void Load(string providerName)
        {
          var provider = _providerFactory.GetNamedInstance(providerName);
          // do some operation on provider
        }
    }
    

    This should work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: import com.apple.dnssd.*; public interface IServiceAnnouncer { public void registerService();
I have a custom windows service with my own register handler, how can I
I want to make a WCF timer service where clients can register in order
I have a new service that I have written and need to register it
I'm trying to write a library that will register an arbitrary list of service
int* Register = 0x00FF0000; // Address of micro-seconds timer while(*Register != 0); Should I
Is it possible to register a service at run-time, meaning after the ContainerBuilder has
I am trying to register a service on the phone container that uses generics.
I'm tryng to make my Android app register a service via zeroconf while the
Sometimes when i try to register for C2DM, I get SERVICE_NOT_AVAILABLE error. I used

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.