[C#學(xué)習(xí)筆記27]高內(nèi)聚低耦合思想與實(shí)體集合封裝、模塊化分層與三層結(jié)構(gòu)
基于實(shí)體對(duì)象的數(shù)據(jù)返回
通過實(shí)體類封裝了零散的參數(shù),打包后給數(shù)據(jù)訪問方法。數(shù)據(jù)訪問方法需要把數(shù)據(jù)返回給UI,數(shù)據(jù)是多條,也就是多個(gè)實(shí)體、零散的數(shù)據(jù)。應(yīng)把數(shù)據(jù)封裝到實(shí)體類中,泛型集合List<T>是實(shí)體的容器,把每條數(shù)據(jù)封裝成實(shí)體對(duì)象后再放到List集合中,用List集合作為方法的返回值。
優(yōu)點(diǎn):UI部分不需要知道數(shù)據(jù)訪問是從哪里查詢數(shù)據(jù)的,只使用集合即可(高內(nèi)聚低耦合)
數(shù)據(jù)訪問部分只需把數(shù)據(jù)封裝到集合中,UI是winform、web都沒關(guān)系
查詢結(jié)果對(duì)象封裝的時(shí)候也是比較麻煩,可以使用ORM框架解決。
使用模塊化分層思想來達(dá)到其業(yè)績項(xiàng)目要求
基本的分層:UI+數(shù)據(jù)訪問+實(shí)體類(典型兩層結(jié)構(gòu))
模塊:在.NET平臺(tái)是指類庫項(xiàng)目
一個(gè)模塊可以包含或多個(gè)類庫項(xiàng)目(dll)
項(xiàng)目中包含的模塊:
UI
數(shù)據(jù)模塊:數(shù)據(jù)訪問類+通用數(shù)據(jù)訪問類
業(yè)務(wù)模塊:三層結(jié)構(gòu)必備的
通信模塊、通用類模塊、服務(wù)模塊
三層結(jié)構(gòu)
UI——業(yè)務(wù)模塊——數(shù)據(jù)模塊 ? ? ? 實(shí)體模塊是獨(dú)立層
公司上層——中層(部門經(jīng)理)——員工
業(yè)務(wù)模塊作用:傳遞業(yè)務(wù)請(qǐng)求;業(yè)務(wù)分解
實(shí)現(xiàn):添加4個(gè)模塊(類庫)

添加解決方案MyProject,類庫BLL(業(yè)務(wù)邏輯類CourseManage.cs)、DAL(數(shù)據(jù)處理類CourseService.cs、SQLHelper.cs)、Models(實(shí)體類Course.h、Course.cs、CourseCategory.cs、Teacher.cs)
添加引用:UI——BLL——DAL(Models)
分層優(yōu)點(diǎn):
不管哪層業(yè)務(wù)變化,都不會(huì)影響其他層
后續(xù)升級(jí)也帶來很大的方便
團(tuán)隊(duì)開發(fā)更方便
CourseManager.cs內(nèi)容如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAL;
using Models;
namespace BLL
{
/// <summary>
/// 業(yè)務(wù)邏輯類兩個(gè)職責(zé)
/// 1、業(yè)務(wù)傳遞——隔離
/// 2、業(yè)務(wù)處理——大項(xiàng)目中使用
/// </summary>
public class CourseManager
{
private CourseService courseService = new CourseService();
public int AddCourse(Course course)
{
return courseService.AddCourse(course);
}
public int DeleteCourse(Course course)
{
return courseService.DeleteCourse(course);
}
public object GetCourseCount()
{
return courseService.GetCourseCount();
}
public List<Course> QueryCourseById(int courseId)
{
return courseService.QueryCourseById(courseId);
}
}
}
CourseService.cs內(nèi)容如下:
using Models;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DAL
{
public class CourseService
{
/// <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);
}
/// <summary>
/// 查詢課程總數(shù)
/// </summary>
/// <returns></returns>
public object GetCourseCount()
{
string sql = "select Count(*) as 課程總數(shù) from Course";
return SQLHelper.GetSingleResult(sql);
}
/// <summary>
/// 根據(jù)課程編號(hào)查詢,返回集合對(duì)象
/// </summary>
/// <param name="courseId"></param>
/// <returns></returns>
public List<Course> QueryCourseById(int courseId)
{
string sql = $"select CourseId,CourseName,CourseContent,ClassHour,Credit,CategoryId,TeacherId from Course where CourseId<{courseId}";
//return SQLHelper.GetReader(sql);
SqlDataReader reader = SQLHelper.GetReader(sql);
List<Course> list = new List<Course>();
while (reader.Read())
{
list.Add(new Course//下面要讀取的屬性名必須在上面sql語句只能夠出現(xiàn),否則會(huì)出現(xiàn)找不到的情況
{
CourseName = reader["CourseName"].ToString(),
CourseContent = reader["CourseContent"].ToString(),
ClassHour = Convert.ToInt32(reader["ClassHour"]),
Credit = Convert.ToInt32(reader["Credit"]),
CategoryId = Convert.ToInt32(reader["CategoryId"]),
TeacherId = Convert.ToInt32(reader["TeacherId"]),
CourseId = Convert.ToInt32(reader["CourseId"])
});
}
reader.Close();
return list;
}
}
}
SQLHelper.cs內(nèi)容與之前的一致,修改namespace為HAL
Course.cs內(nèi)容與之前的一致,修改namespace為Models
CourseCategory.cs內(nèi)容添加屬性方式與Course.cs一致
Teacher.cs內(nèi)容添加屬性方式與Course.cs一致
Program.cs內(nèi)容如下:
using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BLL;
namespace MyProjectUI
{
class Program
{
static void Main(string[] args)
{
List<Course> courseList = new CourseManager().QueryCourseById(104);
foreach (var item in courseList)
{
Console.WriteLine(item.CategoryId + "\t" + item.CourseName + "\t" + item.Credit);
}
Console.Read();
}
}
}
App.config內(nèi)容如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<connectionStrings>
<add name="connString" connectionString="server=.;database=CourseManageDB;uid=sa;pwd=123456"/>
</connectionStrings>
</configuration>