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. Git回滚远程版本

    摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! “房子是租的 但生活不是” 1.故事的开始 远程master分支下代码被不小心提交了很多垃圾代码 ...

  2. C# WinForm RDLC报表不预览直接连续打印

    用微软的RDLC报表直接打印不预览 直接上代码. //打印清单 System.Data.DataTable dt = print_QD(dr); ReportViewer rvDoc = new Re ...

  3. makeJar

    task makeJar(type: Jar) { //指定生成的jar名 baseName 'plugin' //从哪里打包class文件 from('build/intermediates/cla ...

  4. Go语言TCP/UDP Socket编程

    1. TCP编程 TCPClient // TCPClient project main.go package main import ( "fmt" "net" ...

  5. char str[]和char *str的区别

    1.http://blog.csdn.net/szchtx/article/details/10396149 char ss[]="C++";  ss[0]='c';        ...

  6. IIS6.0下 Asp.Net 拦截jpg请求

    之前用mvc写了一个接口,访问格式 http://ip:port/{id}-{type}.jpg来获取一个图片,在IIS7.0+运行毫无障碍,但是在IIS6.0下,直接提示文件不存在或者已删除. 经过 ...

  7. Maven进价:Maven的安装和目录结构

    一.在windows上安装Maven 1.下载 下载地址:http://maven.apache.org/download.html 下载最新版本 maven3.2.5 2.解压 解压地址:F:\Ja ...

  8. codeforces Restore Cube(暴力枚举)

    /* 题意:给出立方体的每个顶点的坐标(是由源坐标三个数某几个数被交换之后得到的!), 问是否可以还原出一个立方体的坐标,注意这一句话: The numbers in the i-th output ...

  9. Gulp:新一代前端构建利器

    1.什么是Gulp gulp.js 是一种基于流的,代码优于配置的新一代构建工具. Gulp 和 Grunt 类似.但相比于 Grunt 的频繁的 IO 操作,Gulp 的流操作,能更快地完成构建. ...

  10. Appium移动自动化测试(四)--one demo

    继续更新. -------------------------------------------- 第四节  安装Appium Client Appium Client是对webdriver原生ap ...