share
Stack OverflowRepository pattern tutorial in C#
[+207] [9] Dragan Panjkov
[2008-08-06 08:40:41]
[ c# patterns repository-pattern ]
[ http://stackoverflow.com/questions/3175/repository-pattern-tutorial-in-c ] [DELETED]

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

For a few resources about testing repositories, see: stackoverflow.com/questions/4313985/… - alexandrul
(68) Why is this guy Kev closing everything that has to do with learning repository pattern?? - Shawn Mclean
(1) Shawn, I think if you want an answer, you'll need to ask on meta stackoverflow. I've challenged the aggressive closure policy there (w/o success). - Keith Hoffman
[+42] [2008-08-07 11:22:44] Andrew Peters

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

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

[1] http://www.jnsk.se/adddp/
[2] http://www.mindscape.co.nz/blog/index.php/2008/05/12/using-the-unit-of-work-per-request-pattern-in-aspnet-mvc/

(9) I do not think I have ever seen a more imposing and annoying flying div than on that blog post. Wow! - Travis J
Could you fix your links please? I'm getting 404 for both. - Dariusz
1
[+26] [2008-09-02 21:38:19] Hrvoje

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:)


(12) I agree, it's very good work. See mikehadlow.blogspot.com/2008/03/… and the full source at code.google.com/p/sutekishop/source/browse/#svn/trunk/… - Robert Claypool
2
[+10] [2010-01-28 05:42:06] james

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 [1]

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 [2] Repository (very extensive)

  2. CommonLibrary.NET [3] Repository (light-weight and effective)

  3. Sharp Architechture [4] Repository (good, generic and NHibernate implementation)

[1] http://commonlibrarynet.codeplex.com/
[2] http://ayende.com/blog/2527/rhino-commons-repository-t-and-unit-of-work
[3] http://commonlibrarynet.codeplex.com/
[4] http://www.sharparchitecture.net/downloads.htm

3
[+5] [2010-02-18 04:20:47] James Jones

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 [1] 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.)

[1] http://code.google.com/p/nhibernate-repository-example/

James, I am writing since you are the owner of the example. I new to this and your example was really easy to make. But, How do you mapping the table and the felds to the objects? - Naor
That's all taken care of via FluentNHibernate. wiki.fluentnhibernate.org/Fluent_mapping - James Jones
4
[+4] [2008-09-10 16:03:07] Colin Jack

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.


5
[+3] [2008-08-06 14:43:37] svrist

I'm not sure one exists. I assume you've looke at the Martin Fowler EA description? [1]. 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.

[1] http://martinfowler.com/eaaCatalog/repository.html

Nice slideshare for PoEAA - slideshare.net/pagsousa/poeaa-by-example - Murali Bala
6
[+2] [2008-08-24 04:16:59] Aaron Sanders

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


I (starter of this thread) converted that code. I'd like to have realization of Repository based on Generics - Dragan Panjkov
7
[+2] [2011-11-16 11:15:47] NicoJuicy

This is how i learned it:

CodePlex - project [1]

What i also learned was: IoC containers, ... It's a very good project, stripped in layers and it already has a renewed (more advanced) version. I'd suggest to use the previous version as a start and then upgrade your skill.

[1] http://efmvc.codeplex.com/SourceControl/list/changesets

8
[+1] [2011-04-08 06:06:11] L.W.C. Nirosh

Rocket Framework for windows form is one of the best and it is unique and most itelligant way of applying the repository pattern..

It has a one generic repository for all the entities.. that is something I have never seen before..

You may find the source code of it here..

http://rocketframework.codeplex.com/

Rocket @ CodePlex [1]

[1] http://rocketframework.codeplex.com/

9