I am going to have multiple "types" of an object and I am really not sure how best to retrieve/save those multiple types without having a separate save/retrieve for each type.
My classes:
public class Evaluation {
public int Id
public string Comment
}
public class EvaluationType_1 : Evaluation {
public string field
}
public class EvaluationType_1 : Evaluation {
public string field
}
What I would like to do in my repository:
public interface IEvaluationRepository {
public Evaluation getEvaluation(int id);
public SaveEvaluation(Evaluation);
}
Inside the get/save methods:
// Save/get common fields
Id
Comments
// Get child type, perform switch
Type childType = ???
switch(childType) {
// Set child-specific fields
}
I hope this makes sense. I'd rather not add a "type" column as I have this in another part of my database and I am not really liking it too much
UPDATE
Great comments, everyone - many thanks. Here's more info/questions for clarification, if necessary.
I love the idea of using interfaces and generics, I'm really at a loss for how to incorporate them into my repository pattern.
When I call getEvaluation
, I want it to return an abstract Evaluation, but I'm struggling with this code. Same with Saving - any insight on this would be excellent - thanks again!
UPDATE 2
I hate to keep refining this, but Daniel is helping me hone in on what exactly I am trying to ask :P.
Database: Evaluations Id (PK) Comment
EvaluationType1
Id (FK to Evaluations.Id)
Field
EvaluationType1
Id (FK to Evaluations.Id)
Field
So, in getEvaluation(int id)
, I need to figure out what type of Evaluation they want. Does this mean I should pass in a type? Same is true in saveEvaluation
, But I can do a switch/function map to see what Type
it is.