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 8396863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:38:39+00:00 2026-06-09T20:38:39+00:00

I am just starting in DDD and have a question regarding interfaces of objects

  • 0

I am just starting in DDD and have a question regarding interfaces of objects and repositories. Suppose I have the following objects

public interface IPerson { ... }

public class Student 
{
    double gpa;
    ...
}

public class Teacher
{
    double salary; ...
}

then I also have two repositories such as

public class StudentRepository :IRepository { public void Save(Student) }

public class TeacherRepository :IRepository { public void Save(Teacher) }

My question is, suppose I have a list of IPerson objects called persons, is there a way where I can just do something like repository.Save(persons) ? Without having to use reflection to figure out what type the IPerson actually is.

I currently have another class

PersonRepository :IRepository 
{
     public void Save(IPerson person)
     {
          if(Person is Student)
          {
               new StudentRepository.Save(person as Student);
          }
          else if(Person is Teacher)
          { ....}
      }
}

Then I can call personRepository.Save(persons);
However this doesnt feel like an optimal way to structure things. How can I improve this design?

Thanks

EDIT:

What I’m looking for is, say I receive an IPerson object called person. I do not necessarily know what implementation it is, I just want to call repository.Save(person) and have it call the correct repository. Is there a way to do this without using some sort of switch statement with reflection?

  • 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-06-09T20:38:40+00:00Added an answer on June 9, 2026 at 8:38 pm

    Consider using generic repository

    class Repository<T> :IRepository<T>
    {
         public void Save(T entity)
         {
             ...
         }
    }
    

    Usage

    IRepository<Student> repo1 = new Repository<Student>();
    repo1.Save(new Student());
    
    IRepository<Teacher> repo2 = new Repository<Teacher>();
    repo2.Save(new Teacher());
    

    Next you can use IoC container and DI just to pass repositories around instead of creating them

    At the top level, say in the main method or global.asax

    IRepository<Student> studentRepo = IoC.Current.Resolve<IRepository<Student>>();
    

    Later in a class that needs to save data, pass IRepository<Student> studentRepo into constructor

    class Foo
    {
        private IRepository<Student> repo
    
        Foo(IRepository<Student> repo)
        {
            this.repo = repo;
        }
    
        public void Save(Student s)
        {
            repo.Save(s);
        }
    }
    

    EDIT

    You can move a save operation to the IPerson<T>

    class Person<T> : IPerson<T>
    {
        private IRepository<T> repo;
    
        Person(IRepository<T> repo)
        {
            this.repo = repo;
        }
    
        public void Save()
        {
            repo.Save<T>();
        }
    }
    

    So when you derive Teacher and Student from Person<T> you pass correspondent T, like

    class Student : Person<Student>
    {
        private IRepository<Student> repo;
    
        Person(IRepository<Student> repo):base(repo)
        {
           ...
        }    
    }
    

    This shall give you the ability to work with List without Reflection or switch kung fu.

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

Sidebar

Related Questions

Just starting with FactoryGirl. I have a Model named Subscription . It has a
Just starting out with subversion, have set up repos for 3 current projects and
Just starting out with my first commercial app for the iStore but dont have
Just starting using the Firebug console. I have a test script which I'll post
Just starting to figure Python out. I've read this question and its responses: Is
Just starting out to program in python and I'm having the following issue. I
Just starting to use Ruby on Rails to see what its like. I have
Just starting out in Unix and need t workout this:- I have a csv
Just starting to look into WCF and came across the WSDualHttpBinding binding. I have
Just starting to learn scala for a new project. Have got to the point

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.