1. 程式人生 > >基於對象職責明確原則優化程序

基於對象職責明確原則優化程序

學員 readline convert ssid namespace sqlhelper res ask demo

學習了如何編寫通用數據訪問類SQLHelper,在此基礎上還可以繼續優化程序,基於對象職責明確原則,將程序封裝成學生數據信息訪問類、班級信息訪問類、成績信息訪問類等。

 1     /// <summary>
 2     /// 學員信息數據訪問類
 3     /// </summary>
 4     class StudentService
 5     {
 6         public int AddStudent(string studentName, string gender, DateTime birthday,
 7             string stuIdNo, int
age, string phoneNumber, string stuAddress, int classId) 8 { 9 string sql = "insert into Students(StudentName,Gender,Birthday,studentIdNo,Age,PhoneNumber,studentAddress, classId)"; 10 sql += "values(‘{0}‘,‘{1}‘,‘{2}‘,{3},{4},‘{5}‘,‘{6}‘,{7})"; 11 sql = string
.Format(sql, studentName, gender, birthday, stuIdNo, age, phoneNumber, stuAddress, classId); 12 return SQLHelper.Update(sql); 13 }
14 }

 1         static void Main(string[] args)
 2         {
 3             //獲取用戶輸入
 4             Console.WriteLine("請輸入學員姓名:");
 5             string
stuName = Console.ReadLine(); 6 Console.WriteLine("請輸入學員性別:"); 7 string gender = Console.ReadLine(); 8 Console.WriteLine("請輸入學員出生日期:"); 9 DateTime birthday = Convert.ToDateTime(Console.ReadLine()); 10 Console.WriteLine("請輸入學員身份證號:"); 11 string stuIdNo = Console.ReadLine(); 12 Console.WriteLine("請輸入學員年齡:"); 13 int age = Convert.ToInt16(Console.ReadLine()); 14 Console.WriteLine("請輸入學員電話:"); 15 string phoneNumber = Console.ReadLine(); 16 Console.WriteLine("請輸入學員身份地址:"); 17 string stuAddress = Console.ReadLine(); 18 Console.WriteLine("請輸入學員班級編號:"); 19 int classId = Convert.ToInt16(Console.ReadLine()); 20 21 //調用數據訪問方法 22 StudentService objstuService = new StudentService(); 23 int result = objstuService.AddStudent(stuName, gender, birthday, stuIdNo, age, phoneNumber, stuAddress, classId); 24 //展示結果 25 Console.WriteLine(result); 26 Console.ReadLine(); 27 }

但是以上代碼存在缺點:方法參數多

1.定義和使用不方便,容易把參數寫錯

2.當對象是屬性變化時,方法的參數必須改變

問題解學辦法:使用實體類作為方法參數,穩定對外接口

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace ASP.NETDemo
 8 {
 9     /// <summary>
10     /// 學生實體類
11     /// </summary>
12     class Student
13     {
14         public int StudentId { get; set; }
15         public string StudentName { get; set; }
16         public string Gender { get; set; }
17         public DateTime Birthday { get; set; }
18         public decimal StudentIdNo { get; set; }
19         public int Age { get; set; }
20         public string PhoneNumber { get; set; }
21         public string StudentAddress { get; set; }
22         public int ClassId { get; set; }
23     }
24 }

優化後

1        public int AddStudent(Student objStudent)
2         {
3             string sql = "insert into Students(StudentName,Gender,Birthday,studentIdNo,Age,PhoneNumber,studentAddress, classId)";
4             sql += "values(‘{0}‘,‘{1}‘,‘{2}‘,{3},{4},‘{5}‘,‘{6}‘,{7})";
5             sql = string.Format(sql,objStudent.StudentName,objStudent.Gender,objStudent.Birthday,objStudent.StudentIdNo,objStudent.Age,objStudent.PhoneNumber,objStudent.StudentAddress, objStudent.ClassId);
6             return SQLHelper.Update(sql);
7         }
 1        static void Main(string[] args)
 2         {
 3             //創建學員對象
 4             Student objStu = new Student();
 5             
 6             //獲取用戶輸入,封裝學員對象
 7             Console.WriteLine("請輸入學員姓名:");
 8             objStu.StudentName = Console.ReadLine();
 9             Console.WriteLine("請輸入學員性別:");
10             objStu.Gender = Console.ReadLine();
11             Console.WriteLine("請輸入學員出生日期:");
12             objStu.Birthday = Convert.ToDateTime(Console.ReadLine());
13             Console.WriteLine("請輸入學員身份證號:");
14             objStu.StudentIdNo =Convert.ToDecimal( Console.ReadLine());
15             Console.WriteLine("請輸入學員年齡:");
16             objStu.Age = Convert.ToInt16(Console.ReadLine());
17             Console.WriteLine("請輸入學員電話:");
18             objStu.PhoneNumber = Console.ReadLine();
19             Console.WriteLine("請輸入學員身份地址:");
20             objStu.StudentAddress = Console.ReadLine();
21             Console.WriteLine("請輸入學員班級編號:");
22             objStu.ClassId = Convert.ToInt16(Console.ReadLine());
23 
24             //調用數據訪問方法
25             StudentService objstuService = new StudentService();
26             int result = objstuService.AddStudent(objStu);
27             //展示結果
28             Console.WriteLine(result);
29             Console.ReadLine();
30         }

基於對象職責明確原則優化程序