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 8930471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:54:02+00:00 2026-06-15T08:54:02+00:00

Because I’m relative new to OOP / C# i don’t know the right pattern

  • 0

Because I’m relative new to OOP / C# i don’t know the right pattern to solve this:

I have to build a plugin architecture for different IO Providers.
The host reads the needed Provider name / type from the config, then it should the provider instantiate and parameterize.

So I have basically this interfaces:

public interface IoProvider //(Base interface, all Providers implements this)
{
    void Initialize();
    void Execute();
}

public interface IFileProvider: IoProvider
{
    string PropertyA { get; set; }
}

public interface ISmtpProvider : IoProvider
{
    string PropertyB { get; set; }
    string PropertyC { get; set; }
}

As you see the derived, specialized IO Providers have different additional parameter properties which the base interface doesn’t have.
To avoid if/then/else or switch statements my idea was to use a factory pattern.

But if i understand this correctly it doesn’t solve my if/then/else problem because on the client I have to check the derived type to supply the correct parameters.

So the program flow on the Host would be something like this:
Host reads config, gets Name/Type of needed Provider
Host calls Factory and gets the Provider

But how avoid this – is there a pattern to solve this without if/then/else?

If (provider == typeOf(IFileProvider))  
PropertyA = value  
else if (provider == typeOf(ISmtpProvider))  
PropertyB = value  
PropertyC = value  
Elseif …
  • 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-06-15T08:54:03+00:00Added an answer on June 15, 2026 at 8:54 am

    You can replace switch statement with polymorphism. Just allow each provider to configure itself from config. This is a best option, because each provider knows which values to look for:

    provider.Configure();
    

    This method should exist in base interface:

    public interface IoProvider 
    {
        void Initialize();
        void Execute();
        void Configure();
    }
    

    And each provider implement it:

    public interface ISmtpProvider : IoProvider
    {
        string PropertyB { get; set; }
        string PropertyC { get; set; }
    
        public void Configure()
        {
            PropertyB = ConfigurationManager.AppSettins["B"];
            PropertyB = ConfigurationManager.AppSettins["C"];
        }
    }
    

    Main benefit of this approach is that you will have only one place to change, when new provider added to your application – just add new provider class, which knows how to configure itself. You don’t need to change host implementation also. And your host will satisfy OCP principle – open for extension (you can add new providers), but closed for modification (you don’t need to modify existing code when new provider added).

    Also you can pass some configuration object to Configure(IConfiguration config) to this method (it will make your code testable, and not dependent on static ConfigurationManager).

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

Sidebar

Related Questions

I have this code: imgImage.Source = new BitmapImage(new Uri(Images/LivingRoom/chair.png, UriKind.Relative)); this gives me: {pack://application:,,,/ExpressFurnitureSystem;component/ui/Images/LivingRoom/chair.png}
i have a container div which is position:relative and the squares are position:absolute because
modify this question again, because the solution does not work in the new version
I have a relative URI: Uri U = new Uri(../Services/Authenticated/VariationsService.svc, UriKind.Relative); The problem is
I have another question kind of relative to this but I'd like to separate
Please help me solve this file path conflict: As you know, many HTML pages
Because data from file look like this: line 1 is name (first last), next
Because this is my first attempt at an extension method that seems quite useful
I am absolutely new to javascript, so please bear with me. I have 50
I am pretty new at this stuff, so bear with me. I am using

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.