前言:在工作發現接口至關重要,特別需要把接口的注釋寫清楚,能調用的同事知道這個接口是干嘛用的,要傳遞什么參數等,在這里我做了一個簡單的接口並生成幫助幫助,供大家相互學習,有好的可以提出來我繼續改進。
第一步:建立一個用戶接口(明確這個接口的作用)
按照Add,Delete,Update,Get模式來定義接口的方法(我個人覺得盡可能的重載)
第二步:方法中寫出盡可能詳盡的注釋
按照方法實現的功能,參數意思,異常,備注,返回值等來寫
第三步:接口中如果出現參數為類型的時候千萬不要用1,2這樣的數值代替盡可能用枚舉
為什么用枚舉:因為在實際項目中可能做到最后的時候自己都不知道1代表什么2代表什么,通過枚舉就能解決這些問題,而且枚舉擴展性也很好
現在我們看一下我寫的接口

1 /// <summary> 2 /// 關於用戶信息的操作 3 /// </summary> 4 public interface IUser 5 { 6 /// <summary> 7 /// 添加用戶信息 8 /// </summary> 9 /// <param name="userInfo">用戶實體</param> 10 /// <exception cref="ArgumentNullException">userInfo為null</exception> 11 /// <exception cref="ArgumentException">userInfo.Id為空字符串</exception> 12 /// <exception cref="ArgumentException">userInfo.UserName為空字符串</exception> 13 /// <exception cref="ArgumentException">userInfo.PassWord為空字符串</exception> 14 /// <exception cref="Exception">其他未知異常</exception> 15 /// <returns>true:添加成功 false:添加失敗</returns> 16 bool Add(UserInfo userInfo); 17 18 /// <summary> 19 /// 根據用戶名刪除用戶 20 /// </summary> 21 /// <param name="userName">用戶名</param> 22 /// <exception cref="ArgumentException">userName為空字符串</exception> 23 /// <exception cref="Exception">其他未知異常</exception> 24 /// <returns>true:刪除成功 false:刪除失敗</returns> 25 bool Delete(string userName); 26 27 /// <summary> 28 /// 根據用戶名批量刪除用戶 29 /// </summary> 30 /// <param name="userNames">用戶名(可以單個也可以多個)</param> 31 /// <exception cref="ArgumentNullException">userNames為null</exception> 32 /// <exception cref="ArgumentException">userNames不是有效參數</exception> 33 /// <exception cref="Exception">其他未知異常</exception> 34 ///<remarks>加入事物,如果其中一條未能刪除成功,所有數據進行回滾</remarks> 35 /// <returns>true:刪除成功 false:刪除失敗</returns> 36 bool Delete(IList<string> userNames); 37 38 /// <summary> 39 /// 更新用戶信息 40 /// </summary> 41 /// <exception cref="ArgumentNullException">userInfo為null</exception> 42 /// <exception cref="ArgumentException">userInfo.UserName為空字符串</exception> 43 /// <exception cref="Exception">其他未知異常</exception> 44 /// <returns>true:更新成功 false:更新失敗</returns> 45 bool Update(UserInfo userInfo); 46 47 /// <summary> 48 /// 根據用戶名獲取用戶信息 49 /// </summary> 50 /// <exception cref="ArgumentException">userName為空字符串</exception> 51 /// <exception cref="Exception">其他未知異常</exception> 52 /// <returns>返回一條用戶信息可為null</returns> 53 UserInfo Get(string userName); 54 55 /// <summary> 56 /// 根據用戶名獲取批量用戶信息 57 /// </summary> 58 /// <param name="userNames">用戶名(可以單個也可以多個)</param> 59 /// <exception cref="ArgumentNullException">userNames為null</exception> 60 /// <exception cref="ArgumentException">userNames不是有效參數</exception> 61 /// <exception cref="Exception">其他未知異常</exception> 62 /// <remarks>如果返回結果為空則返回一條沒有任何用戶的結果集</remarks> 63 /// <returns>獲取用戶集</returns> 64 IList<UserInfo> Get(IList<string> userNames); 65 66 /// <summary> 67 /// 根據職稱類型獲取用戶信息 68 /// </summary> 69 /// <param name="professional"></param> 70 /// <exception cref="ArgumentOutOfRangeException">professional枚舉不在范圍內</exception> 71 /// <exception cref="Exception">其他未知異常</exception> 72 /// <returns>獲取用戶集</returns> 73 IList<UserInfo> Get(UserEnum.ProfessionalType professional); 74 }
第四步:實現這個接口
因為項目一上線錯誤出現以后就很難發現,所以我們一定要加入日志系統,所以在項目中我加入了拋異常,然后通過日志就知道問題出現在哪里(沒有實現功能)

1 /// <summary> 2 /// 實現用戶相關操作 3 /// </summary> 4 public class User:IUser { 5 6 public bool Add(UserInfo userInfo) 7 { 8 try 9 { 10 if (userInfo == null) 11 throw new ArgumentNullException("userInfo"); 12 if (string.IsNullOrEmpty(userInfo.Id)) 13 throw new ArgumentException("userInfo.Id無效"); 14 if (string.IsNullOrEmpty(userInfo.UserName)) 15 throw new ArgumentException("userInfo.UserName無效"); 16 if (string.IsNullOrEmpty(userInfo.PassWord)) 17 throw new ArgumentException("userInfo.PassWord無效"); 18 return false; 19 } 20 catch 21 { 22 throw new Exception("其他未知異常"); 23 } 24 } 25 26 public bool Delete(string userName) 27 { 28 try 29 { 30 if (string.IsNullOrEmpty(userName)) 31 throw new ArgumentException("UserName無效"); 32 return false; 33 } 34 catch 35 { 36 throw new Exception("其他未知異常"); 37 } 38 } 39 40 public bool Delete(IList<string> userNames) 41 { 42 try 43 { 44 if (userNames == null) 45 throw new ArgumentNullException("userNames"); 46 if (!userNames.Any()) 47 throw new ArgumentException("userNames無效"); 48 return false; 49 } 50 catch 51 { 52 throw new Exception("其他未知異常"); 53 } 54 } 55 56 public bool Update(UserInfo userInfo) 57 { 58 try 59 { 60 if (userInfo == null) 61 throw new ArgumentNullException("userInfo"); 62 if (string.IsNullOrEmpty(userInfo.UserName)) 63 throw new ArgumentException("userInfo.UserName無效"); 64 return false; 65 } 66 catch 67 { 68 throw new Exception("其他未知異常"); 69 } 70 71 } 72 73 public UserInfo Get(string userName) 74 { 75 try 76 { 77 if (string.IsNullOrEmpty(userName)) 78 throw new ArgumentException("UserName無效"); 79 return null; 80 } 81 catch { 82 throw new Exception("其他未知異常"); 83 } 84 } 85 86 public IList<UserInfo> Get(IList<string> userNames) 87 { 88 try 89 { 90 if (userNames == null) 91 throw new ArgumentNullException("userNames"); 92 if (!userNames.Any()) 93 throw new ArgumentException("userNames無效"); 94 return null; 95 } 96 catch { 97 throw new Exception("其他未知異常"); 98 } 99 100 } 101 102 public IList<UserInfo> Get(UserEnum.ProfessionalType professional) 103 { 104 try 105 { 106 if (professional >= (UserEnum.ProfessionalType)4) 107 throw new ArgumentOutOfRangeException("professional"); 108 return null; 109 } 110 catch { 111 throw new Exception("其他未知異常"); 112 } 113 114 } 115 }
第五步:點擊項目屬性,找到生成勾選xml文檔文件如下圖
第六步:下載一個Sandcastle Help File Builder 然后安裝,安裝成功以后,找到Sandcastle Help File Builder新建一個項目如下圖
第七步:找到生成的dll和xml文件然后導入如下圖
第八步:點擊生成按鈕就可以生成一篇幫助文檔了
生成幫助文檔的效果
以上就是整個效果,大家有好的歡迎相互討論。