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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:12:40+00:00 2026-06-13T00:12:40+00:00

I have a base repository contract which other contracts extend, like follows public interface

  • 0

I have a base repository contract which other contracts extend, like follows

public interface IBaseRepository<T> where T : class 
{
  IList<T> GetContents();
}

and then there are other contracts which extend it like follows

public interface IRepository1 : IBaseRepository<MyClass1>
{
}

public interface IRepository2 : IBaseRepository<MyClass2>
{
}

I implement IRepository1 as follows

public class Repository1 : IRepository1
{
 public IList<MyClass1> GetContents()
 {
  //some code goes here
 }
} 

similarly for IRepository2

public class Repository2 : IRepository2
{
 public IList<MyClass2> GetContents()
 {
  //some code goes here
 }
} 

Now i have a service Service1 which implments IService like follows

public class Service1 : IService
{


}

I want to use my base repository (IBaseRepository) here in my service constructor, get an instance of this base repository and use it like so

 public class Service1 : IService
 {
   private IBaseRepository<T> _baseRepository;
   public Service1(IBaseRepository<T> baseRepository)
   {
     _baseRepository = baseRepository;
   }

   public MyMethod1()
   {
      var contentsOfType1 = _baseRepository<MyClass1>.GetContents();
   }

   public MyMethod1()
   {
      var contentsOfType2 = _baseRepository<MyClass2>.GetContents();
   }
  }

and this is what i am unable to do.

So i have a generic base repository contract with type T and have other contracts (interfaces) extending the base contract and also specifying what type T will be.

All these contracts (which extend generic base contract) have thier individual implementations.

What i want to do is in my service class, instantiate this generic base contract, and use it to infer the extending types (and hence implementations) and use the method from the base repository.

So if the base contract is
IBaseRepository<T>

and extending contract is
IRepository1 : IBaseRepository<MyClass1>

which is implemented by
Repository1 : IRepository1

i want to use this in my service class like

public class service()
{
   *private IBaseRepository<T> _repo;

   public  service(IBaseRepository<T> repo)
   { 
    *_repo = repo;
   } 

   public void MyMethod()
   {

     *var x = _repo<MyClass1>.MethodFromIBaseRepository()
   }

}

So its the *marked lines i want to achieve, which i am unable to.

I am using castle windsor for DI.

Thanks for your help guys

  • 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-13T00:12:41+00:00Added an answer on June 13, 2026 at 12:12 am

    You should not have other repository interfaces besides your generic IRepository<T>. If you need those, you are missing an abstraction.

    For instance, a common reason for people to have custom repository interfaces is because they have a custom query that some repository has, while other don’t. For instance:

    public interface IEmployeeRepository : IRepository<Employee>
    {
        Employee GetEmployeeOfTheMonth(int month);
    }
    

    The problem here is that the IEmployeeRepository is abused for a ‘custom query’. Custom queries deserve their own (generic) abstraction:

    // Defines a query
    public interface IQuery<TResult>
    {
    }
    
    // Defines the handler that will execute queries
    public interface IQueryHandler<TQuery, TResult>
        where TQuery : IQuery<TResult>
    {
        TResult Handle(TQuery query);
    }
    

    With this abstraction we can add custom queries to the system, without the need of creating IRepository<T> derivatives:

    public class GetEmployeeOfTheMonthQuery : IQuery<Employee>
    {
        [Range(1, 12)]
        public int Month { get; set; }
    }
    
    class GetEmployeeOfTheMonthHandler : IQueryHandler<GetEmployeeOfTheMonthQuery, Employee>
    {
        public Employee Handle(GetEmployeeOfTheMonthQuery query)
        {
            // todo: query the database, web service, disk, what ever.
        }
    }
    

    A consumer that needs to know the employee of the month, can now simply take a dependency on IQueryHandler<GetEmployeeOfTheMonthQuery, Employee> and execute the query as follows:

    var query = new GetEmployeeOfTheMonthQuery { Month = 11 };
    var employee = this.employeeOfMonthHandler.Handle(query);
    

    This might seem like overhead, but this model is very flexible, scalable, and has many interesting benefits. For instance, it is very easy to add cross-cutting concerns by wrapping handlers with decorators.

    This also allows our reposities to be hidden behind one generic interface, which allows us to easily batch register them at once and add decorators to them as well.

    For more in depth information, read this article: Meanwhile… on the query side of my architecture.

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

Sidebar

Related Questions

I have a base repository that looks like this: public class BaseRepository<T> : IBaseRepository<T>
I have a base class like this: public class BaseResponse { public string ErrorMessage
If I have a generic class like this: public class Repository<T> { public string
I have a generic repository as like that public class Repository<T> : IRepository<T> where
I have a generic base repository class for LINQ-to-Entities that can do things like
I have a Repository model as follows: class Repository < ActiveRecord::Base belongs_to :user has_many
I have an interface with several events I have base class implementing the interface
in my zf application i have base model class Application_Model, which connects to db
I have a base class: class motorcycle { public: virtual int speed() { return
Our architecture uses the Repository pattern extensively. We have an abstract base class for

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.