SIGN UP MEMBER LOGIN:    
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
share 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
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Team Foundation Server Hosting
Become a Sponsor