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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:21:47+00:00 2026-06-15T15:21:47+00:00

I have three general repository that handle three base classes: public class Entity {

  • 0

I have three general repository that handle three base classes:

public class Entity
{
    public int Id { get; set; }
}

public class Repository
{
    public TEntity[] GetAll<TEntity>() where TEntity : Entity
    {
        return _context.Set<TEntity>.ToArray();
    }
}

public class ArchiveEntity : Entity
{
    public bool Deleted { get; set; }
}

public class ArchiveRepository
{
    public TEntity[] GetAll<TEntity>() where TEntity : ArchiveEntity
    {
        return _context.Set<TEntity>.Where(x => x.Deleted == false).ToArray();
    }
}

public class LogicalStorageEntity : ArchiveEntity
{
    public int StorageId { get; set; }
}

public class LogicalStorageRepository
{
    public int CurrentStorageId { get; set; }

    public TEntity[] GetAll<TEntity>() where TEntity : LogicalStorageEntity
    {
        return _context.Set<TEntity>
            .Where(x => x.Deleted == false)
            .Where(x => x.StorageId = CurrentStorageId)
            .ToArray();
    }
}

Is there way to have one repository that filters entities differently depending on base class? Something that looks like:

public class Entity
{
    public int Id { get; set; }
}

public class ArchiveEntity : Entity
{
    public bool Deleted { get; set; }
}

public class LogicalStorageEntity : ArchiveEntity
{
    public int StorageId { get; set; }
}

public class UniversalRepository
{
    public TEntity[] GetAll<TEntity>() where TEntity : Entity
    {
        if (typeof(TEntity) is LogicalStorageEntity)
        {
            return _context.Set<TEntity>
                .Where(x => /* how to filter by x.Deleted */)
                .Where(x => /* how to filter by x.StorageId */)
                .ToArray();
        }

        if (typeof(TEntity) is ArchiveEntity)
        {
            return _context.Set<TEntity>
                .Where(x => /* how to filter by x.Deleted */)
                .ToArray();
        }

        return _context.Set<TEntity>.ToArray();
    }
}

Edit. The quiestion is not about how to check if entity is of specific type. The real difficult part is to apply filter when you know that entity can be filtered by Deleted or some other property. Since there is limitation TEntity : Entity , you cannot access the Deleted property.

  • 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-15T15:21:48+00:00Added an answer on June 15, 2026 at 3:21 pm

    You can, but you shouldn’t.

    The separate repository per entity type is the correct way to go because that way you encapsulate the entity specific logic in the repository for that entity. If you try and make a universal repository you will have to keep adding to/changing the logic in a huge method with loads of if checks.

    If you want to try and promote some code re-use, you can however provide the functionality from a base repository and allow the specific repositories to specify the behaviour:

    public abstract class Repository<TEntity> where TEntity : Entity
    {
        protected virtual Expression<Func<TEntity, bool>> Filter { get { return null; } }
    
        public TEntity[] GetAll()
        {
            if (this.Filter == null)
            {
                return _context.Set<TEntity>().ToArray();
            }
            else
            {
                return _context.Set<TEntity>().Where(this.Filter).ToArray();
            }
        }
    }
    
    public class ArchiveRepository : Repository<Archive>
    {
        public ArchiveRepository()
        {
            this.Filter = archive => !archive.IsDeleted;
        }
    }
    

    Using this approach, you can reduce the amount of repeated code but increase the readability and maintainability of the code base.

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

Sidebar

Related Questions

As input, I have a date string that can take three general formats: a)
We have a report in Crystal Reports. On that report there is a general
Have three classes User, Group and Field. Many to many relationship on User /
Have three divs in a container that I want to float over a large
I currently have a Repository/UnitOfWork pattern down. However, there is one hard coupling that
I have a working Tycho build that produces a working p2 repository. My current
I am trying to set up my ExtJS 4 project so I have three
I have three lists on Sharepoint 2010 and I have working code that gets
New to Moq and Mocking in general. Testing a class that has a generic
I have three services in my Android app that are fired by two broadcast

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.