您好,登錄后才能下訂單哦!
閱讀目錄:
1.開篇介紹
2.元數據緩存池模式(在運行時構造元數據緩存池)
2.1.元數據設計模式(抽象出對數據的描述數據)
2.2.借助Dynamic來改變IOC、AOP動態綁定的問題
2.3.元數據和模型綁定、元數據應該隱藏在Model背后、元數據與DSL的關系
3.鏈式配置Dynamic模式(愛不釋手的思維習慣編程)
4.委托工廠模式(要優于常見的 工廠,概念更加準確,減少污染)
5.規則外掛(視委托為特殊的規則對象原型)
通過上一篇的“.NET框架設計—常被忽視的C#設計技巧”一文來看,對于框架設計的技巧還是有很多人比較有興趣的,那么框架設計思想對于我們日常開發來說其實并不是很重要,但是對于我們理解框架背后的運行原理至關重要;當我們使用著LINQ靈活的語法的同時我們是否能理解它的背后運行原理、設計原理更深一點就是它的設計模式及復雜的對象模型;
從一開始學習.NET我就比較喜歡框架背后的設計模型,框架提供給我們的使用接口是及其簡單的,單純從使用上來看我們不會隨著對框架的使用時間而增加我們對框架內部設計的理解,反而會養成一樣哪來即用的習慣,我們只有去了解、深挖它的內部設計原理才是我們長久學習的目標;因為框架的內部設計模式是可以提煉出來并被總結的;
這篇文章總結了幾個我最近接觸的框架設計思想,可以稱他們為模式;由于時間關系,這里只是介紹加一個簡單的介紹和示例讓我們能基本的了解它并且能在日后設計框架的時候想起來有這么一個模式、設計方式可以借鑒;當然,這每一節都是一個很大主題,用的時候在去細心的分析學習吧;
很多框架都有將特性放在屬性上面用來標識某種東西,但是這種方式使用不當的話會對性能造成影響;再從框架設計原則來講也是對DomainModel極大的污染,從EntityFramework5.0之前的版本我們就能體會到了,它原本是將部分Attribute加在了Entity上的,但是這畢竟是業務代碼的核心,原則上來說這不能有任何的污染,要絕對的POJO;后來5.0之后就完全獨立了DomainModel.Entity,所有的管理都在運行時構造綁定關系,因為它有EDMX元數據描述文件;
那么這些Attribute其實本質是.NET在運行時的一種元數據,主要的目的是我們想在運行時將它讀取出來,用來對某些方面的判斷;那么現在的問題是如果我們每次都去讀取這個Attribute是必須要走反射機制,當然你可以找一些框架來解決這個問題;(我們這里討論的是你作為開發框架的設計者!)
反射影響性能這不用多講了,那么常規的做法是會在第一次反射之后將這些對象緩存起來,下次再用的時候直接在緩存中讀取;這沒有問題,這是解決了反射的性能問題,那么你的Attribute是否還要加在DomainModel中呢,如果加的話隨著代碼量的增加,這些都會成為后面維護的成本開銷;那么我們如何將干凈的POJO對象提供給程序員用,但是在后臺我們也能對POJO進行強大的控制?這是否是一種設計問題?
我一直比較關注對象與數據之間的關系,面向對象的這種縱橫向關系如何平滑的與E-R實體關系模型對接,這一直是復雜軟件開發的核心問題;這里就用它來作為本章的示例的基本概要;
我們有一個基本的DomainModel聚合,如何在不影響本身簡潔性的情況下與E-R關系對接,比如我們在對聚合進行一個Add操作如何被映射成對數據庫的Insert操作;我們來看一下元數據設計模式思想;
/*============================================================================== * Author:深度訓練 * Create time: 2013-08-04 * Blog Address:http://www.cnblogs.com/wangiqngpei557/ * Author Description:特定領域軟件工程實踐; *==============================================================================*/ namespace ConsoleApplication1.DomainModel { /// <summary> /// Employee.<see cref="DomainModel.Employee"/> /// </summary> public class Employee { /// <summary> /// Primary id. /// </summary> public string EId { get; set; } /// <summary> /// Name. /// </summary> public string Name { get; set; } /// <summary> /// Sex.<see cref="DomainModel.SexType"/> /// </summary> public SexType Sex { get; set; } /// <summary> /// Address. /// </summary> public Address Address { get; set; } } }
/*============================================================================== * Author:深度訓練 * Create time: 2013-08-04 * Blog Address:http://www.cnblogs.com/wangiqngpei557/ * Author Description:特定領域軟件工程實踐; *==============================================================================*/namespace ConsoleApplication1.DomainModel{/// <summary>/// Employee.<see cref="DomainModel.Employee"/>/// </summary>publicclass Employee {/// <summary>/// Primary id./// </summary>publicstring EId { get; set; }/// <summary>/// Name./// </summary>publicstring Name { get; set; }/// <summary>/// Sex.<see cref="DomainModel.SexType"/>/// </summary>public SexType Sex { get; set; }/// <summary>/// Address./// </summary>public Address Address { get; set; } }}
這里有一個以Employee實體為聚合根的聚合,里面包含一些基本的屬性,特別需要強調的是Sex屬性和Address,這兩個屬性分別是Complex類型的屬性; Complex類型的屬性是符合面向對象的需要的,但是在關系型數據庫中是很難實現的,這里就需要我們用元數據將它描述出來并能在一些行為上進行控制;
/*============================================================================== * Author:深度訓練 * Create time: 2013-08-04 * Blog Address:http://www.cnblogs.com/wangiqngpei557/ * Author Description:特定領域軟件工程實踐; *==============================================================================*/ namespace ConsoleApplication1.DomainModel { /// <summary> /// Address . /// </summary> public struct Address { /// <summary> /// Address name. /// </summary> public string AddressName { get; set; } } }
/*============================================================================== * Author:深度訓練 * Create time: 2013-08-04 * Blog Address:http://www.cnblogs.com/wangiqngpei557/ * Author Description:特定領域軟件工程實踐; *==============================================================================*/namespace ConsoleApplication1.DomainModel{/// <summary>/// Address ./// </summary>publicstruct Address {/// <summary>/// Address name./// </summary>publicstring AddressName { get; set; } }}
這是Address類型的定義;
namespace ConsoleApplication1.DomainModel { /// <summary> /// Sex type. /// </summary> public enum SexType { Male, Female } }
namespace ConsoleApplication1.DomainModel{/// <summary>/// Sex type./// </summary>publicenum SexType { Male, Female }}
這是SexType類型的定義;都比較簡單;
只有這樣我們才能對DomainModel進行大面積的復雜設計,如果我們不能將數據對象化我們無法使用設計模式,也就談不上擴展性;
圖1:
這是我們的對象模型,那么我們如何將它與數據庫相關的信息提取出來形成獨立的元數據信息,對元數據的抽取需要動、靜結合才行;
什么動、靜結合,我們是否都會碰見過這樣的問題,很多時候我們的代碼在編譯時是確定的,但是有部分的代碼需要在運行時動態的構造,甚至有些時候代碼需要根據當前的IDE來生成才行,但是最終在使用的時候這些在不同階段生成的代碼都需要結合起來變成一個完整的元數據對象;
框架在很多時候需要跟IDE結合才能使使用變的順手,比如我們在開發自己的ORM框架如果不能直接嵌入到VisualStudio中的話,用起來會很不爽;當我們用自己的插件去連接數據庫并且生成代碼的時候,有部分的元數據模型已經在代碼中實現,但是有部分需要我們動態的去設置才行;
我們來看一下關于元數據的基礎代碼;
/*============================================================================== * Author:深度訓練 * Create time: 2013-08-04 * Blog Address:http://www.cnblogs.com/wangiqngpei557/ * Author Description:特定領域軟件工程實踐; *==============================================================================*/ namespace ORM.Meta { using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; /// <summary> /// Data source context. /// </summary> public abstract class DataBaseContext : List<MetaTable>, IDisposable { /// <summary> /// Data base name. /// </summary> protected string DataBaseName { get; set; } /// <summary> /// Connection string. /// </summary> protected string ConnectionString { get; set; } /// <summary> /// Provider child class add table. /// </summary> /// <param name="table"></param> protected virtual void AddTable(MetaTable table) { this.Add(table); } /// <summary> /// Init context. /// </summary> protected virtual void InitContext() { } public void Dispose() { } } }
/*============================================================================== * Author:深度訓練 * Create time: 2013-08-04 * Blog Address:http://www.cnblogs.com/wangiqngpei557/ * Author Description:特定領域軟件工程實踐; *==============================================================================*/namespace ORM.Meta{using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;/// <summary>/// Data source context./// </summary>publicabstractclass DataBaseContext : List<MetaTable>, IDisposable {/// <summary>/// Data base name./// </summary>protectedstring DataBaseName { get; set; }/// <summary>/// Connection string./// </summary>protectedstring ConnectionString { get; set; }/// <summary>/// Provider child class add table./// </summary>/// <param name="table"></param>protectedvirtualvoid AddTable(MetaTable table) {this.Add(table); }/// <summary>/// Init context./// </summary>protectedvirtualvoid InitContext() { }publicvoid Dispose() { } }}
這表示數據源上下文,屬于運行時元數據的基礎設施;
/*============================================================================== * Author:深度訓練 * Create time: 2013-08-04 * Blog Address:http://www.cnblogs.com/wangiqngpei557/ * Author Description:特定領域軟件工程實踐; *==============================================================================*/ namespace ORM.Meta { using System.Collections.Generic; using System.Linq; /// <summary> /// Database Table meta. /// </summary> public class MetaTable : List<MetaColumn> { /// <summary> /// Table name. /// </summary> public string Name { get; set; } /// <summary> /// Entity name. /// </summary> public string EntityName { get; set; } /// <summary> /// Get column by column name. /// </summary> /// <param name="name">Column name.</param> /// <returns><see cref="ORM.MetaColumn"/></returns> public MetaColumn GetColumnByName(string name) { var column = from item in this.ToList() where item.CoumnName == name select item; return column.FirstOrDefault(); } } }
/*============================================================================== * Author:深度訓練 * Create time: 2013-08-04 * Blog Address:http://www.cnblogs.com/wangiqngpei557/ * Author Description:特定領域軟件工程實踐; *==============================================================================*/namespace ORM.Meta{using System.Collections.Generic;using System.Linq;/// <summary>/// Database Table meta./// </summary>publicclass MetaTable : List<MetaColumn> {/// <summary>/// Table name./// </summary>publicstring Name { get; set; }/// <summary>/// Entity name./// </summary>publicstring EntityName { get; set; }/// <summary>/// Get column by column name./// </summary>/// <param name="name">Column name.</param>/// <returns><see cref="ORM.MetaColumn"/></returns>public MetaColumn GetColumnByName(string name) { var column = from item inthis.ToList() where item.CoumnName == name select item;return column.FirstOrDefault(); } }}
簡單的表示一個Table,里面包含一系列的Columns;要記住在設計元數據基礎代碼的時候將接口留出來,方便在IDE中植入初始化元數據代碼;
圖2:
到目前為止我們都是在為元數據做基礎工作,我們看一下有系統生成的聲明的元數據代碼;
/*============================================================================== * Author:深度訓練 * Create time: 2013-08-04 * Blog Address:http://www.cnblogs.com/wangiqngpei557/ * Author Description:特定領域軟件工程實踐; *==============================================================================*/ namespace ConsoleApplication1.Repository { using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; /// <summary> /// IDE Builder. /// </summary> public class DesignBuilder_DataBaseContext : ORM.Meta.DataBaseContext { //this begin IDE builder. protected override void InitContext() { ORM.Meta.MetaTable metaTable = new ORM.Meta.MetaTable() { Name = "TB_Employee", EntityName = "Employee" }; metaTable.Add(new ORM.Meta.MetaColumn() { CoumnName = "EId", DataType = ORM.Meta.DataType.NVarchar }); metaTable.Add(new ORM.Meta.MetaColumn() { CoumnName = "Name", DataType = ORM.Meta.DataType.NVarchar }); metaTable.Add(new ORM.Meta.MetaColumn() { CoumnName = "Sex", DataType = ORM.Meta.DataType.Int }); metaTable.Add(new ORM.Meta.MetaColumn() { CoumnName = "Address", DataType = ORM.Meta.DataType.NVarchar }); this.AddTable(metaTable); } //end } }
/*============================================================================== * Author:深度訓練 * Create time: 2013-08-04 * Blog Address:http://www.cnblogs.com/wangiqngpei557/ * Author Description:特定領域軟件工程實踐; *==============================================================================*/namespace ConsoleApplication1.Repository{using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;/// <summary>/// IDE Builder./// </summary>publicclass DesignBuilder_DataBaseContext : ORM.Meta.DataBaseContext {//this begin IDE builder.protectedoverridevoid InitContext() { ORM.Meta.MetaTable metaTable = new ORM.Meta.MetaTable() { Name = "TB_Employee", EntityName = "Employee" }; metaTable.Add(new ORM.Meta.MetaColumn() { CoumnName = "EId", DataType = ORM.Meta.DataType.NVarchar }); metaTable.Add(new ORM.Meta.MetaColumn() { CoumnName = "Name", DataType = ORM.Meta.DataType.NVarchar }); metaTable.Add(new ORM.Meta.MetaColumn() { CoumnName = "Sex", DataType = ORM.Meta.DataType.Int }); metaTable.Add(new ORM.Meta.MetaColumn() { CoumnName = "Address", DataType = ORM.Meta.DataType.NVarchar });this.AddTable(metaTable); }//end}}
我假設這是我們框架在IDE中生成的部分元數據代碼,當然你可以用任何方式來存放這些元數據,但是最后還是要去對象化;
圖3:
這個目錄你可以直接隱藏,在后臺屬于你的框架需要的一部分,沒有必要讓它污染項目結構,當然放出來也有理由;如果想讓你的LINQ或者表達式能直接穿過你的元數據上下文你需要直接擴展;
static void Main(string[] args) { using (Repository.DesignBuilder_DataBaseContext context = new Repository.DesignBuilder_DataBaseContext()) { var employee = from emp in context.Employee where emp.EId == "Wqp123" select emp; } }
staticvoid Main(string[] args) {using (Repository.DesignBuilder_DataBaseContext context = new Repository.DesignBuilder_DataBaseContext()) { var employee = from emp in context.Employee where emp.EId == "Wqp123" select emp; } }
這里所有的代碼看上去很簡單,沒有多高深的技術,這也不是本篇文章的目的,任何代碼都需要設計的驅動才能產生價值,我們構建的基礎代碼都是元數據驅動;當你在運行時把這些元數據放入Cache,既不需要加Attribute也不需要反射反而活動了更大程度上的控制,但是要想構建一個能用的元數據結構需要結合具體的需求才行;
要想在運行時完全動態的綁定在編譯時定義的對象行為是需要強大的IOC框架支撐的,這樣的框架我們是做不來的或者需要很多精力,得不償失;對于元數據設計需要將AOP通過IOC的方式注入,在使用的時候需要改變一下思路,AOP的所有的切面在編譯時無法確定,后期通過IOC的方式將所有的行為注入;這里我們需要使用動態類型特性;
使用Dynamic之后我們很多以往不能解決問題都可以解決,更向元編程跨進了一步;對于IOC、AOP的使用也將變的很簡單,也有可能顛覆以往IOC、AOP的使用方式;而且動態編程將在很大程度上越過設計模式了,也就是設計模式的使用方式在動態編程中將不復存在了;
using (Repository.DesignBuilder_DataBaseContext context = new Repository.DesignBuilder_DataBaseContext()) { var employees = from emp in context.Employee where emp.EId == "Wqp123" select emp; Employee employee = new Employee() { EId = "Wqp123" }; var entityOpeartion = DynamicBehavior.EntityDymanicBehavior.GetEntityBehavior<Employee>(employee); entityOpeartion.Add(); }
using (Repository.DesignBuilder_DataBaseContext context = new Repository.DesignBuilder_DataBaseContext()) { var employees = from emp in context.Employee where emp.EId == "Wqp123" select emp; Employee employee = new Employee() { EId = "Wqp123" }; var entityOpeartion = DynamicBehavior.EntityDymanicBehavior.GetEntityBehavior<Employee>(employee); entityOpeartion.Add(); }
/*============================================================================== * Author:深度訓練 * Create time: 2013-08-04 * Blog Address:http://www.cnblogs.com/wangiqngpei557/ * Author Description:特定領域軟件工程實踐; *==============================================================================*/ namespace ConsoleApplication1.DynamicBehavior { using System; using System.Dynamic; public class EntityDymanicBehavior { public static dynamic GetEntityBehavior<TEntity>(TEntity entity) { //auto mark entity behavior dynamic dy = new ExpandoObject(); //load meta data mark dynamic behavior dy.Entity = entity; dy.Add = new Action(() => { Console.WriteLine("Action Add " + entity.GetType()); }); return dy; } } }
/*============================================================================== * Author:深度訓練 * Create time: 2013-08-04 * Blog Address:http://www.cnblogs.com/wangiqngpei557/ * Author Description:特定領域軟件工程實踐; *==============================================================================*/namespace ConsoleApplication1.DynamicBehavior{using System;using System.Dynamic;publicclass EntityDymanicBehavior {publicstatic dynamic GetEntityBehavior<TEntity>(TEntity entity) {//auto mark entity behaviordynamic dy = new ExpandoObject();//load meta data mark dynamic behaviordy.Entity = entity; dy.Add = new Action(() => { Console.WriteLine("Action Add " + entity.GetType()); });return dy; } }}
圖4:
畫紅線的部分是可以抽取來放入擴展方法Add中的,在構造的內部是完全可以進入到元數據緩存池中拿到這些數據然后直接動態生成擴展方法背后的真實方法;
元數據的綁定應該在運行時動態去完成,這點在以往我們需要大費力氣,通過CodeDom、Emit才能完成,但是現在可以通過Dynamic、DLR來完成;思維需要轉變一下,動態編程我們以往用的最多的地方在JS上,現在可以在C#中使用,當然你也可以使用專門的動態語言來寫更強大的元數據框架,IronRuby、IronPython都是很不錯的,簡單的了解過Ruby的元數據編程,很強大,如果我們.NET程序員眼饞就用Iron…系列;
在開發復雜的動態行為時盡量使用元數據設計思想,不要把數據和表示數據的數據揉在一起,要把他們分開,在運行時Dynamic綁定;元數據應該在Model的背后應該在DomainModel的背后;
元數據和DSL有著天然的淵源,如果我們能把所有的語句組件化就可以將其封入.NET組件中,在IDE中進行所見即所得的DSL設計,然后生成可以直接運行的Dynamic代碼,這可能也是元編程的思想之一吧;
圖5:
這可能是未來10年要改變的編程路線吧,我只是猜測;最后軟件將進一步被自定義;
再一次提起鏈式編程是覺得它的靈活性無話可說,語言特性本身用在哪里完全需求驅動;把鏈式用來做配置相關的工作非常的合適;我們上面做了元數據配置相關的工作,這里我們試著用鏈式的方法來改善它;
Dynamic類型本身的所有行為屬性都是可以動態構建的,那么我們把它放入鏈式的方法中去,根據不同的參數來實現動態的添加行為;
擴展Dynamic類型需要使用ExpandoObject開始;
/*============================================================================== * Author:深度訓練 * Create time: 2013-08-04 * Blog Address:http://www.cnblogs.com/wangiqngpei557/ * Author Description:特定領域軟件工程實踐; *==============================================================================*/ namespace ConsoleApplication1.DynamicBehavior { using System; using System.Dynamic; public static class EntityDynamicBehaviorExtent { /// <summary> /// Add dynamic method. /// </summary> /// <param name="entity"></param> /// <returns></returns> public static ExpandoObject AddExten(this ExpandoObject entity) { dynamic dy = entity as dynamic; dy.Add = new Func<ExpandoObject>(() => { Console.WriteLine("add " + entity); return entity; }); return entity; } /// <summary> /// where dynamic method. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entity"></param> /// <param name="where"></param> /// <returns></returns> public static ExpandoObject WhereExten<T>(this ExpandoObject entity, Func<T, bool> where) { dynamic dy = entity as dynamic; dy.Where = where; return entity; } } }
/*============================================================================== * Author:深度訓練 * Create time: 2013-08-04 * Blog Address:http://www.cnblogs.com/wangiqngpei557/ * Author Description:特定領域軟件工程實踐; *==============================================================================*/namespace ConsoleApplication1.DynamicBehavior{using System;using System.Dynamic;publicstaticclass EntityDynamicBehaviorExtent {/// <summary>/// Add dynamic method./// </summary>/// <param name="entity"></param>/// <returns></returns>publicstatic ExpandoObject AddExten(this ExpandoObject entity) { dynamic dy = entity as dynamic; dy.Add = new Func<ExpandoObject>(() => { Console.WriteLine("add " + entity); return entity; });return entity; }/// <summary>/// where dynamic method. /// </summary>/// <typeparam name="T"></typeparam>/// <param name="entity"></param>/// <param name="where"></param>/// <returns></returns>publicstatic ExpandoObject WhereExten<T>(this ExpandoObject entity, Func<T, bool> where) { dynamic dy = entity as dynamic; dy.Where = where;return entity; } }}
擴展方法需要擴展 ExpandoObject對象,DLR在運行時使用的是ExpandoObject對象實例,所以我們不能夠直接擴展Dynamic關鍵字;
Employee employee1 = new Employee() { EId = "Wqp123" }; var dynamicEntity = DynamicBehavior.EntityDymanicBehavior.GetEntityBehavior<Employee>(employee1); (dynamicEntity as System.Dynamic.ExpandoObject).AddExten().WhereExten<Employee>(emp => { Console.WriteLine("Where Method."); return emp.EId == "Wqp123"; }); dynamicEntity.Add().Where(employee1);
Employee employee1 = new Employee() { EId = "Wqp123" }; var dynamicEntity = DynamicBehavior.EntityDymanicBehavior.GetEntityBehavior<Employee>(employee1); (dynamicEntity as System.Dynamic.ExpandoObject).AddExten().WhereExten<Employee>(emp => { Console.WriteLine("Where Method.");return emp.EId == "Wqp123"; }); dynamicEntity.Add().Where(employee1);
圖6:
紅線部分必須要轉換才能順利添加行為;
對于工廠模式我們都會熟悉的一塌糊涂,各種各樣的工廠模式我們見的多了,但是這種類型的工廠使用方式你還真的沒見過;其實這種委托是想部分的邏輯交給外部來處理;
/*============================================================================== * Author:深度訓練 * Create time: 2013-08-04 * Blog Address:http://www.cnblogs.com/wangiqngpei557/ * Author Description:特定領域軟件工程實踐; *==============================================================================*/ namespace ConsoleApplication1.DomainModel { /// <summary> /// Address factory. /// </summary> /// <returns></returns> public delegate Address Factory(); /// <summary> /// Employee.<see cref="DomainModel.Employee"/> /// </summary> public class Employee { public Employee() { } /// <summary> /// Mark employee instance. /// </summary> /// <param name="eID"></param> /// <param name="name"></param> /// <param name="sex"></param> /// <param name="addressFactory">address factory.</param> public Employee(string eID, string name, SexType sex, Factory addressFactory) { this.EId = eID; this.Name = name; this.Sex = sex; this.Address = addressFactory(); } /// <summary> /// Primary id. /// </summary> public string EId { get; set; } /// <summary> /// Name. /// </summary> public string Name { get; set; } /// <summary> /// Sex.<see cref="DomainModel.SexType"/> /// </summary> public SexType Sex { get; set; } /// <summary> /// Address. /// </summary> public Address Address { get; set; } } }
/*============================================================================== * Author:深度訓練 * Create time: 2013-08-04 * Blog Address:http://www.cnblogs.com/wangiqngpei557/ * Author Description:特定領域軟件工程實踐; *==============================================================================*/namespace ConsoleApplication1.DomainModel{/// <summary>/// Address factory./// </summary>/// <returns></returns>publicdelegate Address Factory();/// <summary>/// Employee.<see cref="DomainModel.Employee"/>/// </summary>publicclass Employee {public Employee() { }/// <summary>/// Mark employee instance./// </summary>/// <param name="eID"></param>/// <param name="name"></param>/// <param name="sex"></param>/// <param name="addressFactory">address factory.</param>public Employee(string eID, string name, SexType sex, Factory addressFactory) {this.EId = eID;this.Name = name;this.Sex = sex;this.Address = addressFactory(); }/// <summary>/// Primary id./// </summary>publicstring EId { get; set; }/// <summary>/// Name./// </summary>publicstring Name { get; set; }/// <summary>/// Sex.<see cref="DomainModel.SexType"/>/// </summary>public SexType Sex { get; set; }/// <summary>/// Address./// </summary>public Address Address { get; set; } }}
我們定義了一個用來創建Employee.Address對象的Factory,然后通過構造函數傳入;
Employee employee2 = new Employee("Wqp123", "Wqp", SexType.Male, new Factory(() => { return new Address() { AddressName = "Shanghai" }; }));
Employee employee2 = new Employee("Wqp123", "Wqp", SexType.Male, new Factory(() => {returnnew Address() { AddressName = "Shanghai" }; }));
這里純粹為了演示方便,這種功能是不應該在DommianModel中使用的,都是在一些框架、工具中用來做靈活接口用的;
規則外掛其實跟上面的委托工廠有點像,但是絕對不一樣的設計思想;如何將規則外掛出去,放入Cache中讓運行時可以配置這個規則參數;委托是規則的天然宿主,我們只要將委托序列化進Cache就可以對它進行參數的配置;
/*============================================================================== * Author:深度訓練 * Create time: 2013-08-04 * Blog Address:http://www.cnblogs.com/wangiqngpei557/ * Author Description:特定領域軟件工程實踐; *==============================================================================*/ namespace ConsoleApplication1.DomainModel.Specification { using System; using System.Linq.Expressions; /// <summary> /// Employee add specification. /// </summary> [Serializable] public class EmployeeSpecificationAdd : System.Runtime.Serialization.IDeserializationCallback { /// <summary> /// specification. /// </summary> [NonSerialized] private Func<Employee, bool> _specification; /// <summary> /// Gets specification. /// </summary> public Func<Employee, bool> Specificaion { get { return _specification; } } /// <summary> /// employee. /// </summary> private Employee Employee { get; set; } /// <summary> /// Mark employee specificatoin. /// </summary> /// <param name="employee"></param> public EmployeeSpecificationAdd(Employee employee) { this.Employee = employee; InitSpecification(); } /// <summary> /// Is Check. /// </summary> /// <returns></returns> public bool IsCheck() { return _specification(Employee); } public void OnDeserialization(object sender) { InitSpecification(); } private void InitSpecification() { this._specification = (emp) => { return !string.IsNullOrWhiteSpace(emp.EId) && !string.IsNullOrWhiteSpace(emp.Name); }; } } }
/*============================================================================== * Author:深度訓練 * Create time: 2013-08-04 * Blog Address:http://www.cnblogs.com/wangiqngpei557/ * Author Description:特定領域軟件工程實踐; *==============================================================================*/namespace ConsoleApplication1.DomainModel.Specification{using System;using System.Linq.Expressions;/// <summary>/// Employee add specification./// </summary>[Serializable]publicclass EmployeeSpecificationAdd : System.Runtime.Serialization.IDeserializationCallback {/// <summary>/// specification./// </summary>[NonSerialized]private Func<Employee, bool> _specification;/// <summary>/// Gets specification./// </summary>public Func<Employee, bool> Specificaion { get { return _specification; } }/// <summary>/// employee./// </summary>private Employee Employee { get; set; }/// <summary>/// Mark employee specificatoin./// </summary>/// <param name="employee"></param>public EmployeeSpecificationAdd(Employee employee) {this.Employee = employee; InitSpecification(); }/// <summary>/// Is Check./// </summary>/// <returns></returns>publicbool IsCheck() {return _specification(Employee); }publicvoid OnDeserialization(object sender) { InitSpecification(); }privatevoid InitSpecification() {this._specification = (emp) => {return !string.IsNullOrWhiteSpace(emp.EId) && !string.IsNullOrWhiteSpace(emp.Name); }; } }}
圖7:
注意這里的反序列化接口實現,因為Lambda無法進行序列化,也沒有必要進行序列化;
EmployeeSpecificationAdd specification = new EmployeeSpecificationAdd(employee2); Stream stream = File.Open("specification.xml", FileMode.Create); BinaryFormatter formattter = new BinaryFormatter(); formattter.Serialize(stream, specification); stream.Seek(0, SeekOrigin.Begin); specification = formattter.Deserialize(stream) as EmployeeSpecificationAdd; stream.Close(); stream.Dispose(); if (specification.IsCheck()) { Console.WriteLine("Ok..."); }
EmployeeSpecificationAdd specification = new EmployeeSpecificationAdd(employee2); Stream stream = File.Open("specification.xml", FileMode.Create); BinaryFormatter formattter = new BinaryFormatter(); formattter.Serialize(stream, specification); stream.Seek(0, SeekOrigin.Begin); specification = formattter.Deserialize(stream) as EmployeeSpecificationAdd; stream.Close(); stream.Dispose();if (specification.IsCheck()) { Console.WriteLine("Ok..."); }
既然能將規則序列化了,就可以把它放在任何可以使用的地方了,配置化已經沒有問題了;
示例Demo地址:http://files.cnblogs.com/wangiqngpei557/ConsoleApplication2.zip
作者:王清培
出處:http://wangqingpei557.blog.51cto.com/
本文版權歸作者和51CTO共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。