zoukankan      html  css  js  c++  java
  • 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.

    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

  • 相关阅读:
    获取地址栏的key-value形式的值(包括重复的key值)形成对象--重复的变成数组形式
    vue---定时循环setInterval
    动画效果----webkit-animation-----动画背景变化transition:background-color 1s linear;
    background-clip--背景是否填充
    box-sizing盒子的大小,修改文字种类而保持字体大小不变font-size-adjust,----块级和内联display---盒子阴影box-shadow---对盒子中容纳不下的内容的显示overflow
    地图api学习第一天
    文字添加阴影
    css循环样式: nth-child(n)
    es6语法
    将中国标准时间--转换为yyyy-MM-dd 时分秒
  • 原文地址:https://www.cnblogs.com/ASPNET2008/p/3288234.html
Copyright © 2011-2022 走看看