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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:04:37+00:00 2026-05-18T05:04:37+00:00

I haven’t got problems with code since my last problem with interface covariance. Today

  • 0

I haven’t got problems with code since my last problem with interface covariance. Today I took another approach to the OLGAtherer architecture and I found huge (for me) obstacle. I hope that its huge only for me and I will find answer here 🙂 OK, please check this out:

I have abstract generic class:

abstract class Repository<T> where T : Entity

this class contain signatures of four CRUD methods (add, insert, etc.) and two protected fields. Any repository that I will create must inherit from this superclass. I have repositories like, for example:

class BookRepository : Repository<Book>

Now I have class DB that is responsible for operations on DB file. And there is my problem. Look at this method placed in DB class:

    void Add(List<Entity> enitites, Type entityType)
    {
       Repository<entityType> repo = EntitiesFactory(entityType);
       repo.Add(entities);
    }

I know that above method won’t work, but this visualize my problem (hopefully) – I would like to dynamically create repository of the specified type, but I have no idea how to do this. This has been always my main OOP problem – I could code inheritance (or implementation of interfaces) but I could not use these inherited classes. Please clarify this to me. Thank you in advance.

Paweł


I used hints that you provided and this is what I have now:

public void Add<T>(List<T> entities, string repoName) where T : Entity
{
   Repository<T> repo = RepoFactory<T>(repoName, typeof(T));
   repo.Add(entities);
}

private Repository<T> RepoFactory<T>(string repoName, Type t) where T : Entity
{
   if (t == typeof(Book))
   {
   Repository<T> repo = (Repository<T>)((object)(new BookRepository(this.ConnectionString, repoName)));
   }

   return null;

}

RepoFactory is now in DB class (which is, by the way, some sort of Bridge here) – it should be moved elsewhere probably, but it’s not a main problem here.

First, I don’t know whether RepoFactory body is implemented in the proper way – I guess not.

Second – I assume that I will call DB methods from other class (let’s say – WindowsForm, for simplicity). I will call Add with parameter T dependent on user’s choice. This is situation that I have problem and fix it temporarily, because now DB is OK, but if I would have to implement WindowsForm code, I will encounter it another time (I think) – in the future I will try to figure out answer to the same question, but on the higher level (above DB) – I hope you understand me…
I basically mean, that I will not know how to dynamically call Add method, because T will be dependent on user’s choice (OLGAtherer has to be Repository Manager that would support Books and other kind of collections). And if user will try to work with his book collection, I will have to call Add() method. If with Comics collection, Add(). I don’t want to write class above for each kind of collection, but rather use one call of Add to do this.

If you understand what I mean, you’re really good 🙂 My English sucks, but I’m working on it.
Thank you for previous answers and thanks in advance for further ones.
Paweł

  • 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-18T05:04:38+00:00Added an answer on May 18, 2026 at 5:04 am

    I’m not quite sure what you want. Will this do?

    void Add<T>(List<T> entities) where T : Entity
    {
       Repository<T> repo = EntitiesFactory(typeof(T));
       repo.Add(entities);
    }
    

    Also, I suggest that you be as flexible as possible with input parameters, e.g. use IEnumerable<T> instead of List<T> for this method and also in the Repository<T>.Add() method. You might want to pass in a lazily-evaluated query, or chain multiple LINQ methods together.

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

Sidebar

Related Questions

Haven't done ASP.NET development since VS 2003, so I'd like to save some time
I haven't used C++ since college. Even though I've wanted to I haven't needed
I haven't programmed games for about 10 years (My last experience was DJGPP +
I haven't used a png hack for IE6 for ages. Last time I used
Haven't fired up reflector to look at the difference but would one expect to
Haven't seen many Geneva related questions yet, I have posted this question in the
I haven't touched sharepoint in years. If I want to setup a development environment
I haven't completely understood, how to use sigprocmask() . Particularly, how the set and
I haven't seen any questions relating to GNU autoconf/automake builds, but I'm hoping at
I haven't done a lot of .NET programming, but I've examined a few of

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.