0

I have the interface.

public interface IRepository<T> where T : class
{
   void Add(T entity);
   void Update(T entity);
   void Delete(T entity);
   void Delete(Expression<Func<T, bool>> filter);

Then

 public interface ICatRepository : IRepository<Cat>
 {
 }

Also I have the base class.

public abstract class RepositoryBase<T> where T : class
{
    private DbContext dataContext;
    protected readonly IDbSet<T> dbset;
    protected RepositoryBase(IDatabaseFactory databaseFactory)
    {
        DatabaseFactory = databaseFactory;
        dbset = DataContext.Set<T>();
    }

    protected IDatabaseFactory DatabaseFactory
    {
        get; private set;
    }

    protected DbContext DataContext
    {
        get { return dataContext ?? (dataContext = DatabaseFactory.Get()); }
    }
    public virtual void Add(T entity)
    {
        dbset.Add(entity);           
    }
    public virtual void Update(T entity)
    {
        dbset.Attach(entity);
        dataContext.Entry(entity).State = EntityState.Modified;
    } 
    public virtual void Delete(T entity)
    {
        dbset.Remove(entity);           
    }
    public virtual void Delete(Expression<Func<T, bool>> filter)
    {
        IEnumerable<T> objects = dbset.Where<T>(filter).AsEnumerable();
        foreach (T obj in objects)
           dbset.Remove(obj);
    } 

Now I have the implementation class.

 class CatRepository : RepositoryBase<Cat>, ICatRepository
 {
    public CatRepository(IDatabaseFactory databaseFactory) : base(databaseFactory)
    {

    }

    public void Add(Cat entity)
    {
        throw new NotImplementedException();
    }

    public void Delete(Cat entity)
    {
        throw new NotImplementedException();
    }

    public void Delete(Expression<Func<Cat, bool>> filter)
    {
        throw new NotImplementedException();
    }

My entity framework knowledge is little rusty. Not sure how to implement Add, Delete methods etc. Please give me a hint. Code snippet is warmly welcomed. Thanks.

2 Answers 2

0

Not sure how to implement Add, Delete methods

They are already implemented in RepositoryBase.

Sign up to request clarification or add additional context in comments.

2 Comments

Hey, how stupid I am...Do I have to add SaveChanges() in the methods?
That depends on how you handle Units of Work. An outer scope may control the DbContext lifecycle, transactions and SaveChanges().
0

Your CatRepository inherits from your generic RepositoryBase which has its generic parameter set to your Cat domain entities. Your Add and Delete is already implemented in your RepositoryBase class.

The purpose of generic repository is to have common logic grouped together, like Add(), Delete(), AddRange(), DeleteRange(), and the purpose of your CatRepository is to have extremely specific implementation like GetNaughtiestCat() method. If you don't have these implementations, you could still use the GenericRepository with the generic parameter set to Cat, you need to remove the abstract keyword.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.