Transaction Script
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
TransactionScript
1 using System;
using System;
2
3 namespace POFEAA
namespace POFEAA
4

 {
{
5
 /**//// <summary>
    /**//// <summary>
6 /// TransactionScript 的摘要说明。
    /// TransactionScript 的摘要说明。
7 /// </summary>
    /// </summary>
8 public class TransactionScript
    public class TransactionScript
9
 
     {
{
10 public TransactionScript()
        public TransactionScript()
11
 
         {
{
12 }
        }
13 public static Products GetProducts(string name)
        public static Products GetProducts(string name)
14
 
         {
{
15 ProductsCollection col = ProductsDataGateWay.GetAllProducts();
            ProductsCollection col = ProductsDataGateWay.GetAllProducts();
16 foreach(Products products in col)
            foreach(Products products in col)
17
 
             {
{
18 if(products.Name == name)
                if(products.Name == name)
19
 
                 {
{
20 return products;
                    return products;
21 }
                }
22 }
            }
23 return null;
            return null; 
24 }
        }
25 }
    }
26 }
}
27 

 实体Products
实体Products
1 using System;
using System;
2
3 namespace POFEAA
namespace POFEAA
4

 {
{
5
 /**//// <summary>
    /**//// <summary>
6 /// Products 的摘要说明。
    /// Products 的摘要说明。
7 /// </summary>
    /// </summary>
8 public class Products
    public class Products
9
 
     {
{
10 public Products()
        public Products()
11
 
         {
{
12 }
        }
13 public string Name
        public string Name
14
 
         {
{
15 get
            get
16
 
             {
{
17 return _name;
                return _name;
18 }
            }
19 set
            set
20
 
             {
{
21 _name = value;
                _name = value;
22 }
            }
23 }private string _name = string.Empty;
        }private string _name = string.Empty;
24 }
    }
25 }
}
26 

 ProductsCollection
ProductsCollection
1 using System;
using System;
2
3 using System.Collections;
using System.Collections;
4
5 namespace POFEAA
namespace POFEAA
6

 {
{
7
 /**//// <summary>
    /**//// <summary>
8 /// ProductsCollection 的摘要说明。
    /// ProductsCollection 的摘要说明。
9 /// </summary>
    /// </summary>
10 public class ProductsCollection:IList
    public class ProductsCollection:IList
11
 
     {
{
12 private ArrayList list;
        private ArrayList list;
13 public ProductsCollection()
        public ProductsCollection()
14
 
         {
{
15 list = new ArrayList();
            list = new ArrayList();
16 }
        }
17
 IList 成员#region IList 成员
        IList 成员#region IList 成员
18
19 public bool IsReadOnly
        public bool IsReadOnly
20
 
         {
{
21 get
            get
22
 
             {
{
23 return list.IsReadOnly;
                return list.IsReadOnly;
24 }
            }
25 }
        }
26
27 public object this[int index]
        public object this[int index]
28
 
         {
{
29 get
            get
30
 
             {
{
31 return list[index];
                return list[index];
32 }
            }
33 set
            set
34
 
             {
{
35 list[index] = value;
                list[index] = value;
36 }
            }
37 }
        }
38
39 public void RemoveAt(int index)
        public void RemoveAt(int index)
40
 
         {
{
41 list.RemoveAt(index);
            list.RemoveAt(index);
42 }
        }
43
44 void IList.Insert(int index, object value)
        void IList.Insert(int index, object value)
45
 
         {
{
46 list.Insert(index,value);
            list.Insert(index,value);
47 }
        }
48 public void Insert(int index,Products value)
        public void Insert(int index,Products value)
49
 
         {
{
50 ((IList)this).Insert(index,value);
            ((IList)this).Insert(index,value);
51 }
        }
52
53 void IList.Remove(object value)
        void IList.Remove(object value)
54
 
         {
{
55 list.Remove(value);
            list.Remove(value);
56 }
        }
57
58 public void Remove(Products value)
        public void Remove(Products value)
59
 
         {
{
60 ((IList)this).Remove(value);
            ((IList)this).Remove(value);
61 }
        }
62
63 bool IList.Contains(object value)
        bool IList.Contains(object value)
64
 
         {
{
65 return list.Contains(value);
            return list.Contains(value);
66 }
        }
67
68 public bool Contains(Products value)
        public bool Contains(Products value)
69
 
         {
{
70 return ((IList)this).Contains(value);
            return ((IList)this).Contains(value);
71 }
        }
72
73 public void Clear()
        public void Clear()
74
 
         {
{
75 list.Clear();
            list.Clear();
76 }
        }
77
78 int IList.IndexOf(object value)
        int IList.IndexOf(object value)
79
 
         {
{
80 return list.IndexOf(value);
            return list.IndexOf(value);
81 }
        }
82
83 public int IndexOf(Products value)
        public int IndexOf(Products value)
84
 
         {
{
85 return ((IList)this).IndexOf(value);
            return ((IList)this).IndexOf(value);
86 }
        }
87
88 int IList.Add(object value)
        int IList.Add(object value)
89
 
         {
{
90 return list.Add(value);
            return list.Add(value);
91 }
        }
92
93 public int Add(Products value)
        public int Add(Products value)
94
 
         {
{
95 return ((IList)this).Add(value);
            return ((IList)this).Add(value);
96 }
        }
97
98 public bool IsFixedSize
        public bool IsFixedSize
99
 
         {
{
100 get
            get
101
 
             {
{
102 return list.IsFixedSize;
                return list.IsFixedSize;
103 }
            }
104 }
        }
105
106 #endregion
        #endregion
107
108
 ICollection 成员#region ICollection 成员
        ICollection 成员#region ICollection 成员
109
110 public bool IsSynchronized
        public bool IsSynchronized
111
 
         {
{
112 get
            get
113
 
             {
{
114 return list.IsSynchronized;
                return list.IsSynchronized;
115 }
            }
116 }
        }
117
118 public int Count
        public int Count
119
 
         {
{
120 get
            get
121
 
             {
{
122 return list.Count;
                return list.Count;
123 }
            }
124 }
        }
125
126 public void CopyTo(Array array, int index)
        public void CopyTo(Array array, int index)
127
 
         {
{
128 list.CopyTo(array,index);
            list.CopyTo(array,index);
129 }
        }
130
131 public object SyncRoot
        public object SyncRoot
132
 
         {
{
133 get
            get
134
 
             {
{
135 return list.SyncRoot;
                return list.SyncRoot;
136 }
            }
137 }
        }
138
139 #endregion
        #endregion
140
141
 IEnumerable 成员#region IEnumerable 成员
        IEnumerable 成员#region IEnumerable 成员
142
143 public IEnumerator GetEnumerator()
        public IEnumerator GetEnumerator()
144
 
         {
{
145 // TODO:  添加 ProductsCollection.GetEnumerator 实现
            // TODO:  添加 ProductsCollection.GetEnumerator 实现
146 return list.GetEnumerator();
            return list.GetEnumerator();
147 }
        }
148
149 #endregion
        #endregion
150 }
    }
151 }
}
152 

 客户代码
客户代码
1 private void Button1_Click(object sender, System.EventArgs e)
    private void Button1_Click(object sender, System.EventArgs e)
2
 
         {
{
3 Products products = TransactionScript.GetProducts("1");
             Products products = TransactionScript.GetProducts("1");
4 Label1.Text = products.Name;
            Label1.Text = products.Name;
5 }
        }
按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
TransactionScript1
 using System;
using System;2

3
 namespace POFEAA
namespace POFEAA4


 {
{5

 /**//// <summary>
    /**//// <summary>6
 /// TransactionScript 的摘要说明。
    /// TransactionScript 的摘要说明。7
 /// </summary>
    /// </summary>8
 public class TransactionScript
    public class TransactionScript9

 
     {
{10
 public TransactionScript()
        public TransactionScript()11

 
         {
{12
 }
        }13
 public static Products GetProducts(string name)
        public static Products GetProducts(string name)14

 
         {
{15
 ProductsCollection col = ProductsDataGateWay.GetAllProducts();
            ProductsCollection col = ProductsDataGateWay.GetAllProducts();16
 foreach(Products products in col)
            foreach(Products products in col)17

 
             {
{18
 if(products.Name == name)
                if(products.Name == name)19

 
                 {
{20
 return products;
                    return products;21
 }
                }22
 }
            }23
 return null;
            return null; 24
 }
        }25
 }
    }26
 }
}27


 实体Products
实体Products1
 using System;
using System;2

3
 namespace POFEAA
namespace POFEAA4


 {
{5

 /**//// <summary>
    /**//// <summary>6
 /// Products 的摘要说明。
    /// Products 的摘要说明。7
 /// </summary>
    /// </summary>8
 public class Products
    public class Products9

 
     {
{10
 public Products()
        public Products()11

 
         {
{12
 }
        }13
 public string Name
        public string Name14

 
         {
{15
 get
            get16

 
             {
{17
 return _name;
                return _name;18
 }
            }19
 set
            set20

 
             {
{21
 _name = value;
                _name = value;22
 }
            }23
 }private string _name = string.Empty;
        }private string _name = string.Empty;24
 }
    }25
 }
}26


 ProductsCollection
ProductsCollection1
 using System;
using System;2

3
 using System.Collections;
using System.Collections;4

5
 namespace POFEAA
namespace POFEAA6


 {
{7

 /**//// <summary>
    /**//// <summary>8
 /// ProductsCollection 的摘要说明。
    /// ProductsCollection 的摘要说明。9
 /// </summary>
    /// </summary>10
 public class ProductsCollection:IList
    public class ProductsCollection:IList11

 
     {
{12
 private ArrayList list;
        private ArrayList list;13
 public ProductsCollection()
        public ProductsCollection()14

 
         {
{15
 list = new ArrayList();
            list = new ArrayList();16
 }
        }17

 IList 成员#region IList 成员
        IList 成员#region IList 成员18

19
 public bool IsReadOnly
        public bool IsReadOnly20

 
         {
{21
 get
            get22

 
             {
{23
 return list.IsReadOnly;
                return list.IsReadOnly;24
 }
            }25
 }
        }26

27
 public object this[int index]
        public object this[int index]28

 
         {
{29
 get
            get30

 
             {
{31
 return list[index];
                return list[index];32
 }
            }33
 set
            set34

 
             {
{35
 list[index] = value;
                list[index] = value;36
 }
            }37
 }
        }38

39
 public void RemoveAt(int index)
        public void RemoveAt(int index)40

 
         {
{41
 list.RemoveAt(index);
            list.RemoveAt(index);42
 }
        }43

44
 void IList.Insert(int index, object value)
        void IList.Insert(int index, object value)45

 
         {
{46
 list.Insert(index,value);
            list.Insert(index,value);47
 }
        }48
 public void Insert(int index,Products value)
        public void Insert(int index,Products value)49

 
         {
{50
 ((IList)this).Insert(index,value);
            ((IList)this).Insert(index,value);51
 }
        }52

53
 void IList.Remove(object value)
        void IList.Remove(object value)54

 
         {
{55
 list.Remove(value);
            list.Remove(value);56
 }
        }57

58
 public void Remove(Products value)
        public void Remove(Products value)59

 
         {
{60
 ((IList)this).Remove(value);
            ((IList)this).Remove(value);61
 }
        }62

63
 bool IList.Contains(object value)
        bool IList.Contains(object value)64

 
         {
{65
 return list.Contains(value);
            return list.Contains(value);66
 }
        }67

68
 public bool Contains(Products value)
        public bool Contains(Products value)69

 
         {
{70
 return ((IList)this).Contains(value);
            return ((IList)this).Contains(value);71
 }
        }72

73
 public void Clear()
        public void Clear()74

 
         {
{75
 list.Clear();
            list.Clear();76
 }
        }77

78
 int IList.IndexOf(object value)
        int IList.IndexOf(object value)79

 
         {
{80
 return list.IndexOf(value);
            return list.IndexOf(value);81
 }
        }82

83
 public int IndexOf(Products value)
        public int IndexOf(Products value)84

 
         {
{85
 return ((IList)this).IndexOf(value);
            return ((IList)this).IndexOf(value);86
 }
        }87

88
 int IList.Add(object value)
        int IList.Add(object value)89

 
         {
{90
 return list.Add(value);
            return list.Add(value);91
 }
        }92

93
 public int Add(Products value)
        public int Add(Products value)94

 
         {
{95
 return ((IList)this).Add(value);
            return ((IList)this).Add(value);96
 }
        }97

98
 public bool IsFixedSize
        public bool IsFixedSize99

 
         {
{100
 get
            get101

 
             {
{102
 return list.IsFixedSize;
                return list.IsFixedSize;103
 }
            }104
 }
        }105

106
 #endregion
        #endregion107

108

 ICollection 成员#region ICollection 成员
        ICollection 成员#region ICollection 成员109

110
 public bool IsSynchronized
        public bool IsSynchronized111

 
         {
{112
 get
            get113

 
             {
{114
 return list.IsSynchronized;
                return list.IsSynchronized;115
 }
            }116
 }
        }117

118
 public int Count
        public int Count119

 
         {
{120
 get
            get121

 
             {
{122
 return list.Count;
                return list.Count;123
 }
            }124
 }
        }125

126
 public void CopyTo(Array array, int index)
        public void CopyTo(Array array, int index)127

 
         {
{128
 list.CopyTo(array,index);
            list.CopyTo(array,index);129
 }
        }130

131
 public object SyncRoot
        public object SyncRoot132

 
         {
{133
 get
            get134

 
             {
{135
 return list.SyncRoot;
                return list.SyncRoot;136
 }
            }137
 }
        }138

139
 #endregion
        #endregion140

141

 IEnumerable 成员#region IEnumerable 成员
        IEnumerable 成员#region IEnumerable 成员142

143
 public IEnumerator GetEnumerator()
        public IEnumerator GetEnumerator()144

 
         {
{145
 // TODO:  添加 ProductsCollection.GetEnumerator 实现
            // TODO:  添加 ProductsCollection.GetEnumerator 实现146
 return list.GetEnumerator();
            return list.GetEnumerator();147
 }
        }148

149
 #endregion
        #endregion150
 }
    }151
 }
}152


 客户代码
客户代码1
 private void Button1_Click(object sender, System.EventArgs e)
    private void Button1_Click(object sender, System.EventArgs e)2

 
         {
{3
 Products products = TransactionScript.GetProducts("1");
             Products products = TransactionScript.GetProducts("1");4
 Label1.Text = products.Name;
            Label1.Text = products.Name;5
 }
        }.jpg) 
  
 
                    
                
 
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号