Elena
Elena

Reputation: 839

C# architecture - design patterns

Not sure how to implement this scenario:

I have a base class let's say

   public abstract class ConfigurationBase
   {
     public int Id {get; set;}

     public string Name {get; set;}

   }

and several derived classes

   public class Config1 : ConfigurationBase
   {
     public string Url {get;set;}
     public string ReferrerUrl {get; set;}
   }


   public string Config2 : ConfigurationBase 
   {
     public string Domain {get; set;}
     public bool AllowCookie {get; set;}
   }

and a ConfigurationService which gets called with different parameters.

The idea would be that according to the parameters range to construct an instance of Config1 or Config2 or etc.

So the question is how can this be implemented in code?

Depending on same condition, instantiate a certain type.

One idea or how we had it so far, was to store the full qualified type to be instantiated in the database and construct it through reflection.

I am kind of reluctant to use this approach and I am asking if there are some other answers.

Upvotes: 2

Views: 492

Answers (4)

T. Fabre
T. Fabre

Reputation: 1527

I think the proper way would be to create a factory class that handles creation of your configuration objects :

public class ConfigurationFactory {
    public ConfigurationBase GetConfig(object[] parameters)
       // Build your objects here according to your params... do stuff...
       if (parameters[0] ...)
            return new Config2(...);
       elseif ...
            return new Config1(...);
    }
}

And then call that method with your parameters

ConfigurationFactory factory = new ConfigurationFactory();
ConfigurationBase config = factory.GetConfig(parameters);

Even though this method requires knowledge of how to create your different objets, it is only centralized in one place, so it can easily be changed.

Additionnaly, you might want to define an more generic interface defining how to get your configuration data, so that your objects will be easier to handle without knowing their concrete type.

Hope that helps

Upvotes: 4

dowhilefor
dowhilefor

Reputation: 11051

A rather weird idea :) So don't blame me, is to build a hashvalue of the different parameters types and store an internal dictionary of hashvalue and specific Config type. So the factory method could be params, then build the hashvalue, retrieve the config type from the dictionary create and pass the params using reflection and return that.

I have to say, all my ideas resolve around reflection :)

Upvotes: 1

Manish Basantani
Manish Basantani

Reputation: 17499

How about a Factory Pattern?

Edit: realized that you don't want to keep separate methods, in that case you will need an enum (at least) + an (ugly) method accepting all possible parameters ( that is the columns in your table)

 public class ConfigurationService {

         private ConfigurationBase  CreateConfig1(string id, string name, string url, string referenceUrl){...}

         private ConfigurationBase  CreateConfig2(string id, string name, string domain, bool allowCookie){...}

         public ConfigudationBase CreateConfig( string id, string name, string domain, bool allowCookie, string url, string referenceUrl, ConfigType configTypeEnum){
          //call the expected factory method based on enum type.
         }
    }

Upvotes: 2

Davin Tryon
Davin Tryon

Reputation: 67296

If you have knowledge of all the subclasses of ConfigurationBase and the rules by which you instantiate them are the same, then you should be looking at a Factory pattern.

If you do not have knowledge of all the subclasses, then you need to pull the type in dynamically from a data store (not necessarily a database) and instantiate it at runtime.

If you do not know all the rules by which you instantiate the objects, you need to pull the rules from a data store and execute them.

Obviously, the more dynamic you make it the more complexity you are going to have to deal with.

Upvotes: 1

Related Questions