בניית שכבת נתונים עבור טבלה בודדת - שלב שני

סגור באמצעות טופס זה תוכלו לספר ולהמליץ לחבריכם..
שם השולח:
כתובת דוא"ל של השולח:
שם המקבל:
שלח לכתובת דוא"ל:
הוסף הערה:
בניית שכבת המידע עבור הטבלה Website

בניית שכבת נתונים עבור טבלה בודדת  
שלב שני

מאת: ארז קלר

אם נתבונן על המימוש של כל המתודות נגלה לא מעט שכפולי קוד (מסומנים בצהוב בהיר) מאוד לא בריאים:
 1  : public class WebsiteDB 
 2  : { 
 3  :     private string connectionString = @"Data Source=CORNER01\SQLEXPRESS;Initial  
                                                         Catalog=RssFeedDB;Integrated Security=True"; 
 4  :     private SqlConnection connection; 
 5  :     private SqlCommand command; 
 6  :     private SqlDataReader reader; 
 7  :     public WebsiteDB() 
 8  :     { 
 9  :         connection = new SqlConnection(connectionString); 
 10 :         command = new SqlCommand(); 
 11 :         command.Connection = connection; 
 12 :     } 
 13 :  
 14 :     public Websites SelectAll() 
 15 :     { 
 16 :         command.CommandText = "SELECT * FROM Website"; 
 17 :         reader = command.ExecuteReader(); 
 18 :         Websites websites = new Websites(); 
 19 :         try 
 20 :         { 
 21 :             command.Connection = connection; 
 22 :             connection.Open(); 
 23 :             Website website; 
 24 :             while (reader.Read()) 
 25 :             { 
 26 :                 website = new Website(); 
 27 :                 website.WebsiteID = (int)reader["WebsiteID"]; 
 28 :                 website.Name = reader["Name"].ToString(); 
 29 :                 website.LogoLink = reader["LogoLink"].ToString(); 
 30 :                 website.Link = reader["Link"].ToString(); 
 31 :                 websites.Add(website); 
 32 :             } 
 33 :         } 
 34 :         catch (Exception e) 
 35 :         { 
 36 :  
 37 :         } 
 38 :         finally 
 39 :         { 
 40 :             if (reader != null) 
 41 :                 reader.Close(); 
 42 :  
 43 :             if (connection.State == ConnectionState.Open) 
 44 :                 connection.Close(); 
 45 :         } 
 46 :         return websites; 
 47 :     } 
 48 :     public Websites SelectByName(string name) 
 49 :     { 
 50 :         command.CommandText = string.Format("SELECT * FROM Website WHERE Name = {0}", name); 
 51 :         reader = command.ExecuteReader(); 
 52 :         Websites websites = new Websites(); 
 53 :         try 
 54 :         { 
 55 :             command.Connection = connection; 
 56 :             connection.Open(); 
 57 :             Website website; 
 58 :             while (reader.Read()) 
 59 :             { 
 60 :                 website = new Website(); 
 61 :                 website.WebsiteID = (int)reader["WebsiteID"]; 
 62 :                 website.Name = reader["Name"].ToString(); 
 63 :                 website.LogoLink = reader["LogoLink"].ToString(); 
 64 :                 website.Link = reader["Link"].ToString(); 
 65 :                 websites.Add(website); 
 66 :             } 
 67 :         } 
 68 :         catch (Exception e) 
 69 :         { 
 70 :  
 71 :         } 
 72 :         finally 
 73 :         { 
 74 :             if (reader != null) 
 75 :                 reader.Close(); 
 76 :  
 77 :             if (connection.State == ConnectionState.Open) 
 78 :                 connection.Close(); 
 79 :         } 
 80 :         return websites; 
 81 :     } 
 82 :     public Website SelectByID(string id) 
 83 :     { 
 84 :         command.CommandText = string.Format("SELECT * FROM Website WHERE WebsiteID = {0}", id); 
 85 :         reader = command.ExecuteReader(); 
 86 :         Websites websites = new Websites(); 
 87 :         try 
 88 :         { 
 89 :             command.Connection = connection; 
 90 :             connection.Open(); 
 91 :             Website website; 
 92 :             while (reader.Read()) 
 93 :             { 
 94 :                 website = new Website(); 
 95 :                 website.WebsiteID = (int)reader["WebsiteID"]; 
 96 :                 website.Name = reader["Name"].ToString(); 
 97 :                 website.LogoLink = reader["LogoLink"].ToString(); 
 98 :                 website.Link = reader["Link"].ToString(); 
 99 :                 websites.Add(website); 
 100:             } 
 101:         } 
 102:         catch (Exception e) 
 103:         { 
 104:  
 105:         } 
 106:         finally 
 107:         { 
 108:             if (reader != null) 
 109:                 reader.Close(); 
 110:  
 111:             if (connection.State == ConnectionState.Open) 
 112:                 connection.Close(); 
 113:         } 
 114:         if (websites.Count() == 1) 
 115:             return websites[0]; 
 116:         return null; 
 117:     } 
 118: } 
את אותם מקטעי קוד משותפים נוציא למתודה חיצוניות: 
 1  : public class WebsiteDB 
 2  : { 
 3  :     private string connectionString = @"Data Source=CORNER01\SQLEXPRESS;Initial 
                                                         Catalog=RssFeedDB;Integrated Security=True"; 
 4  :     SqlConnection connection; 
 5  :     SqlCommand command; 
 6  :     SqlDataReader reader; 
 7  :     public WebsiteDB() 
 8  :     { 
 9  :         connection = new SqlConnection(connectionString); 
 10 :         command = new SqlCommand(); 
 11 :         command.Connection = connection; 
 12 :     } 
 13 :  
 14 :     public Websites SelectAll() 
 15 :     { 
 16 :         command.CommandText = "SELECT * FROM Website"; 
 17 :         Websites websites = Select(); 
 18 :         return websites; 
 19 :     } 
 20 :     public Websites SelectByName(string name) 
 21 :     { 
 22 :         command.CommandText = string.Format("SELECT * FROM Website WHERE Name = {0}", name); 
 23 :         Websites websites = Select(); 
 24 :         return websites; 
 25 :     } 
 26 :     public Website SelectByID(string id) 
 27 :     { 
 28 :         command.CommandText = string.Format("SELECT * FROM Website WHERE WebsiteID = {0}", id); 
 29 :         Websites websites = Select(); 
 30 :         if (websites.Count() == 1) 
 31 :             return websites[0]; 
 32 :         return null; 
 33 :     } 
 34 :     private Websites Select() 
 35 :     { 
 36 :         reader = command.ExecuteReader(); 
 37 :         Websites websites = new Websites(); 
 38 :         try 
 39 :         { 
 40 :             command.Connection = connection; 
 41 :             connection.Open(); 
 42 :             Website website; 
 43 :             while (reader.Read()) 
 44 :             { 
 45 :                 website = new Website(); 
 46 :                 website.WebsiteID = (int)reader["WebsiteID"]; 
 47 :                 website.Name = reader["Name"].ToString(); 
 48 :                 website.LogoLink = reader["LogoLink"].ToString(); 
 49 :                 website.Link = reader["Link"].ToString(); 
 50 :                 websites.Add(website); 
 51 :             } 
 52 :         } 
 53 :         catch (Exception e) 
 54 :         { 
 55 :  
 56 :         } 
 57 :         finally 
 58 :         { 
 59 :             if (reader != null) 
 60 :                 reader.Close(); 
 61 :  
 62 :             if (connection.State == ConnectionState.Open) 
 63 :                 connection.Close(); 
 64 :         } 
 65 :         return websites; 
 66 :     } 
 67 : } 
שלושת המתודות "הוותיקות" יאתחלו את אובייקט ה- command בשאילתה הנדרשת, ובנוסף אליהן נכתוב מתודה פרטית חדשה הנקראת Select (שורה 34) אשר תיגש למסד הנתונים ותבצע את השאילתה.
המתודה הפרטית Select מממשת את כל הפעולות המשותפות לכל שאר המתודות הקוראות נתונים ממסד הנתונים, בשלב זה רק המתודה החדשה Select פונה למסד הנתונים, שאר המתודות אינן ניגשות בעצמם למס הנתונים אלא רק מאתחלות את הפרמטרים הנדרשים לפניה למסד הנתונים.

דוגמת קוד עבור שלב שני: RssReaderSample-ver 002

 



 
 



כל הזכויות שמורות למחבר ©