设计模式

1.xxxx后台用到了两个设计模式,工厂模式和单体模式
先看看工厂模式
public interface IDaoFactory抽象工厂,定义各种XXXDao GetXXXDao()方法。Dao是用来和数据库打交道的如:Save,update等等

看看单体模式,在用Nhibernate操作数据库时

  1 using System.Runtime.Remoting.Messaging;
2 using System.Web;
3 using NHibernate;
4 using NHibernate.Cache;
5 using NHibernate.Cfg;
6 using XXXX.Srm2008.Common.Util;
7 using NHibernate.Type;
8 using NHibernate.Id;
9 using System.Collections;
10 using NHibernate.Engine;
11 using NHibernate.Dialect;
12 using System.Data;
13
14
15 namespace Avon.Srm2008.Core
16 {
17 ///<summary>
18 /// Handles creation and management of sessions and transactions. It is a singleton because
19 /// building the initial session factory is very expensive. Inspiration for this class came
20 /// from Chapter 8 of Hibernate in Action by Bauer and King. Although it is a sealed singleton
21 /// you can use TypeMock (http://www.typemock.com) for more flexible testing.
22 ///</summary>
23 public sealed class NHibernateSessionManager
24 {
25 #region Thread-safe, lazy Singleton
26
27 ///<summary>
28 /// This is a thread-safe, lazy singleton. See http://www.yoda.arachsys.com/csharp/singleton.html
29 /// for more details about its implementation.
30 ///</summary>
31 public static NHibernateSessionManager Instance
32 {
33 get
34 {
35 return Nested.NHibernateSessionManager;
36 }
37 }
38
39 ///<summary>
40 /// Initializes the NHibernate session factory upon instantiation.
41 ///</summary>
42 private NHibernateSessionManager()
43 {
44 InitSessionFactory();
45 }
46
47 ///<summary>
48 /// Assists with ensuring thread-safe, lazy singleton
49 ///</summary>
50 private class Nested
51 {
52 static Nested() { }
53 internal static readonly NHibernateSessionManager NHibernateSessionManager = new NHibernateSessionManager();
54 }
55
56 #endregion
57
58 private void InitSessionFactory()
59 {
60 sessionFactory = new Configuration().Configure().BuildSessionFactory();
61 }
62
63 ///<summary>
64 /// Allows you to register an interceptor on a new session. This may not be called if there is already
65 /// an open session attached to the HttpContext. If you have an interceptor to be used, modify
66 /// the HttpModule to call this before calling BeginTransaction().
67 ///</summary>
68 public void RegisterInterceptor(IInterceptor interceptor)
69 {
70 ISession session = ContextSession;
71
72 if (session != null && session.IsOpen)
73 {
74 throw new CacheException("You cannot register an interceptor once a session has already been opened");
75 }
76
77 GetSession(interceptor);
78 }
79
80 public ISession GetSession()
81 {
82 return GetSession(null);
83 }
84
85 ///<summary>
86 /// Gets a session with or without an interceptor. This method is not called directly; instead,
87 /// it gets invoked from other public methods.
88 ///</summary>
89 private ISession GetSession(IInterceptor interceptor)
90 {
91 ISession session = ContextSession;
92
93 if (session == null)
94 {
95 if (interceptor != null)
96 {
97 session = sessionFactory.OpenSession(interceptor);
98 }
99 else
100 {
101 session = sessionFactory.OpenSession();
102 }
103
104 ContextSession = session;
105 }
106
107 CheckHelper.Ensure(session != null, "session was null");
108
109 return session;
110 }
111
112 ///<summary>
113 /// Flushes anything left in the session and closes the connection.
114 ///</summary>
115 public void CloseSession()
116 {
117 ISession session = ContextSession;
118
119 if (session != null && session.IsOpen)
120 {
121 //session.Flush();
122 session.Close();
123 }
124
125 ContextSession = null;
126 }
127
128 public void BeginTransaction()
129 {
130 ITransaction transaction = ContextTransaction;
131
132 if (transaction == null)
133 {
134 transaction = GetSession().BeginTransaction();
135 ContextTransaction = transaction;
136 }
137 }
138
139 public void CommitTransaction()
140 {
141 ITransaction transaction = ContextTransaction;
142
143 try
144 {
145 if (HasOpenTransaction())
146 {
147 transaction.Commit();
148 ContextTransaction = null;
149 }
150 }
151 catch (HibernateException)
152 {
153 RollbackTransaction();
154 throw;
155 }
156 }
157
158 public bool HasOpenTransaction()
159 {
160 ITransaction transaction = ContextTransaction;
161
162 return transaction != null && !transaction.WasCommitted && !transaction.WasRolledBack;
163 }
164
165 public void RollbackTransaction()
166 {
167 ITransaction transaction = ContextTransaction;
168
169 try
170 {
171 if (HasOpenTransaction())
172 {
173 transaction.Rollback();
174 }
175
176 ContextTransaction = null;
177 }
178 finally
179 {
180 CloseSession();
181 }
182 }
183
184 public object GetSequenceNextValue(IType type, string sequenceName)
185 {
186 SequenceGenerator seq = new SequenceGenerator();
187 Hashtable param = new Hashtable();
188 param.Add("sequence", sequenceName);
189 seq.Configure(type, param, new Oracle9Dialect());
190 return seq.Generate((ISessionImplementor)GetSession(), null);
191 }
192
193 ///<summary>
194 /// If within a web context, this uses <see cref="HttpContext" /> instead of the WinForms
195 /// specific <see cref="CallContext" />. Discussion concerning this found at
196 ///http://forum.springframework.net/showthread.php?t=572.
197 ///</summary>
198 private ITransaction ContextTransaction
199 {
200 get
201 {
202 if (IsInWebContext())
203 {
204 return (ITransaction)HttpContext.Current.Items[TRANSACTION_KEY];
205 }
206 else
207 {
208 return (ITransaction)CallContext.GetData(TRANSACTION_KEY);
209 }
210 }
211 set
212 {
213 if (IsInWebContext())
214 {
215 HttpContext.Current.Items[TRANSACTION_KEY] = value;
216 }
217 else
218 {
219 CallContext.SetData(TRANSACTION_KEY, value);
220 }
221 }
222 }
223
224 ///<summary>
225 /// If within a web context, this uses <see cref="HttpContext" /> instead of the WinForms
226 /// specific <see cref="CallContext" />. Discussion concerning this found at
227 ///http://forum.springframework.net/showthread.php?t=572.
228 ///</summary>
229 private ISession ContextSession
230 {
231 get
232 {
233 if (IsInWebContext())
234 {
235 return (ISession)HttpContext.Current.Items[SESSION_KEY];
236 }
237 else
238 {
239 return (ISession)CallContext.GetData(SESSION_KEY);
240 }
241 }
242 set
243 {
244 if (IsInWebContext())
245 {
246 HttpContext.Current.Items[SESSION_KEY] = value;
247 }
248 else
249 {
250 CallContext.SetData(SESSION_KEY, value);
251 }
252 }
253 }
254
255 private bool IsInWebContext()
256 {
257 return HttpContext.Current != null;
258 }
259
260 private const string TRANSACTION_KEY = "CONTEXT_TRANSACTION";
261 private const string SESSION_KEY = "CONTEXT_SESSION";
262 private ISessionFactory sessionFactory;
263 }
264 }



posted @ 2011-11-03 09:08  蓝鸿鹄  阅读(244)  评论(1编辑  收藏  举报