views:

16418

answers:

8

Can anyone recommend good tutorial on repository pattern usage, in C#?

+2  A: 

I'm not sure one exists. I assume you've looke at the Martin Fowler EA description?. If you have the book, it's very explicit about all the patterns and quite good. In my oppinion :)

Maybe this would be nice opportunity to make one here on this site.

svrist
Repository isn't a GoF pattern, but it is described in PoEAA.
JasonTrue
Indeed. I fixed the description
svrist
+12  A: 

A good place is the book Applying Domain-Driven Design and Patterns by Jimmy Nilsson

My blog post: Using the unit of work-per-request pattern in ASP.NET MVC also details a Repository implementation in C#.

Andrew Peters
+1  A: 

This video http://www.asp.net/learn/mvc-videos/video-403.aspx goes into it some. This video is in VB, but someone converted into C#; and the code is here http://panjkov.qsh.eu/files/folders/aspnetmvc/entry69.aspx

Aaron Sanders
I (starter of this thread) converted that code. I'd like to have realization of Repository based on Generics
Dragan Panjkov
+6  A: 

I'm using one from Mike Hadlow (http://mikehadlow.blogspot.com/)
With LINQ and some IoC, it's really joy to programm...
Also, it's easy to make fake repository based on mike's interface and do some TDD:)

Hrvoje
I agree, it's very good work.See http://mikehadlow.blogspot.com/2008/03/using-irepository-pattern-with-linq-to.html and the full source at http://code.google.com/p/sutekishop/source/browse/#svn/trunk/Suteki.Shop/Suteki.Common/Repositories
Robert Claypool
+3  A: 

This post describes the approach I use:

http://colinjack.blogspot.com/2007/11/repository-implementation-dddnhibernate.html

I'd be careful about expecting to be able to copy/paste a solution though, in my experience with repositories its better to evolve your own implementation.

+1  A: 

Here is an article describing an implementation of the repository pattern using Linq to SQL. The full code is open source, available @ github.

http://www.codevil.com/index.php/2009/07/02/the-repository-pattern/

MACSkeptic
+2  A: 

The repository pattern is fairly straight forward, but transactions, and associations(has one, has many) can make it more complex/advanced.

Here is a simple/effective implementation i'm using from CommonLibrary.NET

It supports the following:

1.Create, Retrieve, Update, Delete, GetAll, DeleteAll, FindByQuery, GetPage, etc methods

2.RepositorySql base class, just implement Create/Update & use conventions e.g. Id

3.It also has an In-Memory Repository implementation. Very useful for unit-tests testing.

However, to be objective, here is a list of all I'm aware / heard of:

1.Rhino.Commons Repository ( very extensive )

2.CommonLibrary.NET Repository ( light-weight / effective )

3.Sharp Architechture Repository ( good, generic & NHibernate implementation )

james
+2  A: 

I feel that NHibernate is the best ORM to implement the repository pattern with, particularly because there is very little code involved and also because it allows absolutely zero coupling with your domain model. There is a great example on code.google that depicts the proper way to implement the Repository pattern using NHibernate (with FluentNHibernate).

(Full disclosure: I am the owner of said code.google project.)

James Jones