I am developing an application based on Repository design pattern. What should be the return type from the Repository?
This is my UserRepository
class:
public class UserRepository : Repository<UserMaster>, IUserRepository
{
public UserRepository(DbContext context)
: base(context)
{
}
public UserMaster GetById(int id)
{
return FindBy(x => x.Userid == id).FirstOrDefault();
}
}
The base Repository
class:
public abstract class Repository<T> : IRepository<T>
where T : class
{
protected DbContext _entities;
protected readonly IDbSet<T> _dbset;
public Repository(DbContext context)
{
_entities = context;
_dbset = context.Set<T>();
}
public virtual IQueryable<T> GetAll()
{
return _dbset.AsQueryable<T>();
}
public IQueryable<T> FindBy(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
{
IQueryable<T> query = _dbset.Where(predicate).AsQueryable();
return query;
}
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 = System.Data.Entity.EntityState.Modified;
}
public virtual void Save()
{
_entities.SaveChanges();
}
}
Is this the right way to return IQueryable
from the repository? If I want to join two tables, where should I implement the join logic? Inside or outside the repository?
Currently my return type is IQueryable
from the Repository and I am doing join on the Business layer after calling GetAll()
function (returns IQueryable
) of the required tables. Is this the right approach?
If i want to join two tables, where should i implement the join logic?
has been asked before. \$\endgroup\$.SingleOrDefault()
rather than.FirstOrDefault()
. \$\endgroup\$