五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

[C#學(xué)習(xí)筆記26]數(shù)據(jù)和UI分層的OOP理論、分層方法、實(shí)體類封裝與解析

2020-10-02 22:57 作者:技術(shù)龍的傳人  | 我要投稿

用分層的思想設(shè)計(jì)企業(yè)級(jí)項(xiàng)目

對(duì)象職責(zé)明確原則,具有高可靠性、高性能、高維護(hù)性

1、在UI中,后臺(tái)數(shù)據(jù)訪問的代碼和UI數(shù)據(jù)展示的代碼,混編到一起。給團(tuán)隊(duì)協(xié)作帶來困難,后續(xù)維護(hù)不方便

2、代碼不優(yōu)雅,各個(gè)功能耦合在一起,不符合OOP高內(nèi)聚低耦合的做法

解決方法:按任務(wù)職責(zé)開發(fā)數(shù)據(jù)庫應(yīng)用程序,會(huì)有一些類的劃分

1、UI用來和用戶交互,給用戶展示數(shù)據(jù)、獲取用戶數(shù)據(jù)

2、數(shù)據(jù)訪問類,用來封裝數(shù)據(jù)庫操作的各種方法

1)使用通用數(shù)據(jù)訪問類SQLHelper

2)各種數(shù)據(jù)訪問方法的封裝,通用一般的數(shù)據(jù)訪問類

數(shù)據(jù)傳遞順序:UI——>一般數(shù)據(jù)訪問類——>通用數(shù)據(jù)訪問類

優(yōu)點(diǎn):

1、UI不知道數(shù)據(jù)存到哪兒,符合“高內(nèi)聚低耦合”

2、數(shù)據(jù)訪問類不用關(guān)心數(shù)據(jù)從哪兒來,職責(zé)明確

UI和數(shù)據(jù)訪問之間達(dá)到解耦目的,對(duì)后續(xù)項(xiàng)目擴(kuò)展有利。

在[C#學(xué)習(xí)筆記25]的工程基礎(chǔ)上,新建文件夾Service存放類SQLHelper和新建的類CourseService

新建的類CourseService.cs內(nèi)容如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace SQLTest

{

namespace SQLTest

{

/// <summary>

/// 課程數(shù)據(jù)訪問類

/// </summary>

public class CourseService

{

public int AddCourse(string courseName,string courseContent,int classHour,int credit,int cateforyId,int teacherId)

{

string sql = "Insert into Course(CourseName,CourseContent,ClassHour,Credit,CategoryId,TeacherId)";

sql += $"values ('{courseName}','{courseContent}',{classHour},{credit},{cateforyId},{teacherId})";

//執(zhí)行SQL語句

return SQLHelper.Update(sql);

}

}

}

Program.cs添加方法如下:

public static void AddCourse()

{

//通過窗體或web頁面獲取數(shù)據(jù)

//1、獲取用戶輸入的信息:用戶輸入的每一項(xiàng)數(shù)據(jù)保存到局部變量中

Console.WriteLine("請(qǐng)輸入課程名稱:");//模擬從界面輸入

string courseName = Console.ReadLine();

string courseContent = ".NET框架";

int classHour = 100;

int credit = 22;

int cateforyId = 11;

int teacherId = 102;

//2、調(diào)用后臺(tái)數(shù)據(jù)訪問方法

int result = new CourseService().AddCourse(courseName, courseContent, classHour, credit, cateforyId, teacherId);

//3、顯示操作結(jié)果

Console.WriteLine("受影響行數(shù)="+ result);

}

方法參數(shù)的定義:

1、方法參數(shù)控制在1~4個(gè)最好

2、鑒于UI和數(shù)據(jù)訪問類交互參數(shù)過多問題,使用“實(shí)體類”代替過多的參數(shù)

實(shí)體類:表示數(shù)據(jù)實(shí)體的類(數(shù)據(jù)表中的一條數(shù)據(jù)就是一個(gè)實(shí)體)

實(shí)體類設(shè)計(jì)一般只包括屬性,并且屬性和數(shù)據(jù)表的列是映射關(guān)系

數(shù)據(jù)表列名稱遵循Pancal命名法

數(shù)據(jù)表有多少列就至少有多少屬性

數(shù)據(jù)類型要一致

int——int

字符類型——string(char、varchar、nvarchar、text...)

浮點(diǎn)型——double

smalldatetime——DataTime

實(shí)體類名稱與數(shù)據(jù)表名稱一致

實(shí)體類個(gè)數(shù):有多少數(shù)據(jù)表就應(yīng)該有多少實(shí)體類,根據(jù)需要添加擴(kuò)展的實(shí)體類

實(shí)體類作用:

封裝數(shù)據(jù):調(diào)用對(duì)象時(shí)把參數(shù)封裝到實(shí)體類的屬性中(參數(shù)打包)

傳遞數(shù)據(jù):將數(shù)據(jù)通過實(shí)體類傳遞給被調(diào)用者,反之亦然

項(xiàng)目中通常先添加實(shí)體類,然后再增加數(shù)據(jù)訪問類和其他業(yè)務(wù)類

數(shù)據(jù)訪問類命名:實(shí)體類名+Service

業(yè)務(wù)邏輯類命名:實(shí)體類名稱+Mananger

實(shí)體類優(yōu)點(diǎn):方法調(diào)用之間的參數(shù)變得精簡(jiǎn);使用方便,UI只需把數(shù)據(jù)封裝到實(shí)體類中;使方法調(diào)用的接口變得非常穩(wěn)定。

添加實(shí)體類Course.cs并放在新建文件夾Modles

Course.cs中內(nèi)容如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace SQLTest

{

/// <summary>

/// 課程實(shí)體類

/// </summary>

public class Course

{

public int CourseId { get; set; }

public string CourseName { get; set; }

public string CourseContent { get; set; }

public int ClassHour { get; set; }

public int Credit { get; set; }

public int CategoryId { get; set; }

public int TeacherId { get; set; }

}

}

CourseService.cs添加增刪內(nèi)容如下:

/// <summary>

/// 添加課程(通過實(shí)體類作為參數(shù))

/// </summary>

/// <param name="course">課程對(duì)象</param>

/// <returns></returns>

public int AddCourse(Course course)

{

string sql = "Insert into Course(CourseName,CourseContent,ClassHour,Credit,CategoryId,TeacherId)";

sql += $"values ('{course.CourseName}','{course.CourseContent}',{course.ClassHour},{course.Credit},{course.CategoryId},{course.TeacherId})";

//執(zhí)行SQL語句

return SQLHelper.Update(sql);

}

/// <summary>

/// ?刪除采用對(duì)象,ORM框架中全部采用對(duì)象

/// </summary>

/// <param name="course"></param>

/// <returns></returns>

public int DeleteCourse(Course course)

{

string sql = "delete from Course where CourseId="+course.CourseId;

return SQLHelper.Update(sql);

}

Program.cs添加增刪方法如下:

public static void AddCourse1()

{

//通過窗體或web頁面獲取數(shù)據(jù)

//1、獲取用戶輸入的信息:用戶輸入的每一項(xiàng)數(shù)據(jù)保存到局部變量中

Console.WriteLine("請(qǐng)輸入課程名稱:");//模擬從界面輸入

//封裝對(duì)象:將傳遞的數(shù)據(jù)封裝到實(shí)體屬性中

Course course = new Course() {

CourseName = Console.ReadLine(),

CourseContent = ".NET框架",

ClassHour = 111,

Credit = 22,

CategoryId = 11,

TeacherId = 104

};

//2、調(diào)用后臺(tái)數(shù)據(jù)訪問方法

//int result = new CourseService().AddCourse(courseName, courseContent, classHour, credit, cateforyId, teacherId);

int result = new CourseService().AddCourse(course);

//3、顯示操作結(jié)果

Console.WriteLine("受影響行數(shù)="+ result);

}

//刪除課程對(duì)象

public static void DeleteCourse()

{

Course course = new Course { CourseId=22};

int result = new CourseService().DeleteCourse(course);

Console.WriteLine("受影響行數(shù)=" + result);

}


[C#學(xué)習(xí)筆記26]數(shù)據(jù)和UI分層的OOP理論、分層方法、實(shí)體類封裝與解析的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
湖口县| 长宁区| 迁西县| 岫岩| 大新县| 滦南县| 门头沟区| 格尔木市| 开江县| 恩平市| 德兴市| 三台县| 恩施市| 高淳县| 上杭县| 平原县| 新昌县| 玉山县| 恩平市| 福贡县| 临澧县| 五常市| 怀仁县| 陇南市| 枣庄市| 扶沟县| 基隆市| 潜江市| 保亭| 汤阴县| 宜兰县| 遂平县| 竹北市| 宁夏| 新宁县| 阿勒泰市| 莱州市| 定襄县| 阳朔县| 工布江达县| 谢通门县|