Clover.NET coverage report - Coverage

Coverage timestamp: Friday, May 20, 2005 9:17:00 PM

File Stats: LOC: 239   Methods: 8
NCLOC: 132 Classes: 1
 
Source File Conditionals Statements Methods TOTAL
TypeHandlers\TypeHandlerFactory.cs 93.8 % 100.0 % 100.0 % 98.8 %
coverage coverage
1  
2   #region Apache Notice
3   /*****************************************************************************
4   * $Header: $
5   * $Revision: $
6   * $Date: $
7   *
8   * iBATIS.NET Data Mapper
9   * Copyright (C) 2004 - Gilles Bayon
10   *
11   *
12   * Licensed under the Apache License, Version 2.0 (the "License");
13   * you may not use this file except in compliance with the License.
14   * You may obtain a copy of the License at
15   *
16   * http://www.apache.org/licenses/LICENSE-2.0
17   *
18   * Unless required by applicable law or agreed to in writing, software
19   * distributed under the License is distributed on an "AS IS" BASIS,
20   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21   * See the License for the specific language governing permissions and
22   * limitations under the License.
23   *
24   ********************************************************************************/
25   #endregion
26  
27   #region Using
28  
29   using System;
30   using System.Collections.Specialized;
31  
32   #endregion
33  
34   namespace IBatisNet.DataMapper.TypeHandlers
35   {
36   /// <summary>
37   /// Not much of a suprise, this is a factory class for TypeHandler objects.
38   /// </summary>
39   internal class TypeHandlerFactory
40   {
41  
42   #region Fields
43  
44   private HybridDictionary _typeHandlerMap = new HybridDictionary();
45   private ITypeHandler _unknownTypeHandler = null;
46   private const string NULL = "_NULL_TYPE_";
47  
48   #endregion
49  
50   #region Constructor
51  
52   /// <summary>
53   /// Constructor
54   /// </summary>
55 170 public TypeHandlerFactory()
56   {
57 170 ITypeHandler handler = null;
58  
59 170 handler = new BooleanTypeHandler();
60 170 this.Register(typeof(bool), handler); // key= "System.Boolean"
61  
62 170 handler = new ByteTypeHandler();
63 170 this.Register(typeof(Byte), handler);
64  
65 170 handler = new CharTypeHandler();
66 170 this.Register(typeof(Char), handler);
67  
68 170 handler = new DateTimeTypeHandler();
69 170 this.Register(typeof(DateTime), handler);
70  
71 170 handler = new DecimalTypeHandler();
72 170 this.Register(typeof(Decimal), handler);
73  
74 170 handler = new DoubleTypeHandler();
75 170 this.Register(typeof(Double), handler);
76  
77 170 handler = new Int16TypeHandler();
78 170 this.Register(typeof(Int16), handler);
79  
80 170 handler = new Int32TypeHandler();
81 170 this.Register(typeof(Int32), handler);
82  
83 170 handler = new Int64TypeHandler();
84 170 this.Register(typeof(Int64), handler);
85  
86 170 handler = new SingleTypeHandler();
87 170 this.Register(typeof(Single), handler);
88  
89 170 handler = new StringTypeHandler();
90 170 this.Register(typeof(String), handler);
91  
92 170 handler = new GuidTypeHandler();
93 170 this.Register(typeof(Guid), handler);
94  
95 170 handler = new TimeSpanTypeHandler();
96 170 this.Register(typeof(TimeSpan), handler);
97  
98 170 handler = new ByteArrayTypeHandler();
99 170 this.Register(typeof(Byte[]), handler);
100  
101 170 handler = new ObjectTypeHandler();
102 170 this.Register(typeof(object), handler);
103  
104 170 handler = new EnumTypeHandler();
105 170 this.Register( typeof(System.Enum), handler);
106  
107 170 _unknownTypeHandler = new UnknownTypeHandler(this);
108  
109   }
110  
111   #endregion
112  
113   #region Methods
114  
115   /// <summary>
116   /// Get a TypeHandler for a Type
117   /// </summary>
118   /// <param name="type">the Type you want a TypeHandler for</param>
119   /// <returns>the handler</returns>
120 613 public ITypeHandler GetTypeHandler(Type type)
121   {
122 613 return GetTypeHandler(type, null);
123   }
124  
125   /// <summary>
126   /// Get a TypeHandler for a type
127   /// </summary>
128   /// <param name="type">the type you want a TypeHandler for</param>
129   /// <param name="dbType">the database type</param>
130   /// <returns>the handler</returns>
131 78613 public ITypeHandler GetTypeHandler(Type type, string dbType)
132   {
133 78613 if (type.IsEnum)
134   {
135 529 return this.GetPrivateTypeHandler(typeof(System.Enum), dbType);
136   }
137   else
138   {
139 78084 return this.GetPrivateTypeHandler(type, dbType);
140   }
141   }
142  
143   /// <summary>
144   /// Get a TypeHandler for a type and a dbType type
145   /// </summary>
146   /// <param name="type">the type</param>
147   /// <param name="dbType">the dbType type</param>
148   /// <returns>the handler</returns>
149 78613 private ITypeHandler GetPrivateTypeHandler(Type type, string dbType)
150   {
151 78613 HybridDictionary dbTypeHandlerMap = (HybridDictionary) _typeHandlerMap[ type ];
152 78613 ITypeHandler handler = null;
153  
154 78613 if (dbTypeHandlerMap != null)
155   {
156 47671 if (dbType==null)
157   {
158 43720 handler = (ITypeHandler) dbTypeHandlerMap[ NULL ];
159   }
160   else
161   {
162 3951 handler = (ITypeHandler) dbTypeHandlerMap[ dbType ];
163 3951 if (handler == null)
164   {
165 3611 handler = (ITypeHandler) dbTypeHandlerMap[ NULL ];
166   }
167   }
168  
169   }
170 78613 return handler;
171   }
172  
173  
174   /// <summary>
175   /// Register (add) a type handler for a type
176   /// </summary>
177   /// <param name="type">the type</param>
178   /// <param name="handler">the handler instance</param>
179 2720 public void Register(Type type, ITypeHandler handler)
180   {
181 2720 this.Register(type, null, handler);
182   }
183  
184   /// <summary>
185   /// Register (add) a type handler for a type and dbType
186   /// </summary>
187   /// <param name="type">the type</param>
188   /// <param name="dbType">the dbType</param>
189   /// <param name="handler">the handler instance</param>
190 2890 public void Register(Type type, string dbType, ITypeHandler handler)
191   {
192 2890 HybridDictionary map = (HybridDictionary) _typeHandlerMap[ type ];
193 2890 if (map == null)
194   {
195 2720 map = new HybridDictionary();
196 2720 _typeHandlerMap.Add(type, map) ;
197   }
198 2890 if (dbType==null)
199   {
200 2720 map.Add(NULL, handler);
201   }
202   else
203   {
204 170 map.Add(dbType, handler);
205   }
206   }
207  
208   /// <summary>
209   /// When in doubt, get the "unknown" type handler
210   /// </summary>
211   /// <returns>if I told you, it would not be unknown, would it?</returns>
212 89128 public ITypeHandler GetUnkownTypeHandler()
213   {
214 89128 return _unknownTypeHandler;
215   }
216  
217   /// <summary>
218   ///
219   /// </summary>
220   /// <param name="type"></param>
221   /// <returns></returns>
222 385 public bool IsSimpleType(Type type)
223   {
224 385 bool result = false;
225 385 if (type != null)
226   {
227 385 ITypeHandler handler = this.GetTypeHandler(type, null);
228 385 if (handler != null)
229   {
230 213 result = handler.IsSimpleType;
231   }
232   }
233 385 return result;
234   }
235  
236   #endregion
237   }
238   }
239