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.
- What is repository pattern?
- Why should we use it?
- 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
相关文章
- org.apache.hadoop.ipc.RemoteException: User: root is not allowed to impersonate root
- Leetcode 之Convert Sorted Array to Binary Search Tree(54)
- 服务器磁盘扩展卷时遭遇“There is not enough space available on the disk(s) to complete this operation.”错误
- Android Studio解决unspecified on project app resolves to an APK archive which is not supported
- [React Testing] Use Generated Data in Tests with tests-data-bot to Improve Test Maintainability
- [Angular2] Map keyboards events to Function
- 【bug】Unable to execute dex: Multiple dex files define
- bug--Unable to add window –token is not valid; is your activity running?
- [XState] Send Events to the Machine with the XState Send Action Creator
- [Git] How to revert one file changes from one commit
- why we have to redefine _isRefreshNeeded for oData model
- try to navigate from button to line item page
- How to find where settype DB table COMM_PRMAT is accessed without debugging
- DOCX to PDF conversion is successfully done document builder
- association in CDS view is converted to LEFT OUTER MANY TO ONE JOIN in the runtime
- How to judge if one OData model data is out of date
- Document Builder: how is rule detail loaded from DB to memory
- IBASE component valid to field
- How is data replicted from HANA to AS
- E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?
- 缺少构造方法:Cause java.sql.SQLDataException Unsupported conversion from LONG to java.sql.Timestamp
- 【AIGC】阿里达摩院:文本生成视频大模型-通用领域 (Text-to-video-synthesis Model in Open Domain)
- capturing self strongly in this block is likely to lead to a retain cycle
- ERROR: ssh: connect to host hadoop103 port 22: No route to host