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’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
Here’s more info 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.
Update 2
Daniel is helping me hone in on what exactly I am trying to ask.
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.
Try this
Then you can have a collection of ISaveable such as
List<ISaveable>and call SaveFields on each one, regardless of their type. You are now programming against the interface rather than against concrete types. The first step towards decoupling of code.Edited: In response to your comment
In your repository, you would no longer program against the Evaluation class. Instead you would program agains t the methods in the interface that it implements:
Instead of:
You might have:
And an implementation of the repository might be like: