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:

  1. public interface IRepository<T> where T : class, new()
  2. {
  3. T Update(T entity, Expression<Func<T, bool>> filter, bool IsCommit = true);
  4.  
  5. T Insert(T entity, bool IsCommit = true);
  6.  
  7. void Delete(T entity, bool IsCommit = true);
  8.  
  9. void Delete(Expression<Func<T, bool>> filter);
  10.  
  11. IQueryable<T> Query(Expression<Func<T, bool>> filter);
  12.  
  13. IList<T> QueryByPage<TKey>(Expression<Func<T, bool>> filter,
  14. Expression<Func<T, TKey>> orderBy,int orderType, int pageSize, int pageIndex, out int recordsCount);
  15.  
  16. IList<T> QueryByPage(Expression<Func<T, bool>> filter,
  17. string orderBy, int pageSize, int pageIndex, out int recordsCount);
  18.  
  19. T GetSingleOrDefault(Expression<Func<T, bool>> filter);
  20.  
  21. IList<T> FindAll();
  22. }

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. Hadoop 2.4.1 Map/Reduce小结【原创】

    看了下MapReduce的例子.再看了下Mapper和Reducer源码,理清了参数的意义,就o了. public class Mapper<KEYIN, VALUEIN, KEYOUT, VA ...

  2. ITF Demo代码(用VBScript构建的接口测试框架)

    ITF Demo代码(用VBScript构建的接口测试框架) http://blog.csdn.net/testing_is_believing/article/details/20872629

  3. undefined reference to `_imp___ZN8QWebViewC1EP7QWidget'

    add this line to your .pro file: QT += webkitwidgets

  4. 享元模式及C++实现

    享元模式(flyweight) flyweight是轻量级的意思,中文这边翻译成享元,更容易让人理解一些. 享元模式是为了应对大量细粒度对象重复的问题.程序中存在大量细粒度的对象,每次要使用时都必须创 ...

  5. mysql中int、bigint、smallint 和 tinyint的区别详细介绍

    1 bytes = 8 bit ,一个字节最多可以代表的数据长度是2的8次方 11111111 在计算机中也就是 -128到127 1.BIT[M] 位字段类型,M表示每个值的位数,范围从1到64,如 ...

  6. Android抽象布局——include、merge 、ViewStub

    http://blog.csdn.net/xyz_lmn/article/details/14524567

  7. 批量修改Project视图中Prefab的名字

    简要代码如下: using UnityEditor; using UnityEngine; using System.IO; using System.Collections; using Syste ...

  8. hdu 2152

    题目大意:本题是中文题.读者可以直接到OJ上去阅读.提议并不难理解 代码如下: /* * 2152_1.cpp * * Created on: 2013年8月9日 * Author: Administ ...

  9. Sass学习之路:Sass、Compass安装与命令行

    导言 CSS不是一门真正意义上的编程语言,很多编程语言理所当然的特性(比如变量),都不被支持.同时再开发模块化的web项目的时候,也要避免相互干扰.为了弥补CSS的这些不足,就产生了CSS预处理器,S ...

  10. 在做Android开发的,如何去掉滚动view在尽头时的阴影效果

    不经意的在开发中,发现qq的侧滑几乎没有阴影效果,就是拉到边界没有时出现的效果:于是在网上找了下,发现很简单的设置 只要在xml布局文件的滚动或者侧滑控件中加入如下样式: android:overSc ...