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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:04:53+00:00 2026-05-20T03:04:53+00:00

In trying to implement the repository pattern I’ve run into a minor problem that

  • 0

In trying to implement the repository pattern I’ve run into a minor problem that I’m concerned actually belies bigger issues.

I’ve got a DatabaseEntity<T> which I’m using to handle all the basic CRUD operations and from which all other classes that need to be stored in a database will descend from. It is working fine for classes that inherit directly from it, however when using it with classes that have an intermediate parent I’ve run into a problem.

Suppose I have three other classes, Parent, ChildA and ChildB and and the inheritance looks like:

DatabaseEntity
          |
     Parent
     |        |
ChildA ChildB

Also suppose that DatabaseEntity<T> has a method with the following signature:

public static T FindBy(int id)  

The issue I’m having is when I try something like:

ChildA Foo = ChildA.FindBy(SomeID);

I get a compiler error telling me that there’s no implicit conversion from a Parent to a ChildA. This is because Parent is the class that’s being passed in for the type parameter to DatabaseEntity for both ChildA and ChildB. Easy fix I think, just add a type parameter to Parent thus passing through the appropriate type. Only wait a second, then I’ll have to explicitly define the subtype any time I’m using Parent which ruins any polymorphism. No, on second thought maybe that’s not such a great fix.

I think that I could just drop the type parameter on the class DatabaseEntity itself and have each method require a type parameter but then I’d have to do something like:

ChildA Foo = ChildA.FindBy<ChildA>(SomeID);

While that compiles, it seems less clean and certainly requires more typing. Visual Studio asks if I’m missing a cast and while its true I could just cast my first example its only a matter of time before I accidentally type out:

ChildB Foo = (ChildB) ChildA.FindBy(SomeID)

I’m not especially pleased with any of the solutions I’ve thought of so far and I’m hoping someone here can point out an elegant one that I’ve missed.

  • 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-20T03:04:53+00:00Added an answer on May 20, 2026 at 3:04 am

    I think that making Parent a generic class is the way to go. You didn’t explain what exactly is the purpose of the type T in your example, but I suppose you want it to be the actual type of the entity, so for example your Parent would inherit Entity<Parent>.

    You can still write polymorphic code in this scenario – you just have to use generics:

    static void Foo<T>(Parent<T> p) where T : Parent<T>
    {
      Parent<T> entity = p.Find();
    }
    

    This method can be called with both ChildA and ChildB. The only tricky aspect is that you cannot actually create an instance of Parent<Parent<...>> (because the dots would have to be replaced with more nested Parent<...> types), but you can write somthing like this:

    class ParentFix : Parent<ParentFix> { }
    

    .. then you can pass instances of ParentFix to the Foo method as well.

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

Sidebar

Related Questions

I am trying to implement Repository pattern in JavaScript. I have ViewModel which i
I am trying to implement generic repository pattern using LINQ to SQL data context.
I'm trying to implement the repository pattern using a generic repository like the one
I am trying to implement a generic repository pattern. I found this site which
I'm trying to implement the best code re-usability. The problem is that I can't
I'm trying to implement the repository pattern with ef4 ctp5, I came up with
I'm trying to implement the repository pattern using entity framework code first rc 1.
I'm trying to implement svnperms into a repository, but am having difficulty with a
When using ASP.NET MVC plus Entity Framework, and trying to implement a generic repository
I am trying implement a most recent widget into my tumblr post. So far,

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.