locked
Generic Repository RRS feed

  • Question

  • Hi Team,

    I have implemented generic repository and it is working fine till now. But I stuck at a place where i need to implement "SoftDelete" functionality at the generic repository  because there are lot of table where i don't want to delete the record permanently and just mark the records as isdeleted = true and also my Repository methods (i.e. GetALL and FindBy ) should only return the records which are not marked as deleted.  Following is my Repository class code to study and suggest.

    Thanks and regards

    public class Repository<TType, TContext> : IRepository<TType, TContext>
            where TContext : DbContext, new()
            where TType : class
        {
            private TContext _DataContext;
            public virtual TContext Context
            {
                get
                {
                    if (_DataContext == null)
                    {
                        _DataContext = new TContext();
                    }
                    return _DataContext;
                }
                set
                {
                    _DataContext = value;
                }
            }

            private IDbSet<TType> Entities
            {
                get
                {
                    var myEntitySet = Context.Set<TType>();
                    return Context.Set<TType>();
                }
            }

            public IQueryable<TType> GetAll()
            {
                return Entities;
            }

            public IQueryable<TType> FindBy(Expression<Func<TType, bool>> predicate)
            {
                return Entities.Where(predicate);
            }
            public TType GetById(object id)
            {
                var E = Entities.Find(id);
                return E;
            }

            public void Insert(TType entity)
            {
                Entities.Add(entity);
            }

            public void Update(TType entity)
            {
                if (entity == null)
                    throw new ArgumentNullException("entity");
                
                Entities.Attach(entity);
                Context.Entry(entity).State = EntityState.Modified;
            }

            public void Dispose()
            {
                if (Context != null) Context.Dispose();
            }

            public void Delete(TType entity)
            {
                Entities.Remove(entity);
            }

            public int Save()
            {
                return Context.SaveChanges();
            }

        }
    }


    Regards Gurmail Saini

    Friday, July 5, 2013 5:36 PM

Answers

  • Hi Gurmail,

    Is the "isdeleted" property defined in the entity class? If so, you need to use reflection to get the property value.

    PropertyInfo propInfo = entity.GetType().GetProperty("isdeleted");
    if (propInfo != null)
    {
        bool value = (bool)propInfo.GetValue(entity);
    }

    Else you can keep a collection like ObservableCollection or a dictionary in your repository to mark the isdeleted flag for your entities or entity id. Check the flag to see whether the entity is "soft deleted".

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help

    • Marked as answer by Chester Hong Thursday, July 18, 2013 2:12 AM
    Tuesday, July 9, 2013 8:37 AM

All replies

  • Hi Gurmail,

    Is the "isdeleted" property defined in the entity class? If so, you need to use reflection to get the property value.

    PropertyInfo propInfo = entity.GetType().GetProperty("isdeleted");
    if (propInfo != null)
    {
        bool value = (bool)propInfo.GetValue(entity);
    }

    Else you can keep a collection like ObservableCollection or a dictionary in your repository to mark the isdeleted flag for your entities or entity id. Check the flag to see whether the entity is "soft deleted".

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help

    • Marked as answer by Chester Hong Thursday, July 18, 2013 2:12 AM
    Tuesday, July 9, 2013 8:37 AM
  • Hi Hong,

    I don't like to use reflection in the repository. There are much better ways we implement soft delete to do if we don't want to use reflection.

    Eg.

    1. http://stackoverflow.com/questions/13299597/generic-repository-with-soft-delete-feature

    2. We can handle the delete functionality by overriding in SaveChanges Method as well.

    the Next problem once soft deleted marked is how to get the filtered records at generic repository level. I don't want add IsDeleted==false in all the derived repositories.

    Eg i want to get customer by city. I can filter that in my derived repository but i don't want do

       city = city && isdelete = false in all the findby/GetAll methods.

    Regards

    Gurmail Saini


    Regards Gurmail Saini

    Thursday, July 18, 2013 10:02 AM