Repository模式(转载)
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                Repository模式(转载)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.                        
                                
                            
                            
                            近來(lái)發(fā)現(xiàn)很多ASP.NET MVC的例子中都使用了Repository模式,比如Oxite,ScottGu最近發(fā)布的免費(fèi)的ASP.NET MVC教程都使用了該模式。就簡(jiǎn)單看了下。
在《企業(yè)架構(gòu)模式》中,譯者將Repository翻譯為資源庫(kù)。給出如下說(shuō)明:
通過(guò)用來(lái)訪問(wèn)領(lǐng)域?qū)ο蟮囊粋€(gè)類似集合的接口,在領(lǐng)域與數(shù)據(jù)映射層之間進(jìn)行協(xié)調(diào)。
在《領(lǐng)域驅(qū)動(dòng)設(shè)計(jì):軟件核心復(fù)雜性應(yīng)對(duì)之道》中,譯者將Repository翻譯為倉(cāng)儲(chǔ),給出如下說(shuō)明:
一種用來(lái)封裝存儲(chǔ),讀取和查找行為的機(jī)制,它模擬了一個(gè)對(duì)象集合。
使用該模式的最大好處就是將領(lǐng)域模型從客戶代碼和數(shù)據(jù)映射層之間解耦出來(lái)。
我們來(lái)看下在LinqToSql中如何應(yīng)用該模式。
1. 我們將對(duì)實(shí)體的公共操作部分,提取為IRepository接口,比如常見(jiàn)的增加,刪除等方法。如下代碼:
interface IRepository<T> where T : class
{
??? IEnumerable<T> FindAll(Func<T, bool> exp);
??? void Add(T entity);
??? void Delete(T entity);
??? void Save();
}
2.下面我們實(shí)現(xiàn)一個(gè)泛型的類來(lái)具體實(shí)現(xiàn)上面的接口的方法。
public class Repository<T> : IRepository<T> where T : class
{
??? public DataContext context;
??? public Repository(DataContext context)
??? {
??????? this.context = context;
??? }
??? public IEnumerable<T> FindAll(Func<T, bool> exp)
??? {
??????? return context.GetTable<T>().Where(exp);
??? }
??? public void Add(T entity)
??? {
??????? context.GetTable<T>().InsertOnSubmit(entity);
??? }
??? public void Delete(T entity)
??? {
??????? context.GetTable<T>().DeleteOnSubmit(entity);
??? }
??? public void Save()
??? {
??????? context.SubmitChanges();
??? }
}
3.上面我們實(shí)現(xiàn)是每個(gè)實(shí)體公共的操作,但是實(shí)際中每個(gè)實(shí)體都有符合自己業(yè)務(wù)的邏輯。我們單獨(dú)定義另外一個(gè)接口,例如:
interface IBookRepository : IRepository<Book>
{
??? IList<Book> GetAllByBookId(int id);
}
4.最后該實(shí)體的Repository類實(shí)現(xiàn)如下:
public class BookRepository : Repository<Book>, IBookRepository
{
??? public BookRepository(DataContext dc)
??????? : base(dc)
??? { }
??? public IList<Book> GetAllByBookId(int id)
??? {
??????? var listbook = from c in context.GetTable<Book>()
?????????????????????? where c.BookId == id
?????????????????????? select c;
??????? return listbook.ToList();
??? }
}
上面只是為大家提供了一個(gè)最基本使用框架。
                        
                        
                        在《企業(yè)架構(gòu)模式》中,譯者將Repository翻譯為資源庫(kù)。給出如下說(shuō)明:
通過(guò)用來(lái)訪問(wèn)領(lǐng)域?qū)ο蟮囊粋€(gè)類似集合的接口,在領(lǐng)域與數(shù)據(jù)映射層之間進(jìn)行協(xié)調(diào)。
在《領(lǐng)域驅(qū)動(dòng)設(shè)計(jì):軟件核心復(fù)雜性應(yīng)對(duì)之道》中,譯者將Repository翻譯為倉(cāng)儲(chǔ),給出如下說(shuō)明:
一種用來(lái)封裝存儲(chǔ),讀取和查找行為的機(jī)制,它模擬了一個(gè)對(duì)象集合。
使用該模式的最大好處就是將領(lǐng)域模型從客戶代碼和數(shù)據(jù)映射層之間解耦出來(lái)。
我們來(lái)看下在LinqToSql中如何應(yīng)用該模式。
1. 我們將對(duì)實(shí)體的公共操作部分,提取為IRepository接口,比如常見(jiàn)的增加,刪除等方法。如下代碼:
interface IRepository<T> where T : class
{
??? IEnumerable<T> FindAll(Func<T, bool> exp);
??? void Add(T entity);
??? void Delete(T entity);
??? void Save();
}
2.下面我們實(shí)現(xiàn)一個(gè)泛型的類來(lái)具體實(shí)現(xiàn)上面的接口的方法。
public class Repository<T> : IRepository<T> where T : class
{
??? public DataContext context;
??? public Repository(DataContext context)
??? {
??????? this.context = context;
??? }
??? public IEnumerable<T> FindAll(Func<T, bool> exp)
??? {
??????? return context.GetTable<T>().Where(exp);
??? }
??? public void Add(T entity)
??? {
??????? context.GetTable<T>().InsertOnSubmit(entity);
??? }
??? public void Delete(T entity)
??? {
??????? context.GetTable<T>().DeleteOnSubmit(entity);
??? }
??? public void Save()
??? {
??????? context.SubmitChanges();
??? }
}
3.上面我們實(shí)現(xiàn)是每個(gè)實(shí)體公共的操作,但是實(shí)際中每個(gè)實(shí)體都有符合自己業(yè)務(wù)的邏輯。我們單獨(dú)定義另外一個(gè)接口,例如:
interface IBookRepository : IRepository<Book>
{
??? IList<Book> GetAllByBookId(int id);
}
4.最后該實(shí)體的Repository類實(shí)現(xiàn)如下:
public class BookRepository : Repository<Book>, IBookRepository
{
??? public BookRepository(DataContext dc)
??????? : base(dc)
??? { }
??? public IList<Book> GetAllByBookId(int id)
??? {
??????? var listbook = from c in context.GetTable<Book>()
?????????????????????? where c.BookId == id
?????????????????????? select c;
??????? return listbook.ToList();
??? }
}
上面只是為大家提供了一個(gè)最基本使用框架。
轉(zhuǎn)載于:https://www.cnblogs.com/sandea/p/3289942.html
總結(jié)
以上是生活随笔為你收集整理的Repository模式(转载)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
                            
                        - 上一篇: iReport中求和的问题
 - 下一篇: 顾晓斌_百度百科