c#項目架構搭建經驗


         讀過.Net項目中感覺代碼寫的不錯(備注1)有:bbsMax(可惜唧唧喳喳鳥像消失了一樣),Umbraco(國外開源的cms項目),Kooboo(國內做開源cms)。本人狹隘,讀的代碼不多,范圍也不廣泛。

         你是否一直渴望自己能開發出一套系統架構,讓人讀起來感覺到程序有條理,結構很合理。

         好的架構師需要具備:

        1.項目經驗豐富。(應該是從一堆堆項目中走出來的,豐富的經驗比大篇大論的理論更深刻。)

        2.知識的全面性,基礎的扎實程度。(知識的全面性不要求你什么都知道,起碼要能熟練掌握WCF,Silerlight,WebService,WindowService,Asp.net,WinForm,Asp.net Mvc,ORM(Entity Framework,Linq,NHibernate)DI/IOC(Autofac,Unity,Cast),Javascript(javascript能自己封裝類庫,很熟練jquery),Ajax(js幫助方式,AjaxPanel),OAuth(Open Identity),OData等)

        3.會從不同的角度去了解問題。(但不必什么語言都懂,起碼你不能只懂一種技術,當你去讀不同語言實現的架構時會給你帶來不一樣的感覺,處理事情的方式。比如:Django,或者Ruby On Rails,可以讓你能了解到MVC架構的真諦,在讀Java的struts的感覺又是不同的感覺)

        4.熟練應用的設計模式。(行為模型,結構模式,創建型模式)

        5.對程序有濃厚的興趣。(如果你只把程序當作換取收入的一種技能,估計很難成為資深級別;業余時間【晚上8:00到10:00你在忙什麽,你將在你的未來收獲什么。】(備注2),你是否能達到每天這樣做,從畢業一直到離開這個行業,業務時間一直把程序作為你的愛好,它是否在業余時間能成為能讓你充實的東西)

        6.理科是否優秀。(不管你信不信,如果你理科不好,計算機技術你成為高手會很漫長)

        怎么搭建c#項目架構像計算機一樣運行的計算機

        1.Boot.cs,它應該在工程中出現在什么位置,它能做什么事情。

配置信息管理類的初始化(加載)。。。

系統信息管理類的初始化(加載)。。。

業務訂閱實現的初始化(加載)。。。

系統適配管理類初始化(加載)。。。

模版引擎初始化(加載)。。。

theme管理類初始化(加載)。。。

plugin管理類初始化(加載)。。。

widget管理類初始化(加載)。。。

數據操作實現類初始化(加載)。。。

等等

實現示例:

 

 1     protected void Application_BeginRequest(object sender, EventArgs e)
 2     {
 3             if (false == hasInit)
 4             {
 5                 lock (locker)
 6                 {
 7                     if (false == hasInit)
 8                     {
 9                         // 第一次HttpApplicaiton初始實例化時,啟動Boot類中全局靜態型變量信息
10                         // AllEnums初始化:包含Enum屬性,字段,注解信息,避免每次使用enum上下文信息時都動態創建;
11                         // AllSettings初始化:避免全局唯一配置管理類多次初始化,保證應用程序池中保留一份對象實例
12                         // 配置信息管理類的初始化(加載)。。。
13                         // 系統信息管理類的初始化(加載)。。。
14                         // 業務訂閱實現的初始化(加載)。。。
15                         // 系統適配管理類初始化(加載)。。。
16                         // 模版引擎初始化(加載)。。。
17                         // theme管理類初始化(加載)。。。
18                         // plugin管理類初始化(加載)。。。
19                         // widget管理類初始化(加載)。。。
20                         // 數據操作實現類初始化(加載)。。
21                         Boot.Init();
22                     }
23                 }
24             }
25 
26             // 每次http請求開始時,都需要初始化http上下文包裝類
27             AppContext.Init();
28 。。。。。。
29     }
30 
31     /// <summary>
32     /// 啟動時
33     /// </summary>
34     public class Boot
35     {
36         /// <summary>
37         /// 初始化。。。
38         /// </summary>
39         public static void Init()
40         {
41             // 全局系統變量加載
42             Globals.Init();
43 
44             // 全局唯一配置信息加載
45             SettingsManager.Init();
46 
47             // 全局Enums上下文加載
48             AllEnums.Init();
49             // 全局配置信息
AllSettings.Init(); 50 // 初始化Theme代理實現類 51 ThemeProxies.Proxies.Clear(); 52 ThemeProxies.Proxies.Add(new DefaultThemeProxy()); 53 。。。。。。 54 } 55 }

 

 皮膚管理示例:

 

  

 1 public interface IThemeProxy
 2     {
 3         List<Theme> Load();
 4 
 5         List<TemplateInfo> Load(string dir);
 6 
 7         string Load(string file);
 8 
 9         Theme GetDefault();
10 
11         void SetDefault(Theme theme);
12 
13         void Delete(Theme theme);
14 
15         void Upload(Theme theme);
16 
17         void Save(string file, string fileContext);
18     }
View Code
1   public class ThemeProxyCollection : Collection<IThemeProxy>
2     {
3 
4     }
View Code
1  public class ThemeProxies
2     {
3         public static ThemeProxyCollection Proxies = new ThemeProxyCollection() { 
4            new  DefaultThemeProxy()
5         };
6     }
View Code

 

        2.AppContext.cs,它扮演什么角色,它能做什么事情。

皮膚管理,

錯誤、跟蹤信息處理,

存放、獲取上下文臨時變量,

當前訪問用戶(基本賬戶信息,權限信息),

等等

        3.Global.cs(AppRuntime.cs),看到這個類,你能想到它能做什么,在系統架構中他該扮演什么作用。

系統參數信息。。。

config配置信息。。。

系統路徑快速獲取幫助函數。。。

        4.系統配置,系統經常出現的Enum類型,怎么管能讓你的系統感覺到整潔。

AllEnums.cs來維護全局enum的上下文。。。

AllSettings.cs來維護全局配置信息。。。

等等

 

 1     /// <summary>
 2     /// 全局enum對象成員信息管理類
 3     /// </summary>
 4     public class AllEnums
 5     {
 6         public AllEnums()
 7         {
 8         }
 9 
10         public static EnumInfoCollection CategoriesTypeMembers { get; set; }
11 
12         /// <summary>
13         /// 初始化全局enum對象成員信息
14         /// </summary>
15         public static void Init()
16         {
17             CategoriesTypeMembers = EnumUtil.GetEnumItems(typeof(CategoriesType));
18 
19         }
20     }

 

 1  public class EnumInfo : IPrimaryKey<string>
 2     {
 3         // Methods
 4         public string GetKey()
 5         {
 6             return this.Name;
 7         }
 8 
 9         // Properties
10         public string DefaultValue { get; set; }
11 
12         public string Description { get; set; }
13 
14         public string Name { get; set; }
15 
16         public object Value { get; set; }
17     }
18 
19     public class EnumInfoCollection : Collection<EnumInfo>
20     {
21         // Methods
22         public EnumInfoCollection()
23         {
24         }
25 
26         public EnumInfoCollection(EnumInfoCollection items)
27         {
28             if (items != null)
29             {
30                 foreach (EnumInfo info in items)
31                 {
32                     base.Add(info);
33                 }
34             }
35         }
36     }
View Code
 1  public enum CategoriesType : int
 2     {
 3         [Description("文章欄目")]
 4         [DefaultValue(0)]
 5         Article,
 6 
 7         [Description("產品欄目")]
 8         [DefaultValue(1)]
 9         Product,
10 
11         [Description("下載欄目")]
12         [DefaultValue(0)]
13         Dowload,
14 
15         [Description("圖片欄目")]
16         [DefaultValue(0)]
17         Images,
18 
19     }
View Code
 1  public class EnumUtil
 2     {
 3         public static EnumInfoCollection GetEnumItems(Type enumType)
 4         {
 5             EnumInfoCollection infos = new EnumInfoCollection();
 6             FieldInfo[] fields = enumType.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly);
 7             foreach (FieldInfo info2 in fields)
 8             {
 9                 EnumInfo item = new EnumInfo
10                 {
11                     Name = info2.Name
12                 };
13                 DescriptionAttribute[] customAttributes = (DescriptionAttribute[])info2.GetCustomAttributes(typeof(DescriptionAttribute), true);
14                 if ((customAttributes != null) && (customAttributes.Length > 0))
15                 {
16                     item.Description = customAttributes[0].Description;
17                 }
18                 DefaultValueAttribute[] attributeArray2 = (DefaultValueAttribute[])info2.GetCustomAttributes(typeof(DefaultValueAttribute), true);
19                 if ((attributeArray2 != null) && (attributeArray2.Length > 0))
20                 {
21                     item.DefaultValue = attributeArray2[0].Value as string;
22                 }
23                 object obj2 = enumType.InvokeMember(info2.Name, BindingFlags.GetField, null, null, null);
24                 switch (Enum.GetUnderlyingType(enumType).FullName)
25                 {
26                     case "System.Int32":
27                         item.Value = (int)obj2;
28                         break;
29 
30                     case "System.Int16":
31                         item.Value = (short)obj2;
32                         break;
33 
34                     case "System.Byte":
35                         item.Value = (byte)obj2;
36                         break;
37 
38                     case "System.UInt16":
39                         item.Value = Convert.ToInt32((ushort)obj2);
40                         break;
41 
42                     case "System.UInt32":
43                         item.Value = (int)obj2;
44                         break;
45 
46                     case "System.UInt64":
47                         item.Value = (int)obj2;
48                         break;
49                 }
50                 infos.Add(item);
51             }
52             return infos;
53         }
54 
55         public static bool TryParse<T>(string memberName, out T t) where T : struct
56         {
57             t = default(T);
58             return (!memberName.IsNullOrEmpty() && Enum.TryParse<T>(memberName, true, out t));
59         }
60 
61     }
View Code

 

 

        5.數據操作類,能怎么處理讓DataAccess層更規范。

        6.Plugin怎么管理。

        7.Widgets怎么管理。

        8.你是否還在死守Dao,Business,Web,Entity(Model)這樣的分層結構,這種從Foosun.Net(cms),PetShop流傳出來的結構,是否毒害了你對程序的結構的創想。

        9.Template Engin,你是否嘗試去處理實現過,請拋棄NVelocity帶給你的毒害。是能否從Discuzz!NT中獲得點靈感。或者Asp.net mvc模版運行原理你是否清晰,實現原理是否能找到核心代碼實現處。

        10. 偽靜態,原理,實現。是否嘗試過用。

        11. 靜態HTML生成系統你是否嘗試用,自己是否嘗試過寫。

        12. 接口類,抽象類,實現類,管理類,代理類,創建型類,行為型類,裝飾型類,實體型類,枚舉類等,你是否在程序創建類時對他們命名,文件存放位置,結構划分有過深思熟慮。

 

備注1:

什么代碼算上好:

1,代碼書寫規范;

2,代碼結構合理;

3,代碼變量,函數,類,命名空間,項目名稱規范;

4,代碼注釋完整;

5,對靜態接口類,抽象類等使用的地道。

6,設計模式,用的典型,經典。

7,代碼執行效率高,算法優。

8,數據訪問處理恰當,sql行文規范,緩存分配合理,內存占用合理。

備注2(希望大家不要誤解) 

1.胡適先生畢業典禮講話:“一個人的業余時間用來做什么,就決定了他的將來”;

2.愛因斯坦說:“一個人活到六七十歲,大概有13年做工作,有17年是業余時間,此外是吃飯睡覺的時間。一個人能不能成才,關鍵在於利用你的17年,能夠利用業余時間的人就能成才,否則就不能成才。”

  

本人學術短淺,還是希望更牛的朋友們給點架構方面的建議,意見。

看到這個哥們的文章中截圖不錯:

http://www.cnblogs.com/mienreal/tag/%E6%9E%B6%E6%9E%84%E8%AE%BE%E8%AE%A1/

積累好文章:http://www.cnblogs.com/mienreal/p/4340864.html(經驗豐富的架構,產品人發的文章)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM