Proxy 为其他对象提供一个代理(surrogate)或者占位符(placeholder),从而完成对其他对象的访问。
自定义代理类
1using System;
2using System.Data;
3
4namespace DataLayer
5{
6 /**//// <summary>
7 /// This class strictly forwards every call to a subject object. To
8 /// intercept any specific call, create a subclass.
9 /// </summary>
10 public class DataReaderProxy : IDataReader
11 {
12 private IDataReader _subject;
13 /**//// <summary>
14 /// Create a proxy for the given subject.
15 /// </summary>
16 /// <param name="subject">The real reader to proxy for</param>
17 public DataReaderProxy(IDataReader subject)
18 {
19 _subject = subject;
20 }
21 // properties for IDataRecord
22 public virtual int FieldCount
23 {
24 get
25 {
26 return _subject.FieldCount;
27 }
28 }
29 public virtual object this [string name]
30 {
31 get
32 {
33 return _subject[name];
34 }
35 }
36 public virtual object this [int index]
37 {
38 get
39 {
40 return _subject[index];
41 }
42 }
43 // properties for IDataReader
44 public virtual int Depth
45 {
46 get
47 {
48 return _subject.Depth;
49 }
50 }
51 public virtual bool IsClosed
52 {
53 get
54 {
55 return _subject.IsClosed;
56 }
57 }
58 // methods for IDataRecord
59 public virtual bool GetBoolean(int i)
60 {
61 return _subject.GetBoolean(i);
62 }
63 public virtual byte GetByte(int i)
64 {
65 return _subject.GetByte(i);
66 }
67 public virtual long GetBytes(
68 int i,
69 long fieldoffset,
70 byte[] buffer,
71 int bufferoffset,
72 int length
73 )
74 {
75 return _subject.GetBytes(i, fieldoffset, buffer, bufferoffset, length);
76 }
77 public virtual char GetChar(int i)
78 {
79 return _subject.GetChar(i);
80 }
81 public virtual long GetChars(
82 int i,
83 long fieldoffset,
84 char[] buffer,
85 int bufferoffset,
86 int length
87 )
88 {
89 return _subject.GetChars(i, fieldoffset, buffer, bufferoffset, length);
90 }
91 public virtual IDataReader GetData(int i)
92 {
93 return _subject.GetData(i);
94 }
95 public virtual string GetDataTypeName(int i)
96 {
97 return _subject.GetDataTypeName(i);
98 }
99 public virtual DateTime GetDateTime(int i)
100 {
101 return _subject.GetDateTime(i);
102 }
103 public virtual decimal GetDecimal(int i)
104 {
105 return _subject.GetDecimal(i);
106 }
107 public virtual double GetDouble(int i)
108 {
109 return _subject.GetDouble(i);
110 }
111 public virtual Type GetFieldType(int i)
112 {
113 return _subject.GetFieldType(i);
114 }
115 public virtual float GetFloat(int i)
116 {
117 return _subject.GetFloat(i);
118 }
119 public virtual Guid GetGuid(int i)
120 {
121 return _subject.GetGuid(i);
122 }
123 public virtual short GetInt16(int i)
124 {
125 return _subject.GetInt16(i);
126 }
127 public virtual int GetInt32(int i)
128 {
129 return _subject.GetInt32(i);
130 }
131 public virtual long GetInt64(int i)
132 {
133 return _subject.GetInt64(i);
134 }
135 public virtual string GetName(int i)
136 {
137 return _subject.GetName(i);
138 }
139 public virtual int GetOrdinal(string name)
140 {
141 return _subject.GetOrdinal(name);
142 }
143 public virtual string GetString(int i)
144 {
145 return _subject.GetString(i);
146 }
147 public virtual object GetValue(int i)
148 {
149 return _subject.GetValue(i);
150 }
151 public virtual int GetValues(object [] values)
152 {
153 return _subject.GetValues(values);
154 }
155 public virtual bool IsDBNull(int i)
156 {
157 return _subject.IsDBNull(i);
158 }
159 // methods for IDataReader
160 public virtual int RecordsAffected
161 {
162 get
163 {
164 return _subject.RecordsAffected;
165 }
166 }
167 public virtual void Close()
168 {
169 _subject.Close();
170 }
171 public virtual DataTable GetSchemaTable()
172 {
173 return _subject.GetSchemaTable();
174 }
175 public virtual bool NextResult()
176 {
177 return _subject.NextResult();
178 }
179 public virtual bool Read()
180 {
181 return _subject.Read();
182 }
183 // methods for IDisposable
184 public virtual void Dispose()
185 {
186 _subject.Dispose();
187 }
188 }
189}
1using System;
2using System.Data;
3
4namespace DataLayer
5{
6 /**//// <summary>
7 /// This class strictly forwards every call to a subject object. To
8 /// intercept any specific call, create a subclass.
9 /// </summary>
10 public class DataReaderProxy : IDataReader
11 {
12 private IDataReader _subject;
13 /**//// <summary>
14 /// Create a proxy for the given subject.
15 /// </summary>
16 /// <param name="subject">The real reader to proxy for</param>
17 public DataReaderProxy(IDataReader subject)
18 {
19 _subject = subject;
20 }
21 // properties for IDataRecord
22 public virtual int FieldCount
23 {
24 get
25 {
26 return _subject.FieldCount;
27 }
28 }
29 public virtual object this [string name]
30 {
31 get
32 {
33 return _subject[name];
34 }
35 }
36 public virtual object this [int index]
37 {
38 get
39 {
40 return _subject[index];
41 }
42 }
43 // properties for IDataReader
44 public virtual int Depth
45 {
46 get
47 {
48 return _subject.Depth;
49 }
50 }
51 public virtual bool IsClosed
52 {
53 get
54 {
55 return _subject.IsClosed;
56 }
57 }
58 // methods for IDataRecord
59 public virtual bool GetBoolean(int i)
60 {
61 return _subject.GetBoolean(i);
62 }
63 public virtual byte GetByte(int i)
64 {
65 return _subject.GetByte(i);
66 }
67 public virtual long GetBytes(
68 int i,
69 long fieldoffset,
70 byte[] buffer,
71 int bufferoffset,
72 int length
73 )
74 {
75 return _subject.GetBytes(i, fieldoffset, buffer, bufferoffset, length);
76 }
77 public virtual char GetChar(int i)
78 {
79 return _subject.GetChar(i);
80 }
81 public virtual long GetChars(
82 int i,
83 long fieldoffset,
84 char[] buffer,
85 int bufferoffset,
86 int length
87 )
88 {
89 return _subject.GetChars(i, fieldoffset, buffer, bufferoffset, length);
90 }
91 public virtual IDataReader GetData(int i)
92 {
93 return _subject.GetData(i);
94 }
95 public virtual string GetDataTypeName(int i)
96 {
97 return _subject.GetDataTypeName(i);
98 }
99 public virtual DateTime GetDateTime(int i)
100 {
101 return _subject.GetDateTime(i);
102 }
103 public virtual decimal GetDecimal(int i)
104 {
105 return _subject.GetDecimal(i);
106 }
107 public virtual double GetDouble(int i)
108 {
109 return _subject.GetDouble(i);
110 }
111 public virtual Type GetFieldType(int i)
112 {
113 return _subject.GetFieldType(i);
114 }
115 public virtual float GetFloat(int i)
116 {
117 return _subject.GetFloat(i);
118 }
119 public virtual Guid GetGuid(int i)
120 {
121 return _subject.GetGuid(i);
122 }
123 public virtual short GetInt16(int i)
124 {
125 return _subject.GetInt16(i);
126 }
127 public virtual int GetInt32(int i)
128 {
129 return _subject.GetInt32(i);
130 }
131 public virtual long GetInt64(int i)
132 {
133 return _subject.GetInt64(i);
134 }
135 public virtual string GetName(int i)
136 {
137 return _subject.GetName(i);
138 }
139 public virtual int GetOrdinal(string name)
140 {
141 return _subject.GetOrdinal(name);
142 }
143 public virtual string GetString(int i)
144 {
145 return _subject.GetString(i);
146 }
147 public virtual object GetValue(int i)
148 {
149 return _subject.GetValue(i);
150 }
151 public virtual int GetValues(object [] values)
152 {
153 return _subject.GetValues(values);
154 }
155 public virtual bool IsDBNull(int i)
156 {
157 return _subject.IsDBNull(i);
158 }
159 // methods for IDataReader
160 public virtual int RecordsAffected
161 {
162 get
163 {
164 return _subject.RecordsAffected;
165 }
166 }
167 public virtual void Close()
168 {
169 _subject.Close();
170 }
171 public virtual DataTable GetSchemaTable()
172 {
173 return _subject.GetSchemaTable();
174 }
175 public virtual bool NextResult()
176 {
177 return _subject.NextResult();
178 }
179 public virtual bool Read()
180 {
181 return _subject.Read();
182 }
183 // methods for IDisposable
184 public virtual void Dispose()
185 {
186 _subject.Dispose();
187 }
188 }
189}
重写的特别代理
1using System;
2using System.Data;
3using DataLayer;
4
5/**//// <summary>
6/// Show that we know get our hooks into access to a data reader
7/// </summary>
8public class LimitingReader : DataReaderProxy
9{
10 /**//// <summary>
11 /// Just here to capture the subject
12 /// </summary>
13 /// <param name="subject">the reader we are a proxy for</param>
14 public LimitingReader(IDataReader subject) : base (subject)
15 {
16 }
17 /**//// <summary>
18 /// Show that we can intercept requests for apogee information.
19 /// </summary>
20 public override object this [string name]
21 {
22 get
23 {
24 if (String.Compare(name, "apogee", true) == 0) // same
25 {
26 return 0;
27 }
28 else
29 {
30 return base [name];
31 }
32 }
33 }
34}
1using System;
2using System.Data;
3using DataLayer;
4
5/**//// <summary>
6/// Show that we know get our hooks into access to a data reader
7/// </summary>
8public class LimitingReader : DataReaderProxy
9{
10 /**//// <summary>
11 /// Just here to capture the subject
12 /// </summary>
13 /// <param name="subject">the reader we are a proxy for</param>
14 public LimitingReader(IDataReader subject) : base (subject)
15 {
16 }
17 /**//// <summary>
18 /// Show that we can intercept requests for apogee information.
19 /// </summary>
20 public override object this [string name]
21 {
22 get
23 {
24 if (String.Compare(name, "apogee", true) == 0) // same
25 {
26 return 0;
27 }
28 else
29 {
30 return base [name];
31 }
32 }
33 }
34}
DataService类
1using System;
2using System.Data;
3using System.Data.OleDb;
4using System.Reflection;
5using System.IO;
6
7namespace Gof.Test.Proxy
8{
9 public delegate object BorrowReader(IDataReader reader);
10 public class DataService
11 {
12 public DataService()
13 {}
14 public static object GetDataReader(string sel,BorrowReader borrow)
15 {
16 try
17 {
18 using(OleDbConnection con = CreateConnection())
19 {
20 con.Open();
21 OleDbCommand com = new OleDbCommand();
22 com.Connection = con;
23 com.CommandText = sel;
24 OleDbDataReader reader = com.ExecuteReader();
25 return borrow(reader);
26 }
27 }
28 catch(Exception ex)
29 {
30 throw ex;//不处理了
31 }
32 }
33 public static OleDbConnection CreateConnection()
34 {
35 string fileName = GetFileName("db","oozinoz.mdb");
36 OleDbConnection con = new OleDbConnection();
37 con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + fileName;
38 return con;
39 }
40 public static String GetFileName(String dirName, String fileName)
41 {
42 String path;
43 // Can we find the file using the OOZINOZ environment variable?
44 String oozinozBase = Environment.GetEnvironmentVariable("OOZINOZ");
45 if (oozinozBase != null)
46 {
47 path = Path.Combine(Path.Combine(oozinozBase, dirName), fileName);
48 if (File.Exists(path))
49 {
50 return path;
51 }
52 }
53 // How 'bout relative to where the bin files are?
54 Assembly a = Assembly.GetAssembly(typeof(DataService));
55 DirectoryInfo thisDir = Directory.GetParent(a.Location);
56 DirectoryInfo parentDir = Directory.GetParent(thisDir.FullName);
57 path = Path.Combine(
58 parentDir.FullName,
59 dirName + Path.DirectorySeparatorChar + fileName);
60 if (File.Exists(path))
61 {
62 return path;
63 }
64 // Ok, how 'bout in the top-level directory?
65 path = Path.Combine(Path.Combine(@"\ConsoleApplication2", dirName), fileName);
66 if (File.Exists(path))
67 {
68 return path;
69 }
70 // dang
71 throw new Exception("FileFinder.GetFileName() cannot find " + fileName + " in directory " + dirName);
72
73 }
74 }
75}
1using System;
2using System.Data;
3using System.Data.OleDb;
4using System.Reflection;
5using System.IO;
6
7namespace Gof.Test.Proxy
8{
9 public delegate object BorrowReader(IDataReader reader);
10 public class DataService
11 {
12 public DataService()
13 {}
14 public static object GetDataReader(string sel,BorrowReader borrow)
15 {
16 try
17 {
18 using(OleDbConnection con = CreateConnection())
19 {
20 con.Open();
21 OleDbCommand com = new OleDbCommand();
22 com.Connection = con;
23 com.CommandText = sel;
24 OleDbDataReader reader = com.ExecuteReader();
25 return borrow(reader);
26 }
27 }
28 catch(Exception ex)
29 {
30 throw ex;//不处理了
31 }
32 }
33 public static OleDbConnection CreateConnection()
34 {
35 string fileName = GetFileName("db","oozinoz.mdb");
36 OleDbConnection con = new OleDbConnection();
37 con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + fileName;
38 return con;
39 }
40 public static String GetFileName(String dirName, String fileName)
41 {
42 String path;
43 // Can we find the file using the OOZINOZ environment variable?
44 String oozinozBase = Environment.GetEnvironmentVariable("OOZINOZ");
45 if (oozinozBase != null)
46 {
47 path = Path.Combine(Path.Combine(oozinozBase, dirName), fileName);
48 if (File.Exists(path))
49 {
50 return path;
51 }
52 }
53 // How 'bout relative to where the bin files are?
54 Assembly a = Assembly.GetAssembly(typeof(DataService));
55 DirectoryInfo thisDir = Directory.GetParent(a.Location);
56 DirectoryInfo parentDir = Directory.GetParent(thisDir.FullName);
57 path = Path.Combine(
58 parentDir.FullName,
59 dirName + Path.DirectorySeparatorChar + fileName);
60 if (File.Exists(path))
61 {
62 return path;
63 }
64 // Ok, how 'bout in the top-level directory?
65 path = Path.Combine(Path.Combine(@"\ConsoleApplication2", dirName), fileName);
66 if (File.Exists(path))
67 {
68 return path;
69 }
70 // dang
71 throw new Exception("FileFinder.GetFileName() cannot find " + fileName + " in directory " + dirName);
72
73 }
74 }
75}
客户代码
1 string sel = "SELECT * FROM ROCKET";
2 Gof.Test.Proxy.DataService.GetDataReader(sel, new Gof.Test.Proxy.BorrowReader(GetNames));
3
4 private static Object GetNames(System.Data.IDataReader reader)
5 {
6 System.Data.IDataReader proxy = new LimitingReader(reader);
7 while (proxy.Read())
8 {
9 Console.Write("{0,10} ", proxy["Name"]);
10 Console.Write("{0,7:C} ", proxy["price"]);
11 Console.Write("{0,5}", proxy["apogee"]);
12 Console.WriteLine();
13 }
14 Console.ReadLine();
15 return null;
16 }
1 string sel = "SELECT * FROM ROCKET";
2 Gof.Test.Proxy.DataService.GetDataReader(sel, new Gof.Test.Proxy.BorrowReader(GetNames));
3
4 private static Object GetNames(System.Data.IDataReader reader)
5 {
6 System.Data.IDataReader proxy = new LimitingReader(reader);
7 while (proxy.Read())
8 {
9 Console.Write("{0,10} ", proxy["Name"]);
10 Console.Write("{0,7:C} ", proxy["price"]);
11 Console.Write("{0,5}", proxy["apogee"]);
12 Console.WriteLine();
13 }
14 Console.ReadLine();
15 return null;
16 }