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

  • Home
  • SEARCH
  • 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 4579014
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T20:34:57+00:00 2026-05-21T20:34:57+00:00

Say I have 3 tables in my database: people, tasks and projects Say I

  • 0

Say I have 3 tables in my database: people, tasks and projects

Say I have 3 classes for repository’s: PeopleRepository, TaskRepository and ProjectRepository.

Now I want a single method, that will work with any of these classes.

Obviously I could just write out loads of code for each table, but I’d rather keep code down to a minimum. I already have reflection working fine for the different column names that might be passed to the method, but I can’t seem to get code like this to work.

switch (tableName)
            {

                case "people":
                    var repository = new PeopleRepository();
                    break;
                case "tasks":
                    var repository = new TaskRepository();
                    break;
                case "projects":
                    var repository = new ProjectRepository();
                    break;
             }

//Modify respective database table
repository.getItem(id); //etc
repository.Save(); //etc

I’ve tried a few other things like this, but none of them seemed to work. Changing the scope of where the variable is defined etc.

I feel like c# should have something nice to deal with this, is this so? Or do I have to write the same code in each switch statement?

  • 1 1 Answer
  • 1 View
  • 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-21T20:34:58+00:00Added an answer on May 21, 2026 at 8:34 pm

    You could make an interface that those classes would implement. Then use

    IRepository repository;
    
    // your switch here
    //switch() { case x: repository = new TypeRepository(); }
    
    repository.GetItem(id);
    repository.Save();
    

    You can also make repository dynamic

    dynamic repository;
    

    And the type will be resolved in runtime.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have 2 database tables: table B is a set of people,
I have multiple tables in a MySQL database. Lets say they look like this:
Let's say we have system A comprising a MySQL database, with several tables. After
I have a Firebird database, which let's say has Tables A and B which
Let's say you have a database with a single table like... --------------------------------------------- | Name
Let's say I have three tables: Office ID SalespeopleOffice ID OfficeID PersonID People ID
Lets say you have a large, popular database-driven website. There are people on the
Let's say I have the following tables in a database forming a Many-to-Many Relationship.
Let's say i have a database table called 'people'. 70% of the fields in
Let's say that I have and database table called People, and entity People. Let's

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.