Play to ASP. 5: Data operation Encapsulation (i)

Source: Internet
Author: User

1. Data Operation Package

    • 1.1 Overview

In the habit of using the ADO database access and operation encapsulation, usually write the Datahelper/sqlhelper class. By now the ORM is on its way, we should fall in love with the repository pattern to encapsulate the operation. Of course, in order to take into account the beginner , in the encapsulation method, or the teaching method , step by step, eventually re-form the common reusable code. So start without generics and write some extension methods first.

    • 1.2 New Listings

    • 1.3 Code

         Interface-oriented programming, first define interface :

 using   BlogASPNET5.Entity.Accounts;  using   System.Collections.Generic;  namespace   blogaspnet5.repository.interfaces{ public  interface   Iuserrepository {Ienumerab        Le  <user> Getusers ();        User GetUser ( int   ID);        User CreateUser (user user);        User UpdateUser ( int   ID, user user);  bool  deleteuser (int   ID); }}

Interface Implementation :

usingBlogASPNET5.Repository.Interfaces;usingBlogASPNET5.Entity.Accounts;usingSystem.Collections.Generic;usingBlogASPNET5.Repository.Contexts;usingSystem.Linq;namespaceblogaspnet5.repository.implements{ Public classUserrepository:iuserrepository {//The constructor should be instantiated and injected into the Di container, a singleton patternEfcontext db =NewEfcontext ();  Publicuser CreateUser (user user) {db.            Users.add (user); Db.            SaveChanges (); returnuser; }         Public BOOLDeleteUser (intID) {varuser = db. Users.where (U = u.id = =ID).            First (); Db.            Users.remove (user); if(Db. SaveChanges () >0)            {                return true; }            return false; }         PublicUser GetUser (intID) {varuser = db. Users.where (U = u.id = =ID).            First (); returnuser; }         PublicIenumerable<user>getusers () {returndb.        Users.asenumerable (); }         PublicUser UpdateUser (intID, User us) {            varuser = db. Users.where (U = u.id = =ID).            First (); if(User! =NULL) {User. Name=us.                Name; User. Password=us.                Password; User. Gender=us.                Gender; User. Role=us.                Role; Db.            SaveChanges (); }            returnuser; }    }}
    • 1.4 Calls

In the console program test:

usingBlogASPNET5.Repository.Implements;usingBlogASPNET5.Repository.Interfaces;usingSystem;namespaceblogaspnet5.consoleapp{ Public classProgram { Public voidMain (string[] args) {            //interfaces refer to specific instances and are clearly not decoupled//the ASP. NET 5 has built-in Di, very convenient to configure, after the articleiuserrepository ur =Newuserrepository (); varList =Ur.            Getusers (); foreach(varIteminchlist) {Console.WriteLine (item.            Name);        } console.readline (); }    }}

2. Summary

This code is really bad, but the process of simplifying into the complex , but it is very easy to understand. (Go back to the next article ...) )

Play to ASP. 5: Data operation Encapsulation (i)

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.