I usually use a “Page -> Service -> Repository” pattern in my applications putting all my database calls in “repository” classes.
In some situations I’ve to test collection items count in my service logic.
Example:
EventService eventService=container.Resolve<EventService>();
IEnumerable<Event> events=eventService.GetAll();
if(events.Count()>0)
{
...do something...
}
Using NHibernate this will generate automatically a SQL statement with a DB access out of my repository class.
Can I avoid this? Is there a best practice?
I try to avoid exposing
IQueryable<T>on repositories for the following reasons:Instead I would opt for using a simple query object which is translated into whatever it needs to be by the repository.
For this case in particular I would have a method on the repository returning a paginated list of events:
This also highlights the importance of pagination. In the repository this would like something like:
You may also choose to create a wrapping object for IList which can contain the total count as well.