Starting from 0, framing and doing the project (3) Public basic data Operation class Repositorybase

Source: Internet
Author: User

Index

"Selfless sharing: from getting started to mastering ASP. NET MVC" starting with 0, build a framework, do a project directory index

Briefly

Today we write a basic data operation class, if there are people do not understand the place, can take two ways, first: Put forward, second: will be used on the line. This class I generally do not modify it, because the basic operation of the class, you can also directly copied to their own projects.

Project preparation

The tools we use are: VS + SQL Server + IIS7.5

I hope that we have a preliminary understanding of ASP. NET MVC, the theoretical things we do not do too much explanation, some places do not understand also does not matter, will use on the line, with more, with a long time, natural understanding.

Project start

First, create the interface irepository

We create a new interface under the service class library of the application services irepository

Right-click service class library → add → new item → interface name IRepository

Two, the basic operation of the interface method

Do not understand the interface of friends need to pay attention to, the interface is a specification, is not required method body, said the popular point, there is an interface if the inheritance will definitely have to implement, here you only need to write methods on the line, the specific method is how to achieve the way we do, I just tell you, if you inherit my interface, Then I have these methods you have to achieve, and to conform to my norms. This is with the leader of an instruction: you give me a way to upload pictures, return to upload success or failure. Leadership is the interface, you are the implementation of the class, you work under the leadership, the equivalent of you inherit the leadership, then you have to complete this image upload method and return to upload results, as you are using the online upload method ah or write a WebService ah or use jquery plugin ah with you, The leader does not care what you use, he only cares for you to follow his request to achieve this method. So I don't know if you understand the interface?

We have this interface is the common basic data Operation class, so to have a data model Ah, the model is the class, because it is universal, so we write t here and the identity T is a class

Our service class library, if according to the three-tier architecture, it should be the data management layer, since it is the data management layer, then we are manipulating the data model, we add a reference to the domain data model, and we may use some common methods, So we also added a reference to the common public help class library

As we said above, this is the data management layer, we are to operate the data, the common class library, we provide a variety of data management methods, we add two library references EntityFramework and Entityframework.sqlserver

Let's start by declaring several data object operations

Below, we write several interface methods

There are mainly single model operation multi-model operation stored procedure operation query multiple data paging query ADO. NET additions and deletions change

Have friends see this will squalling, I wipe, can directly paste code, why ~ ~ Don't hurry, do not hurry, must have patience ~ ~

I am the first map, to everyone has a general understanding, the code will certainly be posted out of ~ ~

Single-Model operations

View Code

Multi-model operations

View Code

Stored Procedure Operations

View Code

Querying more than one piece of data

View Code

Paging Query

View Code

Ado. NET additions and deletions change

View Code

The class libraries and solutions we reference

View Code

This is the complete IRepository.cs.

View Code

Note: In a paged query, we reference a public class of the common public class library PageCollection.cs

This class library is connected in C # Public helper class: "C # Public helper class" paging Logical processing class

Third, the implementation of the basic operation of the class

We create a new common base data Operation Class RepositoryBase.cs inheritance interface under the Service class library IRepository

Because this implementation class, things more, if there is no problem, we can directly take to use, in view of the time of the relationship (still at work, we understand ~ ~), I directly put the code out, this article is not finished, I will take time to come back briefly introduce some methods and principles of this implementation class, at the same time, If the netizen raises the question or the rectification, I also will return to tidy up the revision.

Fixed common help, including transactions

View Code

Get a single record

View Code

Adding and removing changes operation

View Code

Multi-model operations

View Code

Stored Procedure Operations

View Code

Validation action exists

View Code

Get more than one data operation

View Code

Paging operations

View Code

Ado. NET additions and deletions to search method

View Code

The class libraries and solutions we reference

View Code

The Complete RepositoryBase.cs

View Code

Common public helper classes to use

This writes the class library, everybody Goes to "C # Public help class" to look for. Some public help class, I haven't had time to release, please wait patiently ~ ~ ~ ~ ~ ~ ~ ~ ~

PS1: Everyone's implementation class is not find sqlfunctionfordynamic This method, this is my mistake, because just a bit of work, forget, now add on

We create a new query under the Service class library dynamic class Databaseextensions

The code is as follows: After adding this class, the method has

View Code

Starting from 0, framing and doing the project (3) Public basic data Operation class Repositorybase

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.