IRepository(Of Customer) vs. CustomerRepository

Using IRepository(Of Customer):

Customer[] customersWithUnpaidOrders = repository.FindAll(@"select c from Customer c, Order o 
where o.Customer = c and o.Status = OrderStatus.Unpaid"
); 

Using CustomerRepository:

Customer[] customersWithUnpaidOrders = repository.GetCustomersWithUnpaidOrders();

The difference is the level of abstraction that is used. In the first case, the code define the question explicitly, using the generic repository to pose the question. In the second, it is the CustomerRepository responsability to get the data.

The first is much more flexible, but it is harder to test. The second is much easier to test (but require better integration testing to check that it the repository is actaully doing its job), and is more intention revealing.

Print | posted on Wednesday, August 30, 2006 10:49 AM

Feedback


Gravatar

#  8/30/2006 10:57 PM Murad

Hi Ayende,

Is there anyway I can download your respository app you wrote for nhibernate.

thanks,

-Murad


Gravatar

#  8/30/2006 11:20 PM Ayende Rahien

It is available from SVN only at the moment.
Check the previous posts in the subject for the URL.


Gravatar

#  8/31/2006 5:59 AM Ayende Rahien

Once a month? Is that blogging 

svn checkout svn://svn.berlios.de/rhino-mocks/trunk/rhino-commons


Gravatar

#  8/31/2006 7:47 PM Liang

Hi Ayende,

In the IRepository Class, you declare:
ICollection<T> FindAll(params ICriterion[] criteria);
So you already assume you are going to use NHibernate, instead of anything else, to implement repository?

Thanks!


Gravatar

#  8/31/2006 9:51 PM Ayende Rahien

Yes, I do.
I don't see a lot of reason to _not_ use it.

The IRepository interface isolate me from NHibernte API (which is generaly a good thing, since it is simpler), but I have a project where the requirement is no code depenedencies on NHibernate, and I keep finding out that I NEED to do this or that is very simple in NHib but not possible in the API we expose.

If / when I need to use another OR/M layer, it is a work of an hour to repreduce it.


Gravatar

#  8/31/2006 10:23 PM Liang

Thank you, Ayende!
Since it is a common interface, how do you thik to make it independent from any concrete implementation? So that we can have NhibernateRepository, IBatisRepository or EnterpriseLibraryRepositiry?


Gravatar

#  9/1/2006 10:05 AM Ayende Rahien

The simplest solution would be to wrap it in something like IPredicate, which could be translated to different implementations by the repository.


Gravatar

#  9/1/2006 3:23 PM Liang

Thank you, Ayende! The Rhino Common is elegant!! Very appreciate you can share this with us.

Comments have been closed on this topic.