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.
If you have knowledge of all the subclasses of
ConfigurationBaseand the rules by which you instantiate them are the same, then you should be looking at aFactorypattern.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.