Implementing repository pattern in ado.net entity model

No.of Views1884
Bookmarked0 times
Downloads 
Votes0
By  jalpesh   On  04 Jun 2010 10:06:13
Tag : ADO.NET Entity Framework , General
There are lots of ways to implement repository pattern but with ado.net entity model you can create repository pattern within 10 to 15 minutes.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

There are lots of ways to implement repository pattern but with ado.net entity model you can create repository pattern within 10 to 15 minutes.If you are creating asp.net mvc application then just right click->Model folder->Add->Net Item->select ado.net entity data model. 

Image Loading

From database explorer right click and drag your tables to your database explorer. It will create a entity class. For example i have created a notes table for my sample and it will create a notes entity class in my ado.net entity data model. 

Image Loading

After creating the entity class not its time to create a interface for repository pattern which will contain my all the operations related to notes entity.

Following is the interface which we will implement use for repository.
 

namespace DotNetJapsMVC.Models.Respository.Interface
{interface INotesRepository
{void CreateNotes(Notes notesToCreate);void EditNotes(Notes notesToEdit);void DeleteNotes(Notes notesToDelete);Notes GetNotes(Guid noteId);IEnumerable ListNotes(); 
}}

 Now we will create a class that will implement this repository interface. Here is the coding for that. 

using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace DotNetJapsMVC.Models.Respository.Class
{public class NotesRepository:Interface.INotesRepository 
{private DotNetJapsEntities _myEntities = new DotNetJapsEntities();public void CreateNotes(Notes notesToCreate){_myEntities.AddToNotes(notesToCreate);_myEntities.SaveChanges(); 
}public void EditNotes(Notes notesToEdit){var originalNotes = (from n in _myEntities.Notes
where n.NoteId == notesToEdit.NoteId
select n).FirstOrDefault();_myEntities.ApplyPropertyChanges(originalNotes.EntityKey.EntitySetName, notesToEdit);_myEntities.SaveChanges();}public void DeleteNotes(Notes notesToDelete){var originalNotes = GetNotes(notesToDelete.NoteId);_myEntities.DeleteObject(originalNotes );_myEntities.SaveChanges();}public Notes GetNotes(Guid noteId){return (from n in _myEntities.Notes
where n.NoteId == noteId
select n ).FirstOrDefault();}public IEnumerable ListNotes(){return _myEntities.Notes.ToList(); 
}}}

That's it we have created the repository classes and repository interface. You can use as following in your class or pages. 

private  INotesRepository _repository;public List LoadNotes(){return _repository.ListNotes();}

Advantage Of Repository Pattern

  • Make application loosely coupled so if you want to change the something then you don't need to change everything from scratch.
  • With the repository pattern we can have nice abstraction which will separate our business logic as well as database logic.
  • You can prevent dependency injection through the repository pattern.

Happy Programming and enjoy

 
Sign Up to vote for this article
 
About Author
 
jalpesh
Occupation-Software Engineer
Company-DotNetJaps
Member Type-Expert
Location-India
Joined date-08 May 2010
Home Page-http://www.dotnetjalps.com
Blog Page-http://www.dotnetjalps.com
I am jalpesh vadgamaa an Microsoft MVP for Visual C# and BrainBench Certified ASP.NET Developer having experience of five year in Microsoft .NET Technology.I am working as Project Leader in Mid Size company.My work area comprises of Enterprise Level projects using ASP.NET and other Microsoft .NET Technologies.Please feel free to contact me for any queries via posting comments on my blog I will try to reply as early as possible.
 
 
Other popularSectionarticles
    This article will cover the code-only approach, which provides developers the ability to use the Entity Framework using POCO entities and without an EDMX file. This approach lets developers view code as their model.
    Published Date : 20/Jul/2010
    In Model first approach, we will be creating the EDM, conceptual model, first with respect to the business, and keep evolute the EDM till it fits for the business and then generate the database schema based on the EDM. At least it makes easier to limit the huge changes.
    Published Date : 20/Jul/2010
    In this article, I'm going to explain how to handle Output Parameter in Stored Procedure with Entiry Framework.The Entity Framework is getting popular and many developers love it.
    Published Date : 11/Sep/2011
    Microsoft .net framework 4 have usefull enhancements in there Data-services model After the WCf For quite long Microsoft .net Providing such useful services fro client and server side features One of Most Beautful enhancements
    Published Date : 16/May/2010
    Introduction to ADO.NET Data service
    Published Date : 16/Feb/2010
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top