Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic Repository add custom method

I'm trying to create a library with a Generic Repository for later use in my MVC App. The code is as follows...

public abstract class BaseEntity
{
}

public interface IEntity<T>
{
    T Id { get; set; }
}

public abstract class Entity<T> : BaseEntity, IEntity<T>
{
    public virtual T Id { get; set; }
}

public interface IAuditableEntity
{
    int? UsuarioId { get; set; }
    DateTime CreatedDate { get; set; }
    string CreatedBy { get; set; }
    DateTime UpdatedDate { get; set; }
    string UpdatedBy { get; set; }
}

public abstract class AuditableEntity<T> : Entity<T>, IAuditableEntity
{
    public int? UsuarioId { get; set; }
    public DateTime CreatedDate { get; set; }
    public string CreatedBy { get; set; }
    public DateTime UpdatedDate { get; set; }
    public string UpdatedBy { get; set; }
}   

public interface IGenericRepository<T> where T : BaseEntity
{
    IEnumerable<T> GetAll();
    IEnumerable<T> GetByUsuarioId(int usuarioId);
    T GetById(int id);
    T Add(T entity);
    T Delete(T entity);
    void Edit(T entity);
    void Save();
}

public class GenericRepository<T> : IGenericRepository<T>
   where T : BaseEntity
{
    protected DbContext _entities;
    protected readonly IDbSet<T> _dbset;

    public GenericRepository(DbContext context)
    {
        _entities = context;
        _dbset = context.Set<T>();
    }

    public virtual IEnumerable<T> GetAll()
    {
        return _dbset.AsEnumerable<T>();
    }

    public IEnumerable<T> FindBy(Expression<Func<T, bool>> predicate)
    {
        IEnumerable<T> query = _dbset.Where(predicate).AsEnumerable();
        return query;
    }


    public virtual IEnumerable<T> GetByUsuarioId(int usuarioId)
    {
        // NO SÉ COMO IMPLEMENTAR ESTE METODO!!!!!
        return null;
        // NO SÉ COMO IMPLEMENTAR ESTE METODO!!!!!
    }

    public virtual T GetById(int id)
    {
        return _dbset.Find(id);
    }

    public virtual T Add(T entity)
    {
        return _dbset.Add(entity);
    }

    public virtual T Delete(T entity)
    {
        return _dbset.Remove(entity);
    }

    public virtual void Edit(T entity)
    {
        _entities.Entry(entity).State = EntityState.Modified;
    }

    public virtual void Save()
    {
        _entities.SaveChanges();
    }
}

And these are some of my classes POCO...

   public class Documento : AuditableEntity<int>
{
    public string Descripcion { get; set; }        
    public string Foto { get; set; }

    public virtual Usuario Usuario { get; set; }
}

public class Gasto : AuditableEntity<int>
{
    public int? TaxiId { get; set; }        
    public int TipoGastoId { get; set; }
    public DateTime Fecha { get; set; }
    public double Importe { get; set; }
    public int Kilometros { get; set; }
    public string Descripcion { get; set; }
    public string Foto { get; set; }

    public virtual Usuario Usuario { get; set; }
    public virtual Taxi Taxi { get; set; }
    public virtual TipoGasto TipoGasto { get; set; }
    public virtual PartidaTarjetas PartidaTarjetas { get; set; }

    public virtual ICollection<Tarea> Tareas { get; set; }      

    public int? PartidaTarjetasId { get; set; }

    public Gasto()
    {
        Tareas = new List<Tarea>();
    }
}   

public class Nivel : Entity<int>
{
    public string Descripcion { get; set; }
    public string PaginaInicio { get; set; }

    public virtual ICollection<Usuario> Usuarios { get; set; }

    public Nivel()
    {
        Usuarios = new List<Usuario>();
    }
}   

My problem is that it will not implement the method...

    public virtual IEnumerable<T> GetByUsuarioId(int usuarioId)
    {
        // NO SÉ COMO IMPLEMENTAR ESTE METODO!!!!!
        return null;
      // NO SÉ COMO IMPLEMENTAR ESTE METODO!!!!!
    }

It is a generic method which should only return results when is IAuditable type..., must be something as well...

    public virtual IEnumerable<T> GetByUsuarioId(int usuarioId)
    {
        return _dbset.FindBy(c => c.UsuarioId == usuarioId);
    }

Can you help me?, Thanks.

Hello C Bauer ...

Note that ...

public abstract class BaseEntity
{
}

public interface IEntity<T>
{
    T Id { get; set; }
}

public abstract class Entity<T> : BaseEntity, IEntity<T>
{
    public virtual T Id { get; set; }
}

public interface IAuditableEntity
{
    int? UsuarioId { get; set; }
    DateTime CreatedDate { get; set; }
    string CreatedBy { get; set; }
    DateTime UpdatedDate { get; set; }
    string UpdatedBy { get; set; }
}

public abstract class AuditableEntity<T> : Entity<T>, IAuditableEntity
{
    public int? UsuarioId { get; set; }
    public DateTime CreatedDate { get; set; }
    public string CreatedBy { get; set; }
    public DateTime UpdatedDate { get; set; }
    public string UpdatedBy { get; set; }
}   

And my GenericRepositry is ..

public interface IGenericRepository<T> where T : BaseEntity

Then now ¿?

public interface IGenericRepository<T> where T : ¿¿ ??
like image 576
Jesús Sánchez Avatar asked Nov 01 '22 12:11

Jesús Sánchez


1 Answers

You can implement a typed repository extension class so that it only works on IRepository<IAuditable>:

    public static class Extensions
    {
        public static IEnumerable<IAuditable> GetUsuarioById(this IRepository<IAuditable> repository, int id)
        {
            return repository.FindBy(audible => audible.Id == id);
        }
    }

Edit:

using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace StackOverflowPlayground
{
    class JesusPlayground
    {
        public JesusPlayground()
        {
            var auditableRepo = new AuditableRepo();
            auditableRepo.GetUsuarioById(1);

            var otherRepo = new OtherRepo();
            //otherRepo. (does not have GetUsuarioById


            var auditableRepoNotUsingTheActualClass = new GenericRepository<IAuditable>();
            auditableRepoNotUsingTheActualClass.GetUsuarioById(1); //still works!
        }
    }


    public static class Extensions
    {
        public static IEnumerable<IAuditable> GetUsuarioById(this IRepository<IAuditable> repository, int id)
        {
            return repository.FindBy(audible => audible.Id == id);
        }
    }



    public class OtherRepo : IRepository<OtherType>
    {
        public IEnumerable<OtherType> FindBy(Expression<Func<OtherType, bool>> expr)
        {
            throw new NotImplementedException();
        }
    }

    public class OtherType
    {
    }

    public interface IAuditable
    {
        int Id { get; set; }
    }

    public interface IRepository<T>
    {
        IEnumerable<T> FindBy(Expression<Func<T, bool>> expr);
    }

    public class GenericRepository<T> : IRepository<T>
    {
        public IEnumerable<T> FindBy(Expression<Func<T, bool>> expr)
        {
            throw new NotImplementedException();
        }
    }
    class AuditableRepo : IRepository<IAuditable>
    {
        public IEnumerable<IAuditable> FindBy(Expression<Func<IAuditable, bool>> expr)
        {
            throw new NotImplementedException();
        }
    }
}
like image 184
C Bauer Avatar answered Nov 26 '22 17:11

C Bauer