A Layered ASP.NET MVC Application – Part II

Posted: April 12, 2009 in ASP.NET, Uncategorized
Tags: ,

Building The Repository

In my last post I had discussed about the various O/R mapping patterns and Repository is one of them.In this post we will develop a repository library for our sample application.This application is a very list of personal bookmarks with an url and description.

Step1: Create the database table

The database contains a single table called Bookmarks as shown below:

CREATE TABLE [dbo].[Bookmarks](
    [BookmarkId] [int] IDENTITY(1,1) NOT NULL,
    [Url] [varchar](100) COLLATE Latin1_General_CI_AI NOT NULL,
    [Description] [text] COLLATE Latin1_General_CI_AI NOT NULL,
    [BookmarkId] ASC

Step2: Create the Entity Model

I have created a class library named BookmarkRepository and added an ADO.NET Entity Model based on the Bookmarks table mentioned above.Along with these the BookmarkModel.edmx file there are two classes automatically generated:

  • BookmarkDBEntities inheriting from System.Data.Objects.ObjectContext. Since I don’t want any client code to directly access BookmarkDBEntities,I have marked that class a internal in the designer.cs file.
  • Bookmarks class which is the object model of Bookmarks database table.

Step3: Define the Repository interface

This interface defines all method client code requires to perform on the Bookmark repository as shown below:

public interface  IBookmarkRepository {
    Bookmarks GetBookmark(int bookmarkId);
    IEnumerable<Bookmarks> GetBookmarkList();
    void AddBookMark(Bookmarks bookmark);
    void UpdateBookmark(Bookmarks bookmark);
    void DeleteBookmark(Bookmarks bookmark);


Step4: Implement the Repository interface

This class internally uses the Entity Framework classes to Load/Save information from/to the database as shown below:

public  class BookmarkRepositoryManager:IBookmarkRepository {

      #region Member Variables
      private BookmarkDBEntities entity = null;

      #region Constructor

      public BookmarkRepositoryManager() {
          entity = new BookmarkDBEntities(); //Wrap and instantiate the ObjectContext

      #region IBookmarkRepository Members

      public Bookmarks GetBookmark(int bookmarkId) {

         //Apply LINQ get the specific bookmark based in id
          var bookmark = from b in entity.Bookmarks
                         where b.BookmarkId == bookmarkId
                         select b;
          return bookmark.FirstOrDefault();


      public IEnumerable<Bookmarks> GetBookmarkList() {
          return entity.Bookmarks.ToList();

      public void AddBookMark(Bookmarks bookmark) {
          entity.AddToBookmarks(bookmark); // Add new bookmark
          entity.SaveChanges(); //Persist changes to DB

      public void UpdateBookmark(Bookmarks bookmark) {
          Bookmarks existingBookmark = GetBookmark(bookmark.BookmarkId);
          entity.ApplyPropertyChanges(existingBookmark.EntityKey.EntitySetName, bookmark); //Update information
          entity.SaveChanges(); //Persist changes to DB

      public void DeleteBookmark(Bookmarks bookmark) {
          Bookmarks existingBookmark = GetBookmark(bookmark.BookmarkId);
          entity.DeleteObject(existingBookmark); //Remove the object
          entity.SaveChanges(); //Persist changes to DB


So our repository class library is complete.Note one thing.When we have added the Entity Model VS2008 has added a App.config in the project.This config contains the database connection string.This needs to be moved to the config of the appropriate host application from which this library will be invoked.

As a next step we will add another layer on top, to expose this repository as a WCF service.In the next post we will continue with this sample and talk a little bit about Service Interface pattern as well.

  1. […] to VoteA Layered ASP.NET MVC Application – Part II (4/12/2009)Sunday, April 12, 2009 from sankarsan.wordpress.comNET Entity Model based on the Bookmarks table […]

  2. […] Entity Framework. In the first part, he presents the options for the O/R layer isolation. In the second part, he creates the repository. In the third part,  he creates the service layer. In the fourth […]

  3. Chris Holmes says:

    Your interface seems less than ideal. You’re going to have to create a completely different interface for every domain object that you want to create a Repository for. That’s a lot of unnecessary work. Also, your method name does not match what is returned. GetBookmarkList() does not actually return a list – it returns an enumerable.

    Here is an interface I use for my Repositories:

    public interface IRepository
    T Find(int id);
    IList FindAll();
    void Save(T item);
    void Delete(T item);

    As you can see, it’s generic. I can use this same interface for all my Repositories. Save is smart – with an OR/M like NHibernate, it knows whether the object is new or not. There’s no need for extraneous methods like AddSomething or UpdateSomthing. A save will do whichever is most appropriate.

  4. مهدی says:

    Some of your points are right like using Generics (since .Net 1.1 is actually dead now) and returning an IList instead of an IEnumerable, but there is, indeed, a necessity to have Insert, Update and Delete. There are times in your code where you’d want to exactly INSERT something and you don’t want to Insert/Update that thing; That’s when a clean insert method that throws exceptions comes handy…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s