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.

  1. What is repository pattern?
  2. Why should we use it?
  3. 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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. Laravel与Repository Pattern(仓库模式)

    为什么要学习Repository Pattern(仓库模式) Repository 模式主要思想是建立一个数据操作代理层,把controller里的数据操作剥离出来,这样做有几个好处: 把数据处理逻辑 ...

  5. 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 ...

  6. 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 ...

  7. Using Repository Pattern in Entity Framework

    One of the most common pattern is followed in the world of Entity Framework is “Repository Pattern”. ...

  8. 学习笔记之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 ...

  9. [转]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 ...

随机推荐

  1. 解决ubuntu解压zip文件名乱码的问题

    1. 安装7-zip 和 convmv : 命令: sudo apt-get install convmv p7zip-full 2. 解压zip文件: 命令:LANG=C 7z e yourZIPf ...

  2. tolua++实现分析

    项目正在使用cocos2dx的lua绑定,绑定的方式是tolua++.对大规模使用lua代码信心不是很足,花了一些时间阅读tolua++的代码,希望对绑定实现的了解,有助于项目对lua代码的把控.从阅 ...

  3. 转载 ACM训练计划

    leetcode代码 利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode. ...

  4. Android 模拟器 获得 root权限

    启动一个模拟器,开始-运行-输入cmd,打开dos,依次输入 adb shell mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system ...

  5. <转载> 优秀程序员必备的23条好习惯

    转自 优秀程序员必备的23条好习惯 编程是一项聪明人玩的游戏,它既是对智力的考验,也是对习惯的考验,智力的好坏取决于父母的基因,人们无从左右,但习惯的好坏却是可以不断培养.一项由美国芝加哥大学国家研究 ...

  6. Unity不同平台生成中预处理的注意点

    http://blog.csdn.net/pandawuwyj/article/details/7959335 Unity3D的项目,这周吃亏在宏上了.大背景是项目需要在Unity中用Hudson自动 ...

  7. 【转】基于CXF Java 搭建Web Service (Restful Web Service与基于SOAP的Web Service混合方案)

    转载:http://www.cnblogs.com/windwithlife/archive/2013/03/03/2942157.html 一,选择一个合适的,Web开发环境: 我选择的是Eclip ...

  8. 【原创】MYSQL++源码剖析——前言与目录

    终于完成了! 从第一次想写到现在真的写好大概花了我3个月时间.原来一直读人家的系列文章,总感慨作者的用心良苦和无私奉献,自己在心里总是会觉得有那么些冲动也来写一个. 最开始的麻烦是犹豫该选哪个主题.其 ...

  9. Lua中的require

    lua中的require机制    为了方便代码管理,通常会把lua代码分成不同的模块,然后在通过require函数把它们加载进来.现在看看lua的require的处理流程.1.require机制相关 ...

  10. http--响应请求状态码

    针对客户端向Web服务器发送的任意一个HTTP请求,不论在何种情况下得到一个响应,每个响应均具有一个由3位数字表示的状态码和相应的描述文字.不同数值的状态码体现了不同类型的响应状态,W3C对响应状态码 ...