ARTICLE

Simple And Best Way of Implementing the Repository Pattern

Posted by Aravind BS Articles | Design & Architecture January 02, 2012
I will try to explain in a very simplest method to understand repository pattern.
Reader Level:


What is Repository Pattern?

I have seen so many ways to write repository pattern. I have read Rob Conery's blog also.

But, according to my experience, the repository pattern is the one constructing criteria by client code and passing it to repository.

A repository is nothing but a mediator between data access layer and business layer.

I will try to explain in a very simplest method to understand repository pattern.

Step 1:

Create VS 2010 project and name it RepPattern.

I have created here a Silverlight application. Because I really like to work with Silverlight.

RepoPtrn1.jpg


In this application, I have added two folders.

  1. Contract:

    To hold all interface entity.

    That is IPattern interface.
     
  2. Service:

    To hold all interface implemented class.

    That is PatternService class.

Step 2:

In IPattern interface, I have declared signature to get some value. I. e Get default name.

At the same time I have implemented IPattern interface on PatternService class as given below.


RepoPtrn2.jpg

These modules belongs to the repository.

Step 3:

Now, it's the time to actually implement this repository.

Here I have added one more class called PatternHelper.cs. Also, I have written one static method to return all repository methods.


RepoPtrn3.jpg
Step 4:

Next one is calling these repository methods from client.

RepoPtrn4.jpg

Step 5:

Run the application.

RepoPtrn5.jpg

As I said before, repository is nothing but a mediator between business logic and data access layer.

Here, PatternHelper works as repository layer.

These separation of business logic and data helps in three ways.
  • It centralizes the data logic or Web service access logic.
  • It provides a substitution point for the unit tests.
  • It provides a flexible architecture that can be adapted as the overall design of the application evolves.

Usage of Repository pattern:
  1. We can use this pattern to access a data source from many locations yet centrally manage the data source.
  2. We can test it individually (Unit test).
  3. Code maintainability. 


Thanks
Enjoy the coding!!!!!

Login to add your contents and source code to this article
post comment
     

Very useful article.

Posted by Abhi Kumar Jan 04, 2012

Very Good concept of your article.

Posted by Stephen Johnson Jan 04, 2012

nice article

Posted by Emmy Dickens Jan 04, 2012

great job done.

Posted by Sonakshi Singh Jan 04, 2012

Nicely written article and helpful too

Posted by Akash Ahlawat Jan 04, 2012
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.