Personally, I use the Repository Pattern
to return all items from the Repository as an IQueryable
. By doing this, my repository layer is now very very light, small .. with the service layer (which consumes the Repository layer) can now be open to all types of query manipulation.
Basically, all my logic now sits in the service layer (which has no idea what type of repository it will be using .. and doesn't want to know <-- separation of concerns) .. while my repository layer is just dealing with Getting data and Saving data to the repo (a sql server, a file, a satellite in space.. etc <-- more separation of concerns).
eg. More or less pseduo code as i'm remembering what we've done in our code and simplifying it for this answer...
public interface IRepository<T>
{
IQueryable<T> Find();
void Save(T entity);
void Delete(T entity);
}
and to have a user repository...
public class UserRepository : IRepository<User>
{
public IQueryable<User> Find()
{
// Context is some Entity Framework context or
// Linq-to-Sql or NHib or an Xml file, etc...
// I didn't bother adding this, to this example code.
return context.Users().AsQueryable();
}
// ... etc
}
and now for the best bit :)
public void UserServices : IUserServices
{
private readonly IRepository<User> _userRepository;
public UserServices(IRepository<User> userRepository)
{
_userRepository = userRepository;
}
public User FindById(int userId)
{
return _userRepository.Find()
.WithUserId(userId)
.SingleOrDefault(); // <-- This will be null, if the
// user doesn't exist
// in the repository.
}
// Note: some people might not want the FindBySingle method because this
// uber method can do that, also. But i wanted to show u the power
// of having the Repository return an IQuerable.
public User FindSingle(Expression<Func<User, bool>> predicate)
{
return _userRepository
.Find()
.SingleOrDefault(predicate);
}
}
Bonus Points: WTF is WithUserId(userId)
in the FindById
method? That's a Pipe and Filter. Use them :) love them :) hug them :) They make your code SOOO much readable :) Now, if u're wanting to know what that does.. this is the extension method.
public static User WithId(this IQueryable<User> source, int userId)
{
return source.Where(u => u.UserId == userId).SingleOrDefault();
}
HTH's even though this question is .. well ... nearly two years old :)