Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 904671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:05:32+00:00 2026-05-15T16:05:32+00:00

I am going to have multiple types of an object and I am really

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T16:05:33+00:00Added an answer on May 15, 2026 at 4:05 pm

    Try this

    public interface ISaveable {
       void SaveFields();
    }
    
    public abstract class Evaluation : ISaveable {
      public int Id
      public string Comment
    
      public virtual void SaveFields() {
         //Save ID and Comments
      }
    }
    
    public class EvaluationType_1 : Evaluation {
        public string field1
    
      public override void SaveFields() {
         //Save field1
         base.SaveFields();
      }
    
    }
    
    public class EvaluationType_2 : Evaluation {
       public string field2
    
      public override void SaveFields() {
         //Save field2
         base.SaveFields();
      }
    
    }
    

    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:

    public interface IEvaluationRepository {
      public Evaluation getEvaluation(int id);
      public SaveEvaluation(Evaluation);
    }
    

    You might have:

     public interface ISaveableRepository {
       public ISaveable getSavable(int id);
       public Save(ISaveable saveable);
     }
    

    And an implementation of the repository might be like:

     public class SaveableEvaluationRepository : ISaveableRepository {
       public ISaveable getSavable(int id) {
           //Add your logic here to retrieve your evaluations, although I think that 
           //this logic would belong elsewhere, rather than the saveable interface.
       }
    
       public Save(ISaveable saveable) {
           saveable.SaveFields();
       }
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am going to have one directory with multiple sub-directories. I want to be
I'm having problems binding on a form with multiple models being submitted. I have
I have a Swing app with multiple JCheckBoxes. Each JCheckBox corresponds to another object
I have a PHP object retrieved from MySQL. I can retrieve the values for
I have a database which is going to have UMT timestamps in standard sql
What I have going on is a listview inside of my windows form. How
I have a website that in a few months is going to have traffic
I'm going to have to write a number of vba modules for a project
I'm going to have a single site that needs to be themed a number
My site is going to have a credit system that basically works a lot

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.