Transaction Script是一种最简单和最容易接受的处理业务的方式。
按martinfowler的说法:
Organizes business logic by procedures where each procedure handles a single request from the presentation。许多的业务过程可以看做是许多的业务过程的集合。一个业务过程可能是对数据的一个提取后展现,也可能是对数据的加工。在客户和程序之间的每次交互都包含着许多的逻辑。一种场景是把数据库里的东西展现出来,一种是包含许多的判断和计算。Transaction Script把这些所有的逻辑按照单一的过程来组织,通过直接的或通过包装来和数据库交互。每一个业务都会有自己的Transaction Script,也可以由其它的Transaction Script组成。和Transaction Script一起用的通常是TableDataGageWay。
按我的理解就是把每个过程用函数抽象出来,在这个函数里只做与底层相关的操作,而没有多少业务的征用。当业务相对复杂的情况下,在组织业务时会出现问题。
简单代码如下:
TransactionScript
1using System; 2 3namespace POFEAA 4{ 5 /**//// <summary> 6 /// TransactionScript 的摘要说明。 7 /// </summary> 8 public class TransactionScript 9 {10 public TransactionScript()11 {12 }13 public static Products GetProducts(string name)14 {15 ProductsCollection col = ProductsDataGateWay.GetAllProducts();16 foreach(Products products in col)17 {18 if(products.Name == name)19 {20 return products;21 }22 }23 return null; 24 }25 }26}27
实体Products
1using System; 2 3namespace POFEAA 4{ 5 /**//// <summary> 6 /// Products 的摘要说明。 7 /// </summary> 8 public class Products 9 {10 public Products()11 {12 }13 public string Name14 {15 get16 {17 return _name;18 }19 set20 {21 _name = value;22 }23 }private string _name = string.Empty;24 }25}26
ProductsCollection
1using System; 2 3using System.Collections; 4 5namespace POFEAA 6{ 7 /**//// <summary> 8 /// ProductsCollection 的摘要说明。 9 /// </summary> 10 public class ProductsCollection:IList 11 { 12 private ArrayList list; 13 public ProductsCollection() 14 { 15 list = new ArrayList(); 16 } 17 IList 成员#region IList 成员 18 19 public bool IsReadOnly 20 { 21 get 22 { 23 return list.IsReadOnly; 24 } 25 } 26 27 public object this[int index] 28 { 29 get 30 { 31 return list[index]; 32 } 33 set 34 { 35 list[index] = value; 36 } 37 } 38 39 public void RemoveAt(int index) 40 { 41 list.RemoveAt(index); 42 } 43 44 void IList.Insert(int index, object value) 45 { 46 list.Insert(index,value); 47 } 48 public void Insert(int index,Products value) 49 { 50 ((IList)this).Insert(index,value); 51 } 52 53 void IList.Remove(object value) 54 { 55 list.Remove(value); 56 } 57 58 public void Remove(Products value) 59 { 60 ((IList)this).Remove(value); 61 } 62 63 bool IList.Contains(object value) 64 { 65 return list.Contains(value); 66 } 67 68 public bool Contains(Products value) 69 { 70 return ((IList)this).Contains(value); 71 } 72 73 public void Clear() 74 { 75 list.Clear(); 76 } 77 78 int IList.IndexOf(object value) 79 { 80 return list.IndexOf(value); 81 } 82 83 public int IndexOf(Products value) 84 { 85 return ((IList)this).IndexOf(value); 86 } 87 88 int IList.Add(object value) 89 { 90 return list.Add(value); 91 } 92 93 public int Add(Products value) 94 { 95 return ((IList)this).Add(value); 96 } 97 98 public bool IsFixedSize 99 {100 get101 {102 return list.IsFixedSize;103 }104 }105106 #endregion107108 ICollection 成员#region ICollection 成员109110 public bool IsSynchronized111 {112 get113 {114 return list.IsSynchronized;115 }116 }117118 public int Count119 {120 get121 {122 return list.Count;123 }124 }125126 public void CopyTo(Array array, int index)127 {128 list.CopyTo(array,index);129 }130131 public object SyncRoot132 {133 get134 {135 return list.SyncRoot;136 }137 }138139 #endregion140141 IEnumerable 成员#region IEnumerable 成员142143 public IEnumerator GetEnumerator()144 {145 // TODO: 添加 ProductsCollection.GetEnumerator 实现146 return list.GetEnumerator();147 }148149 #endregion150 }151}152
客户代码
1 private void Button1_Click(object sender, System.EventArgs e)2 {3 Products products = TransactionScript.GetProducts("1");4 Label1.Text = products.Name;5 }
转载于:https://www.cnblogs.com/nanshouyong326/archive/2006/11/29/576317.html