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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:13:57+00:00 2026-06-14T05:13:57+00:00

I have a generic repository implementation. I’m using asp.net mvc c#, code first entity

  • 0

I have a generic repository implementation. I’m using asp.net mvc c#, code first entity framework.

I created an interface named ISoftDelete:

public interface ISoftDelete
{
    bool IsDeleted { get; set; }
}

I implemented Delete and GetById in my base repository as follows:

    public virtual void Delete(T entity)
    {
        if (entity is ISoftDelete)
        {
            ((ISoftDelete)entity).IsDeleted = true;
        }
        else
        {
            dbset.Remove(entity);
        }
    }

    public virtual T GetById(long id)
    {
        T obj = dbset.Find(id);
        if (obj is ISoftDelete)
        {
            if (((ISoftDelete)obj).IsDeleted)
                return null;
            else
                return obj;
        }
        else
        {
            return obj;
        }
    }

Now, I have 2 questions.

1) Is this approach is a good approach? Any performance related issues?

2) My original GetAll function in the base repository is like this:

    public virtual IEnumerable<T> GetAll()
    {
            return dbset.ToList();
    }

How shall I modify it in order to list records where IsDeleted == false, when T is derived from ISoftDelete?

Thank you!

  • 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-14T05:13:59+00:00Added an answer on June 14, 2026 at 5:13 am

    1) It does not seem ok for checking if (entity is ISoftDelete) every time you need to know it. If you are sure you are not going to check it anywhere else it may be ok. In terms of performance it would be better if you eleminate records that have IsDeleted == true and never fetch them from db. You may need to derive a new base repository which overrides these methods and implements new logics for ISoftDelete object.

    public abstract class BaseRepository<T>
    {
        // protected dbset;
    
        public virtual void Delete(T entity)
        {
            dbset.Remove(entity);
        }
    
        public virtual T GetById(long id)
        {
            return dbset.Find(id);
        }
    
        public virtual IEnumerable<T> GetAll()
        {
            return dbset.ToList();
        }
    }
    
    public abstract class SoftDeleteRepository<T> : BaseRepository<T> where T : ISoftDelete
    {
        public override void Delete(T entity)
        {
             entity.IsDeleted = true;
        }
    
        public override T GetById(long id)
        {
            return (from t in dbSet
                    where !t.IsDeleted && t.Id == id select t)
                    .FirstOrDefault();
        }
    
        public override IEnumerable<T> GetAll()
        {
            return (from t in dbset where !t.IsDeleted select t).ToList();
        }
    }
    
    public static class RepositoryFactory
    {
         public static BaseRepository<T> GetInstance<T>()
         {
              // pseudo code
              if (typeof(T) implements ISoftDelete)
                   return repository of T which extends SoftDeleteRepository
              return repository of T which extends BaseRepository
         }
    }
    

    2) may be something like

     return (from t in dbset  where 
           (t is ISoftDelete && !(t as ISoftDelete).IsDeleted) || 
           !(t is ISoftDelete))
     .ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a generic repository that is using Entity Framework 4 with the DbContext
I am writing a generic repository to interface with EF using DBContext. I have
I have a repository class that inherits from a generic implementation: public namespace RepositoryImplementation
So, I am kind of stumped. I have been using a generic repository, and
I have a generic Repository implementation on top of Linq2SQL. Basically, it uses Table
I have an implementation like that: public interface IGenericRepository { //... void Update<T>(T entity,
I have read a number of posts regarding the implementation of a generic repository.
I have this simple Delete Get and Post methods in a asp.net mvc application
I'm playing around with Domain Driven Development. I'm using a generic repository implementation that
I'm using FluentValidation framework. And at the moment I have several validators (per entity).

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.