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

  • Home
  • SEARCH
  • 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 3634138
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:44:31+00:00 2026-05-19T00:44:31+00:00

I have an interface and I wish to do a factory pattern to instantiate

  • 0

I have an interface and I wish to do a factory pattern to instantiate different implementor of the interface. But I wish to keel the details of which implementor to be instantiated in the config file. I shall like to have it in the fashion of RoleProvider configuration:

<section name="MembershipProvider" type="MyOwn.UserManagement.Providers.MembershipProvider.CustomMembershipProviderConfigurationSection,MyOwn.UserManagement.Providers.MembershipProvider" allowDefinition="MachineToApplication" />

So its kind of an dependency injection technique. Can somebody help me?

Edit:

The actual situation is like this:

I have an interface IDataExchange and want to implement it with different assemblies for different customers. So I might have two assemblies that have classes implementing IDataExchange say, DataExchange1 and DataExchange2. Now when I deploy, depending upon to which customer I am providing it, I shall like to set the details of the assembly (either DataExchange1 or DataExchange2) in the config file. This will also allow me or any developer to write new assemblies implementing IDataExchange for any change required, if the instantiation decision is handled automatically.

So how can I do it?

My implementation:

Thanks to all of you. And special thanks to @Pauli Østerø

I have taken cue from your answers and implemented a solution.

In the config file I added the following:

<add key="Exchanger" value="DExchanger.DExchange1, DExchanger.DExchange1"/>

I added a class (to work as a DI container or the abstract factory). The class is DIContainer containing the following method:

public IDataExchange CreateInstance(string config)
{
    var type = Type.GetType(config);
    return (IDataExchange)Activator.CreateInstance(type);
}

And while I am instantiating, I write the following:

    var config = ConfigurationManager.AppSettings.Get("Exchanger");
    DIContainer x = new DIContainer();
    var instance = x.CreateInstance(config);

    Console.Write("Provide your input please: ");
    string inp=Console.ReadLine();
    Console.WriteLine(instance.DoDataExchange(inp));
    Console.ReadLine();

And this is giving me the result I was looking for.

I shall request all of you to put your feedback for further improvement.

  • 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-19T00:44:31+00:00Added an answer on May 19, 2026 at 12:44 am

    Add a property to your CustomMembershipProviderConfigurationSection named Type so you can configure it like this in your config file

    <group>
       <MembershipProvider type="... my concrete type" />
    </group>
    

    On your CustomMembershipProviderConfigurationSection you create a method named CreateInstance that returns IDataExchange. The body of the method is pretty simple

    public IDataExchange CreateInstance()
    {
       var type = Type.GetType(this.Type);
       return (IDataExchange)Activator.CreateInstance(type);
    }
    

    and when you need an IDataExchange reference you write

    var config = (CustomMembershipProviderConfigurationSection)ConfigurationManager.GetSection("group/MembershipProvider");
    var instance = config.CreateInstance();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wish I would have written the class/interface name down but I didn't... When
I have an interface contract that looks like this: ICollection<FooBar> FindByPredicate(Expression<Func<FooBar,bool>> predicate); ICollection<Foo> FindByPredicate(Expression<Func<Foo,bool>>
I wish to a check if a method exists in an interface based on
I have a class that already has a 'natural' order, and wish to define
I am trying to create a pluggable ASP.NET MVC framework. I have extensively used
I wish I could return an object of virtual base class so that I
I have following situation in a C# Windows Forms (.NET 4.0) application: I have
We have just found we are getting framing errors (as reported by the WCF
In one of my current projects I have a dropdown (select) form element. I
Using iPhone SDK 3.0, I wish to allow text entry with (optional) completion options

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.