Interface, Abstract or Inheritance? C# Design Question -


i have table houses 2 entities, staticprogram , dynamicprogram. there 1 column in table called programtype determines if program of type "static" or "dynamic". though these 2 entities stored in 1 table (i guessing because primitive fields static , dynamic programs same) business point of view these 2 different entities.

so, created 2 classes staticprogram , dynamicprogram. however, donot want create 2 seperate data access classes because going exact same code replicated twice. tried creating "program" class base class , inherited staticprogram , dynamicprogram classes down casting not supported can't return "program" object data access class , cast "staticprogram" class etc.

so, options? can create iprogram interface , have staticprogram , dynamicprogram implement interface , have data access class return iprogram? or making data access method generic method (if possible , recommended approach need not have expreience generics)? other suggestions?

i appreciate help!!

update: data access method static indeed:

public static class programdatamapper {     public static program getprogram(int programid)     {         program p = new program();         // database stuff         p.name = reader["name"].tostring();         ...         return p;     } } 

base class looks like:

public class program {     // properties      public static program getprogram(int programid)      {             return programdatamapper.getprogram(programid);      } } 

finally derived class:

public class dynamicprogram : program {     // additional business related properties      public new static dynamicprogram getprogram(int programid)         {             return (dynamicprogram)program.getprogram(programid);         } } 

this compiles fine cannot cast "program" "dynamicprogram" exception @ runtime.

also, generic method? unless off mark here but, theoratically, can not like:

public static iprogram getprogram<t>(int programid) t : iprogram {     iprogram program;     type returntype = typeof(t);     if(returntype staticprogram)     {         program = new staticprogram();     }     else if(returntype = dynamicprogram)     {         program = new dynamicprogram();     }      //if (t staticprogram)         //{         //  returnvalue = new staticprogram();         //}         //else if (t dynamicprogram)         //{         //  returnvalue = new dynamicprogram();         //}      // db operations } 

of course code above doesn't work. "the given expression never of provided type (staticprogram) type."

how this:

public static class programdatamapper {     // change generic here manufacture class deriving program.     public static t getprogram<t>(int programid)         t : program, new()     {         t p = new t();         // database stuff         p.name = reader["name"].tostring();         ...         return p;     } }  public abstract class program {     // properties      // manufacture concrete class derives program     public new static t getprogram<t>(int programid)         t : program, new()     {         return programdatamapper.getprogram<t>(programid);     } }  public class dynamicprogram : program {     // additional business related properties      public new static dynamicprogram getprogram(int programid)     {         // manufacture dynamicprogram         return program.getprogram<dynamicprogram>(programid);     } } 

philosophically, perhaps interface might more suited, getting have work minimal effort, should trick.

your posted code not cast dynamicprogram because constructed program object in programdatamapper. while dynamicprogram "is a" program, reverse not true. in code have provided here, using generics, dynamicprogram constructed instead of program. using generics removes need casting @ in dynamicprogram.getprogram method.


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -