
Chapter 9: Creating Web Applications with ASP.NET MVC | 265 |
Creating a Repository
A common pattern for working with data is to build a repository that is responsible for all
A repository is a class that performs create, read, update, and delete (CRUD) operations on a specific data type. Listing
Listing 9-6 A repository for working with customer data
C#:
using System;
using System.Collections.Generic; using System.Linq;
using System.Web;
namespace MyShopCS.Models
{
public class CustomerRepository
{
private MyShopDataContext m_ctx = new MyShopDataContext();
public int InsertCustomer(Customer cust)
{
m_ctx.Customers.InsertOnSubmit(cust); m_ctx.SubmitChanges();
return cust.CustomerID;
}
public void UpdateCustomer(Customer cust)
{
var currentCust =
(from currCust in m_ctx.Customers
where currCust.CustomerID == cust.CustomerID select currCust)
.SingleOrDefault(); if (currentCust != null)