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. CentOS 6.5 Python 2.6.6+Flask 用wsgi方式部署在Apache 2.2.15下

    1,安装wsgi Apache模块 easy_install mod_wsgi 2,添加/etc/httpd/conf.d/wsgi.conf LoadModule wsgi_module modul ...

  2. 如何将 Microsoft Bot Framework 链接至微信公共号

    说到 Microsoft Bot Framework 其实微软发布了已经有一段时间了,有很多朋友可能还不太了解,微软Bot的功能今天我给大家简单的介绍一下,Bot Framework的开发基础以及如何 ...

  3. 实现无锁的栈与队列(5):Hazard Pointer

    两年多以前随手写了点与 lock free 相关的笔记:1,2,3,4,质量都不是很高其实(读者见谅),但两年来陆陆续续竟也有些阅读量了(可见剑走偏锋的技巧是多容易吸引眼球).笔记当中在解决内存释放和 ...

  4. MyBatis知多少(23)MyBatis结果映射

    resultMap的元素是在MyBatis的最重要和最强大的元素.您可以通过使用MyBatis的结果映射减少高达90%的JDBC编码,在某些情况下,可以让你做JDBC不支持的事情. ResultMap ...

  5. 导出excel和PDF小结 vba

    最近接触了一个关于Access工具的项目,所以整理下需要使用的方法. 功能要求简介: 1.将数据表中的数据导出到excel和PDF 2.并根据某个字段名称分sheet输出. 3.无模板方式 方案简介: ...

  6. mvc4.0添加EF4.0时发生编译时错误

    解决此问题是因为MVC4.0默认未添加EF4.0的引用,EF4.0引用的是System.Data.Entity.dll, Version=4.0.0.0, 解决办法: 在web.config文件sys ...

  7. codeforces A. Bayan Bus(简单模拟)

    #include <queue> #include <string> #include <cstdio> #include <cstring> #inc ...

  8. 爬虫技术 -- 基础学习(三)理解URL和URI的联系与区别

    网络爬虫的基本操作是抓取网页.首先要了解下URL~~ 在理解URL之前,先了解下URI,这两个概念我曾经混淆过~@_@|| 什么是URI? Web上每种可用的资源,如:html文档.视频,图片等都由一 ...

  9. 介绍WEB站点结构

    在这节里,我们将抛开Umbraco来看看已创建的站点.在我们介绍Umbraco之前,需要了解站点是如何工作的,如何使用使用浏览器工具. 我们看到在标签顶端的内容叫做页面标题.每个页面的标题都会改变表示 ...

  10. Pattern Lab - 构建先进的原子设计系统

    Pattern Lab 是一个工具集,帮助您创建原子设计系统.在它的核心,是一个自定义静态网站生成器,构建了类似原子,分子和界面结合在一起,形成模板和页面.Pattern Lab 可以作为项目的模式库 ...