通过用来访问领域对象的一个类似集合的接口,在领域与数据映射层之间进行协调。
一种用来封装存储,读取和查找行为的机制,它模拟了一个对象集合。
使用该模式的最大好处就是将领域模型从客户代码和数据映射层之间解耦出来。
1. 将对实体的公共操作部分,提取为IRepository接口,比如常见的增加,删除等方法。如下代码
1 public interface IRepository<T> where T : class
{
2 void Add(T entity);
3 void Update(T entity);
4 void Delete(T entity);
5 void Delete(Expression<Func<T, bool>> where);
6 T GetById(long Id);
7 T GetById(string Id);
8 T Get(Expression<Func<T, bool>> where);
9 IEnumerable<T> GetAll();
10 IEnumerable<T> GetMany(Expression<Func<T, bool>> where);
11 IEnumerable<T> GetPage(int PageIndex, int PageSize, Expression<Func<T, bool>> where,
2 void Add(T entity);
3 void Update(T entity);
4 void Delete(T entity);
5 void Delete(Expression<Func<T, bool>> where);
6 T GetById(long Id);
7 T GetById(string Id);
8 T Get(Expression<Func<T, bool>> where);
9 IEnumerable<T> GetAll();
10 IEnumerable<T> GetMany(Expression<Func<T, bool>> where);
11 IEnumerable<T> GetPage(int PageIndex, int PageSize, Expression<Func<T, bool>> where,
Expression<Func<T, IComparable>> keySelector);
12 }
12 }
2.下面实现一个泛型的类来具体实现上面的接口的方法。
1 public abstract class RepositoryBase<T> : IRepository<T> where T : class
2 {
3 private KMobileContext dataContext;
4 private readonly IDbSet<T> dbset;
5 protected RepositoryBase(IDatabaseFactory databaseFactory) {
6 DatabaseFactory = databaseFactory;
7 dbset = DataContext.Set<T>();
8 }
9
10 protected IDatabaseFactory DatabaseFactory {
11 get;
12 private set;
13 }
14
15 protected KMobileContext DataContext
16 {
17 get { return dataContext ?? (dataContext = DatabaseFactory.Get()); }
18 }
19 public virtual void Add(T entity) {
20 dbset.Add(entity);
21 }
22 public virtual void Update(T entity) {
23 dbset.Attach(entity);
24 dataContext.Entry(entity).State = EntityState.Modified;
25 }
26 public virtual void Delete(T entity) {
27 dbset.Remove(entity);
28 }
29 public virtual void Delete(Expression<Func<T, bool>> where) {
30 IEnumerable<T> objects = dbset.Where<T>(where).AsEnumerable();
31 foreach (T obj in objects)
32 dbset.Remove(obj);
33 }
34 public virtual T GetById(long id) {
35 return dbset.Find(id);
36 }
37 public virtual T GetById(string id) {
38 return dbset.Find(id);
39 }
40 public virtual IEnumerable<T> GetAll() {
41 return dbset.ToList();
42 }
43 public virtual IEnumerable<T> GetMany(Expression<Func<T, bool>> where) {
44 return dbset.Where(where).ToList();
45 }
46 public virtual IEnumerable<T> GetPage(int PageIndex, int PageSize, Expression<Func<T, bool>> where, Expression<Func<T, IComparable>> keySelector) {
47 return dbset.Where(where).OrderBy(keySelector).Skip(PageIndex - 1).Take(PageSize).ToList();
48 }
49 public T Get(Expression<Func<T, bool>> where) {
50 return dbset.Where(where).FirstOrDefault<T>();
51 }
52 }
2 {
3 private KMobileContext dataContext;
4 private readonly IDbSet<T> dbset;
5 protected RepositoryBase(IDatabaseFactory databaseFactory) {
6 DatabaseFactory = databaseFactory;
7 dbset = DataContext.Set<T>();
8 }
9
10 protected IDatabaseFactory DatabaseFactory {
11 get;
12 private set;
13 }
14
15 protected KMobileContext DataContext
16 {
17 get { return dataContext ?? (dataContext = DatabaseFactory.Get()); }
18 }
19 public virtual void Add(T entity) {
20 dbset.Add(entity);
21 }
22 public virtual void Update(T entity) {
23 dbset.Attach(entity);
24 dataContext.Entry(entity).State = EntityState.Modified;
25 }
26 public virtual void Delete(T entity) {
27 dbset.Remove(entity);
28 }
29 public virtual void Delete(Expression<Func<T, bool>> where) {
30 IEnumerable<T> objects = dbset.Where<T>(where).AsEnumerable();
31 foreach (T obj in objects)
32 dbset.Remove(obj);
33 }
34 public virtual T GetById(long id) {
35 return dbset.Find(id);
36 }
37 public virtual T GetById(string id) {
38 return dbset.Find(id);
39 }
40 public virtual IEnumerable<T> GetAll() {
41 return dbset.ToList();
42 }
43 public virtual IEnumerable<T> GetMany(Expression<Func<T, bool>> where) {
44 return dbset.Where(where).ToList();
45 }
46 public virtual IEnumerable<T> GetPage(int PageIndex, int PageSize, Expression<Func<T, bool>> where, Expression<Func<T, IComparable>> keySelector) {
47 return dbset.Where(where).OrderBy(keySelector).Skip(PageIndex - 1).Take(PageSize).ToList();
48 }
49 public T Get(Expression<Func<T, bool>> where) {
50 return dbset.Where(where).FirstOrDefault<T>();
51 }
52 }
3.上面我们实现是每个实体公共的操作,但是实际中每个实体都有符合自己业务的逻辑。我们单独定义另外一个接口
1 interface IBookRepository : IRepository<Book>
2 {
3 IList<Book> GetAllByBookId(int id);
4 }
2 {
3 IList<Book> GetAllByBookId(int id);
4 }
4.最后该实体的Repository类实现如下:
1 public class BookRepository : Repository<Book>, IBookRepository
2 {
3 public BookRepository(DataContext dc)
4 : base(dc)
5 { }
6 public IList<Book> GetAllByBookId(int id)
7 {
8 var listbook = from c in context.GetTable<Book>()
9 where c.BookId == id
10 select c;
11 return listbook.ToList();
12 }
13 }
2 {
3 public BookRepository(DataContext dc)
4 : base(dc)
5 { }
6 public IList<Book> GetAllByBookId(int id)
7 {
8 var listbook = from c in context.GetTable<Book>()
9 where c.BookId == id
10 select c;
11 return listbook.ToList();
12 }
13 }