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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:25:49+00:00 2026-05-31T09:25:49+00:00

I have an application that is structured as an service layer wich uses a

  • 0

I have an application that is structured as an service layer wich uses a repository layer for persistence.
I’m trying to create a generic controller class to reuse shared behavior but I’m having trouble trying to set the generic parameters. The following code:

public class BusinessEntity
{ }

public class Person : BusinessEntity
{ }

public interface IRepository<T> where T : BusinessEntity
{ }

public interface IService<T, R>
    where T : BusinessEntity
    where R : IRepository<T>
{ }

public partial interface IPersonRepository : IRepository<Person>
{ }

public interface IPersonService : IService<Person, IPersonRepository>
{ }

public abstract class BaseController<X, Y>
    where X : BusinessEntity
    where Y : IService<X, IRepository<X>>
{ }

public class PersonController : BaseController<Person, IPersonService>
{ }

fails at compilation with

The type ConsoleApplication.IPersonService cannot be used as type parameter Y in the generic type or method ConsoleApplication.BaseController<X,Y>. There is no implicit reference conversion from ConsoleApplication.IPersonService to ConsoleApplication.IService<ConsoleApplication.Person,ConsoleApplication.IRepository<ConsoleApplication.Person>>

this works

public interface IPersonService : IService<Person, IRepository<Person>>

but I lose the custom repository

There is a way to make the compiler realize IPersonRepository is an IRepository<Person>?

  • 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-31T09:25:51+00:00Added an answer on May 31, 2026 at 9:25 am
    public class BusinessEntity
    { }
    
    public class Person : BusinessEntity
    { }
    
    public interface IRepository<T> where T : BusinessEntity
    { }
    
    public interface IService<T, R>
        where T : BusinessEntity
        where R : IRepository<T>
    { }
    
    public partial interface IPersonRepository : IRepository<Person>
    { }
    
    public interface IPersonService : IService<Person, IPersonRepository>
    { }
    
    public abstract class BaseController<X, Y, Z>
        where X : BusinessEntity
        where Y : IService<X, Z>
        where Z : IRepository<X>
    { }
    
    public class PersonController : BaseController<Person, IPersonService, IPersonRepository>
    { } 
    

    To address your comment:

    IPersonService can extend the base service class to add custom facilities, like FindPersonsUnderAge(). For this it requires a custom repository. Actually LINQ avoids a lot of custom repository code, but sometimes they are required.

    Couldn’t IPersonService do that without requiring the repository type to be a type parameter? For example:

    public interface IService<T> where T : BusinessEntity { } 
    
    public interface IPersonService : IService<Person>
    {
        IEnumerable<Person> FindPersonsByAge(double minAge, double maxAge);
    }
    
    public class Service<T, R> : IService<T>
        where T : BusinessEntity 
        where R : IRepository<T> 
    { }
    
    public class PersonService : Service<Person, IPersonRepository>, IPersonService
    { }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that sends messages to an external web service. I build
I have an application that uses NHibernate as its ORM and sometimes it experiences
So we have a PHP+Zend Framework+Doctrine 1.2 application that has the following structure: Controller
I have a flex 4.6 application calling a web service that access a MS
I have a class that handles all the interaction in my application with my
I have a WPF application that uses a WCF services to perform operations on
I have a service running in my Android application which contains a HashMap that
My colleague and I have a web application that uses Spring 3.0.0 and JPA
I have a C++ application that includes a number of structures with manually controlled
I have application that makes different queries with different results so the caching in

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.