http://www.cnblogs.com/carysun/archive/2009/03/20/Repository.html
ASP.NET MVC 官方示例动手做:联系人管理
http://onlyhujian.spaces.live.com/blog/cns!B45C4FBBFBBE1117!558.entry
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ContactManager.Models;
using System.Collections;
using System.Data.Linq;
namespace ContactManager.Controllers
{
/// <summary>
/// CRUD
/// </summary>
/// <typeparam name="T"></typeparam>
public interface IRepository<T> where T : class {
IEnumerable<T> FindAll(Func<T, bool> exp);
void Add(T entity);
void Delete(T entity);
void Save();
}
public class Repository<T> : IRepository<T> where T : class {
protected System.Data.Linq.DataContext m_context;
public Repository(DataContext context) { m_context = context; }
#region IRepository<T> Members
public virtual IEnumerable<T> FindAll(Func<T, bool> exp)
{
return m_context.GetTable<T>().Where(exp);
}
public virtual void Add(T entity)
{
m_context.GetTable<T>().InsertOnSubmit(entity);
}
public virtual void Delete(T entity)
{
m_context.GetTable<T>().DeleteOnSubmit(entity);
}
public virtual void Save()
{
m_context.SubmitChanges();
}
#endregion
}
public class ContactRepository : Repository<Contact> {
public ContactRepository():this(new ContactManagerDataClassesDataContext()) { }
public ContactRepository(DataContext context) : base(context) { }
}
}