Follow me to learn what is repository pattern
Introduction
Creating a generic repository pattern in an mvc application with entity framework is the first topic that we are about to cover in our journey of learning my mvc template.
this article will focus on repository pattern and shows how to design repository interface when there could be a possibility of creating more than one repository class. To overcome this possibility and overhead, we make a generic repository class for all other repositories.

- What is repository pattern?
- Why should we use it?
- What is the difference between repository and generic repository?
A repository basically works as a mediator between our business logic layer and our data access layer of the application.
Road Map
Part1:Follow me to learn how to use mvc template
Part2:Follow me to learn what is repository pattern
Part3:......
Issue:
Expose the data access mechanism directly to business logic layer.
- it may result in redundant code for accessing data.
- it may result in a code that is hard to test or understand.
To overcome these kinds of issues, and to write an Interface driven and test driven code to access data, we use Repository Pattern. The repository makes queries to the data source for the data, and then maps the data from the data source to a business domain object, finally and persists the changes in the business entity to the data source. The separation between the data and business tiers has three benefits:
- It centralizes the data logic.
- It provides a substitution point for the unit tests.
- It provides a flexible architecture.
If you call the Entity Framework class object in the controller class for accessing the entity classes, now we can say that system is somewhat a tightly coupled system. To overcome this situation, as we discussed, we’ll implement Repository Pattern.
Coupling

According interface hide implementation details

Repository Interface
Now,let us to see our repository interface:
public interface IRepository<T> where T : class, new()
{
T Update(T entity, Expression<Func<T, bool>> filter, bool IsCommit = true); T Insert(T entity, bool IsCommit = true); void Delete(T entity, bool IsCommit = true); void Delete(Expression<Func<T, bool>> filter); IQueryable<T> Query(Expression<Func<T, bool>> filter); IList<T> QueryByPage<TKey>(Expression<Func<T, bool>> filter,
Expression<Func<T, TKey>> orderBy,int orderType, int pageSize, int pageIndex, out int recordsCount); IList<T> QueryByPage(Expression<Func<T, bool>> filter,
string orderBy, int pageSize, int pageIndex, out int recordsCount); T GetSingleOrDefault(Expression<Func<T, bool>> filter); IList<T> FindAll();
}
After when you look at the above interface,you may have a question:what is IsCommt?
I will tell you in the next article.
Quesion:
Why the repository interface is a generic interface?
- Redundant code

- Save time

Note:
refer to:http://www.codeproject.com/Articles/631668/Learning-MVC-Part-5-Repository-Pattern-in-MVC3-App
Follow me to learn what is repository pattern的更多相关文章
- Follow me to learn what is Unit of Work pattern
Introduction A Unit of Work is a combination of several actions that will be grouped into a transact ...
- Follow me to learn how to use mvc template
Introduction After having gone through many project: Project A Project B Project C I start to write ...
- How To Use The Repository Pattern In Laravel
The Repository Pattern in Laravel is a very useful pattern with a couple of great uses. The first us ...
- Laravel与Repository Pattern(仓库模式)
为什么要学习Repository Pattern(仓库模式) Repository 模式主要思想是建立一个数据操作代理层,把controller里的数据操作剥离出来,这样做有几个好处: 把数据处理逻辑 ...
- Generic repository pattern and Unit of work with Entity framework
原文 Generic repository pattern and Unit of work with Entity framework Repository pattern is an abstra ...
- Using the Repository Pattern with ASP.NET MVC and Entity Framework
原文:http://www.codeguru.com/csharp/.net/net_asp/mvc/using-the-repository-pattern-with-asp.net-mvc-and ...
- Using Repository Pattern in Entity Framework
One of the most common pattern is followed in the world of Entity Framework is “Repository Pattern”. ...
- 学习笔记之ASP.NET MVC & MVVM & The Repository Pattern
ASP.NET MVC | The ASP.NET Site https://www.asp.net/mvc ASP.NET MVC gives you a powerful, patterns-ba ...
- [转]Using the Repository Pattern with ASP.NET MVC and Entity Framework
本文转自:http://www.codeguru.com/csharp/.net/net_asp/mvc/using-the-repository-pattern-with-asp.net-mvc-a ...
随机推荐
- 从今天起,记录CEF使用开发心得经验
已经使用CEF来呈现桌面程序界面大半年了,从来没有写过相关博文.发现网上的中文资料甚至英文已经无法满足我的开发需求,不少问题只得自己探索.在此先谢过网络上各位CEF使用开发博文的贡献者,没有你们我也难 ...
- CSS - Tooltip-arrow 绘制三角形
问题:纯CSS实现bubble的三角形部分 方法:使用border来绘制三角形:例如 .trangle { ; border-color: transparent; border-style: sol ...
- 部署tomcat在windows服务器下,将tomcat控制台日志记录到日志文件中
在Linux系统中,Tomcat 启动后默认将很多信息都写入到 catalina.out 文件中,我们可以通过tail -f catalina.out 来跟踪Tomcat 和相关应用运行的情况. ...
- mem_fun 例子
// functional_mem_fun.cpp // compile with: /EHsc #include <vector> #include <functional> ...
- 使用PowerDesigner导出Word/HTML的一些配置
目标:根据物理视图导出代码和列清单(包括字段.类型.注释).由于默认导出的要素太多,大多不是自己想要的,这里简单说下导出目标的一些配置,留下来备用. 其中,Title中的图和Table表格中的代码预览 ...
- Unity3d游戏中自定义贝塞尔曲线编辑器[转]
关于贝塞尔曲线曲线我们再前面的文章提到过<Unity 教程之-在Unity3d中使用贝塞尔曲线>,那么本篇文章我们来深入学习下,并自定义实现贝塞尔曲线编辑器,贝塞尔曲线是最基本的曲线,一般 ...
- 查询修改linux 打开文件句柄数量
查询系统支持最大可打开文件句柄数量: #vi /proc/sys/fs/file-max 查询当前连接用户最大可打开文件句柄数量: #ulimit -a 修改当前连接用户最大可打开文件句柄数量: #u ...
- 华为 HG8245C 光猫 修改无线用户数限制+hw_ctree.xml 文件解密
这个操作方法是 从网上其他人提供的方法 和我一个朋友总结出来的,我只是负责整理,并实地在我自己的光猫上操作成功了 风险提示 :刷机有风险,操作需谨慎, 备份备份备份! 俺家,俺的新家是电信光纤接入, ...
- 同程旅游网开放平台SDK开发完成
最近利用业余时间,使用了同程旅游网的开放平台,并对大部分的方法进行了调用,发现有很多不好用的地方,比如 1.同一个开放平台居然有几个调用地址,景区调用http://tcopenapi.17usoft. ...
- java攻城狮之路(Android篇)--ListView与ContentProvider
一.ListView 1.三种Adapter构建ListView ListView添加条目的时候, 可以使用setAdapter(ListAdapter)方法, 常用的ListAdapter有三种 B ...