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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:48:18+00:00 2026-06-02T16:48:18+00:00

MVC has nothing to do with my problem. Don’t get confused for the example.

  • 0

MVC has nothing to do with my problem. Don’t get confused for the example. Its a pure C# problem

The title of the question is not well explaining the question I think.


Suppose, I have a base class for some entity classes called EntityBase

Some classes are like

class Entity1 : EntityBase
class Entity2 : EntityBase

I have an abstract Repository that works with basic operations on entities. The declaration is:

abstract class RepositoryBase<TEntity> where TEntity : EntityBase

And there are several implementations of this class

class Repository1 : RepositoryBase<Entity1>
class Repository2 : RepositoryBase<Entity2>

Now there are some controllers with a base:

public abstract class RepositoryControllerBase<TRepository, TEntity> 
        where TRepository : RepositoryBase<TEntity>
        where TEntity : EntityBase

And implementations are like

class Controller1 : RepositoryControllerBase<Repository1, Entity1>
class Controller2 : RepositoryControllerBase<Repository2, Entity2>

Now, you must have noticed that, When the type of repository in a controller is Repository1, The entity type must be Entity1. Otherwise it will be a compilation error.

So, I think there is a way to skip the second generic type and automatically infer that one. I just do not know how. Any suggestions?

Perhaps, the problem could be easily solved with ? if it was Java. Replacing ControllerBase declaration with

public abstract class RepositoryControllerBase<TRepository> 
            where TRepository : RepositoryBase<?>
  • 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-02T16:48:20+00:00Added an answer on June 2, 2026 at 4:48 pm

    There is no constraint type inference for a reason: http://blogs.msdn.com/b/ericlippert/archive/2012/03/09/why-not-automatically-infer-constraints.aspx

    Also, the obvious counterexample for your idea would be using interfaces:

    interface IEntity1 : IEntityBase {}
    interface IEntity2 : IEntityBase {}
    
    interface IRepositoryBase<TEntity> where TEntity : class, IEntityBase {}
    
    class Repository1 : RepositoryBase<IEntity1> {}
    class Repository2 : RepositoryBase<IEntity2> {}
    class Repository12 : IRepositoryBase<IEntity1>, IRepositoryBase<IEntity2> {}
    
    public abstract class RepositoryControllerBase<TRepository, TEntity> 
        where TRepository : RepositoryBase<TEntity>
        where TEntity : IEntityBase {}
    
    class Controller1 : RepositoryControllerBase<Repository1, Entity1>
    class Controller2 : RepositoryControllerBase<Repository2, Entity2>
    class Controller12 : RepositoryControllerBase<Repository12, Entity1>
    

    Without specifying an Entity1 type parameter in a Controller12 definition, what should a compiler check?

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

Sidebar

Related Questions

I'm new to MVC and I probably don't completely understand its logic yet and
You all know that the master page in MVC has this: <nav> <ul id=menu>
My ASP.NET MVC project has some chapter class and related tag class They have
Once an asp.net MVC project has a web deployment project associated with it, there
I am building a web application using ASP.NET MVC that has two very distinct
It looks like the final release for ASP.NET MVC 2 has been already around
My application has an MVC structure. Is it sufficient to only cache the model
I'm building a MVC application which has a heavy client side scripting using Knockout,JQuery
I have a MVC application that has a Jquery Post $.post(virtualPath + cookie +
I have an MVC controller that has a single constructor, I am using structure

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.