Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am reading the official Your first NHibernate based application.

While the tutorial is good and easy to follow, I am wondering why the Repository pattern is used.

In the various Add, Update, Remove methods in the ProductRepository implementation, the code is nearly identical - they are all using transactions, and the difference is in the "meat" i.e. call session.Save int the Add method, session.Delete in the remove method. (The page lacks HTML anchors, but you can search the page for the relevant code like public void Remove, public void Add)

That code just "feels wrong".

Why is the author using the Repository pattern - is it just for demonstration of using NHibernate or is that required or some other reason?

Ps. My background is from Ruby on Rails using ActiveRecord so I'm trying to make sense of how NHibernate works/is used.

share|improve this question

migrated from stackoverflow.com Sep 26 '11 at 6:18

This question came from our site for professional and enthusiast programmers.

1  
If you prefer the ACtive Record pattern then you can use castle Active Record to sit on top of NHibernate castleproject.org/activerecord –  Ben Robinson Sep 6 '11 at 9:04
3  
This is a critical question. There is some argueing going on whether to use it or not. Ayende wrote his arguments to not use it in Repository is the new Singleton –  Jan Selke Sep 6 '11 at 9:06
add comment

2 Answers

Advantage of using repository pattern is to mock your data access layer, so that you can test your business layer code without calling DAL code. There are other big advantages but this seems to be very vital to me.

share|improve this answer
1  
+1 the ActiveRecord pattern makes it very difficult to isolate the DAL for mocking, usually ending up with the unit tests requiring their own database (in which case the unit test becomes an integration test). –  MattDavey Sep 26 '11 at 8:57
add comment

The repository pattern is not required. As for all the other pattern is an "Architectural" decision that you have to make against your business needs. In general the repository Pattern is used to implement the "Entity Persistance Ingorance" that means that your entities don’t know anything on how to persist themselves on your storing device (Database,XML,TextFile,etc). If for example you have an entity Address it won’t contains the persistence logic (you won’t find anywhere something like address.Save or address.Update) but you will pass your entity to a repository method which is in charge to persist the changes

share|improve this answer
    
I think yes and no. The NHibernate session itself is soem kind of generic repository. So adding a additional repository is in general nothing more than adding a facade to the session object. –  Jan Selke Sep 6 '11 at 9:13
    
in fact my answer begin like "The repository pattern is not required..." it can be just an architectural decision make against the business needs that's it –  Massimiliano Peluso Sep 6 '11 at 9:15
    
I totally agree on that. But I was missing the point that session itself is a repository, that's all. –  Jan Selke Sep 6 '11 at 9:20
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.