Brian David Berman: Technical Blog

Super Simple Repository Pattern Example

Posted in ASP.NET, ASP.NET MVC by briandberman on May 7, 2009

After reading Chapter 1 of Professional ASP.NET MVC 1.0, I got a nice introduction to the “repository” pattern.  Using a repository helps us separate concerns, which is a big part of (but not limited to) ASP.NET MVC.  Let’s start off really simple by looking at a table (called Employees) I created and dragged into the LINQ to SQL Designer in Visual Studio 2008:

image

Once we save our .dbml file, Visual Studio 2008 creates an “Employee” class that exposes the available properties, allowing us to persist data back to our database.  A simple way to add a new record to this table is as follows:

 1: CompanyDataContext companyDataContext = new CompanyDataContext();
 2:
 3: Employee employee = new Employee();
 4:
 5: employee.LastName = "Berman";
 6: employee.FirstName = "Brian";
 7: employee.JobTitle = "Software Engineer";
 8: employee.Extension = "1234";
 9: employee.HireDate = DateTime.Parse("01/01/2006");
 10:
 11: companyDataContext.Employees.InsertOnSubmit(employee);
 12: companyDataContext.SubmitChanges();

The above produces the following result in the database:

image

While this may be a fine way to update a table, it is difficult to test and requires mention of the data context, as well as data storage implementation (LINQ to SQL) methods.  To fix this, we can add a “repository” layer:

 1: public class EmployeeRepository
 2: {
 3:     CompanyDataContext companyDataContext = new CompanyDataContext();
 4:
 5:     public void Add(Employee employee)
 6:     {
 7:         companyDataContext.Employees.InsertOnSubmit(employee);
 8:     }
 9:
 10:     public void Save()
 11:     {
 12:         companyDataContext.SubmitChanges();
 13:     }
 14: }

The repository class contains the data storage implementation activity, including the data context and methods.  We can then change our original code to the following:

 1: EmployeeRepository employeeRepository = new EmployeeRepository();
 2:
 3: Employee employee = new Employee();
 4:
 5: employee.LastName = "Yandle";
 6: employee.FirstName = "Justine";
 7: employee.JobTitle = "Teacher";
 8: employee.Extension = "5678";
 9: employee.HireDate = DateTime.Parse("01/01/2008");
 10:
 11: employeeRepository.Add(employee);
 12: employeeRepository.Save();

While this example doesn’t use less code, you will notice the “insert” and “save” functionality is handled through the database repository methods Add() and Save().  The result of running this code (along with our original code) produces the following in the database:

image

This allows our code to be more testable since we could now write unit tests against our objects without a database (mock objects).  Although less likely, we are also able to swap out our data storage implementation (e.g. going from LINQ to SQL to LINQ to Entities) at a later time in a much smoother fashion.  Some other example methods within the EmployeeRepository class are as follows:

 1: public class EmployeeRepository
 2:     {
 3:         CompanyDataContext companyDataContext = new CompanyDataContext();
 4:
 5:         public IQueryable<Employee> FindAllEmployees()
 6:         {
 7:             return companyDataContext.Employees;
 8:         }
 9:
 10:         public Employee GetEmployee(int id)
 11:         {
 12:             return companyDataContext.Employees.SingleOrDefault(e => e.EmployeeId == id);
 13:         }
 14:
 15:         public void Add(Employee employee)
 16:         {
 17:             companyDataContext.Employees.InsertOnSubmit(employee);
 18:         }
 19:
 20:         public void Save()
 21:         {
 22:             companyDataContext.SubmitChanges();
 23:         }
 24:     }

Hopefully this explains the repository pattern in the simplest way possible.  Please feel free to leave comments.  I am still learning and wouldn’t mind learning more through comments!  ;-)

Advertisement

One Response

Subscribe to comments with RSS.

  1. [...] Read the original here: Super Simple Repository Pattern Example [...]


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 )

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

Follow

Get every new post delivered to your Inbox.