I am developing an application in ASP.NET MVC3 in C#.
I am currently building my DAL composed by ADO.NET EF and a Repository class MyDBRepository:
public class MyDBRepository
{
MyDBEntities myDB;
public MinervaDBRepository()
{
myDB = new MyDBEntities();
}
//methods
}
I am still uncertain about the level of detail of the queries performed by MyDBRepository methods. I know that this query is appropriate in a repository:
public IQueryable<Products> GetAllTickets()
{
return myDB.products;
}
But what about those using joins through the navigation properties or queries using methods from other classes? Examples:
This method retrieves the products dismissed up to a certain date (date stored as string in the DB), and uses the DateUtilities class created by me:
public IQueryable<Products> GetProductsDismissed(DateTime date)
{
return myDB.products.Where(m => (string.IsNullOrEmpty(m.ProductDismissDate) ||
Equals(m.ProductDismissDate, "-")) ? false :
(DateTime.Compare(date, DateUtilities.ConvertToDateTime(m.ProductDismissDate)) > 0));
}
This method performs a join using the navigation properties to retrieve all the parts composing a product (assume there is a 1 to many relationship):
public IQueryable<Products> GetAllProductParts(int productId)
{
return myDB.products.Where(m => Equals(m.ProductId, productId));
}
Are they to be implemented in the Repository class or is it better to move (one or both) to the Service Layer?
A Repository encapsulates the persistence layer, it’s normal to implement there all the things related to the database. THe actually implementation doesn;t matter for the rest of the application, that’s why you’re using a repository in the first place. The only thing I’d suggest to change is to return directly an IEnumerable, not an IQueryable.
There is a difference between them, but in this case it doesn’t matter and using IQueryable means it’s harder to change the persistence access in the future (you maybe want to switch to a micro orm or change the RDBMS whose driver doesn’t support IQueryable or just use a cloud storage, for example).