The Provider Model

Ideally when you have a bunch of options (to execute an operation) , and want a loose coupling on how exactly to execute, this would be good pattern.

So in a configuration file you want to dynamically have 1) your options 2) Load current choice of execution. One class keeps the collection and default strategy. Provider class may alredy has a filewatcher alert on the configuration file.

I have two ways of travelling

    public abstract class ITravelProvidor : ProviderBase
    {
        public abstract void goSomewhere2(string dest);
    }
    public class GoByCar : ITravelProvidor
    {
        private string mystring = "By car ";
        public override void goSomewhere2(string dest)
        {
            string h = mystring;
        }
    }
    public class GoByCab : ITravelProvidor
    {
        private string mystring = "By cab ";
        public override void goSomewhere2(string dest)
        {
            string h = mystring;
        }
    }

Now a manager

    // Know current choice and all choices
    // Notice the static constructor -- reload whenever confoig file changes
    public class ChoiceManager
    {
        private static ITravelProvidor defaultChoice;
        private static ChoiceCollection providers;
        static ChoiceManager()
        {
            Initialize();
        }

        private static void Initialize()
        {
            //Load ChoiceCollection From config file
            //set default one from config file
        }
    }

    // Just a collection class
    public class ChoiceCollection : ProviderCollection
    {       
        new public ITravelProvidor this[string name]
        {
            get { return (ITravelProvidor)base[name]; }
        }
    }
v

now add a class that can load and store stuff from config file

public class TravelProviderConfiguration : ConfigurationSection
{
    [ConfigurationProperty("choices")]
    public ProviderSettingsCollection Providers
    {
        get
        {
            return (ProviderSettingsCollection)base["choices"];
        }
    }

    [ConfigurationProperty("default", DefaultValue = "car")]
    public string Default
    {
        get
        {
            return (string)base["default"];
        }
        set
        {
            base["default"] = value;
        }
    }
}

Two Questions

Wholoads It? where from we load it.

modify ChoiceManagesr's Initialize implementation

private static void Initialize()
{
    //Load ChoiceCollection From config file
    TravelProviderConfiguration configuration = (TravelProviderConfiguration)ConfigurationManager.GetSection("TravelProvider");
    providers = new ChoiceCollection();
    ProvidersHelper.InstantiateProviders(configuration.Providers, providers, typeof(ITravelProvidor));
    providers.SetReadOnly();
    //set default one from config file
    defaultChoice = providers[configuration.Default];
}

Have the following configuration

  <TravelProvider default="car">
    <providers>
      <add name="car" type="SampleProvider.GoByCar, SampleProvider" connectionString="abcd"/>
      <add name="bike" type="SampleProvider.GoByBike, SampleProvider" connectionString="bcde"/>
      <add name="cab" type="SampleProvider.GoByCab, SampleProvider" connectionString="cdef"/>
    </providers>
  </TravelProvider>