博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
设计模式
阅读量:5024 次
发布时间:2019-06-12

本文共 8894 字,大约阅读时间需要 29 分钟。

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 /// 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 /// 23 public sealed class NHibernateSessionManager 24 {
25 #region Thread-safe, lazy Singleton 26 27 /// 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 /// 31 public static NHibernateSessionManager Instance 32 {
33 get 34 {
35 return Nested.NHibernateSessionManager; 36 } 37 } 38 39 /// 40 /// Initializes the NHibernate session factory upon instantiation. 41 /// 42 private NHibernateSessionManager() 43 {
44 InitSessionFactory(); 45 } 46 47 /// 48 /// Assists with ensuring thread-safe, lazy singleton 49 /// 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 /// 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 /// 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 /// 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 /// 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 /// 113 /// Flushes anything left in the session and closes the connection. 114 /// 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 /// 194 /// If within a web context, this uses
instead of the WinForms 195 /// specific
. Discussion concerning this found at 196 ///http://forum.springframework.net/showthread.php?t=572. 197 ///
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 /// 225 /// If within a web context, this uses
instead of the WinForms 226 /// specific
. Discussion concerning this found at 227 ///http://forum.springframework.net/showthread.php?t=572. 228 ///
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 }

转载于:https://www.cnblogs.com/chester/archive/2011/11/03/2233996.html

你可能感兴趣的文章
linux下设置固定IP的方法
查看>>
VMware虚拟机下Linux系统的全屏显示
查看>>
net core体系-web应用程序-4asp.net core2.0 项目实战(任务管理系统)-2项目搭建
查看>>
高效的jQuery
查看>>
ubuntu 16.04 (软件应用)-输入法
查看>>
windos7修复引导扇区
查看>>
Leetcode总结之Backtracking
查看>>
Android开发学习之路-图片颜色获取器开发(1)
查看>>
StackExchange.Redis 官方文档(一) Basics
查看>>
nupkg 之破解 nodejs+electron-packager 打包exe的解包
查看>>
Objective-C 使用 C++类
查看>>
浅谈之高级查询over(partition by)
查看>>
Notes: CRM Analytics–BI from a CRM perspective (2)
查看>>
graphite custom functions
查看>>
列出所有的属性键
查看>>
js获取请求地址后面带的参数
查看>>
[原创]使用java批量修改文件编码(ANSI-->UTF-8)
查看>>
设计模式のCompositePattern(组合模式)----结构模式
查看>>
二进制集合枚举子集
查看>>
磁盘管理
查看>>