Commits

Kamyar Inanloo committed 2ae52f9

Fix: DataReader close for IEnumerable.
Add: Disposable feature.
Fix: Nullable return types.

  • Participants
  • Parent commits d93f23d

Comments (0)

Files changed (4)

File Simple.Data.Extension/Properties/AssemblyInfo.cs

 // You can specify all the values or you can default the Build and Revision Numbers 
 // by using the '*' as shown below:
 // [assembly: AssemblyVersion("1.0.*")]
-[assembly: AssemblyVersion("2.0.4.0")]
-[assembly: AssemblyFileVersion("2.0.4.0")]
+[assembly: AssemblyVersion("2.0.7.0")]
+[assembly: AssemblyFileVersion("2.0.7.0")]

File Simple.Data.Extension/Setup.cs

 {
 	using System;
 	using System.Collections.Generic;
-	using Castle.DynamicProxy;		
+	using Castle.DynamicProxy;
 
 	/// <summary>
 	/// This class is the only thing you have to consider using: Sets up the whole things!
 	public static class Setup
 	{
 		internal static StoredProcedureInterceptor interceptor;
-		internal static Dictionary<Type, string> connectionStringNames = new Dictionary<Type,string>();
+		internal static Dictionary<Type, string> connectionStringNames = new Dictionary<Type, string>();		
 
 		static Setup()
 		{
 		/// </summary>
 		/// <typeparam name="T"></typeparam>
 		/// <returns></returns>
-		public static T GetInstance<T>() where T: class
+		public static T GetInstance<T>() where T : class
 		{
 			var proxyGen = new ProxyGenerator();
 			return proxyGen.CreateInterfaceProxyWithoutTarget<T>(new StoredProcedureInterceptor());

File Simple.Data.Extension/StoredProcedureInterceptor.cs

 	using System.Data;
 	using System.Data.Common;
 	using System.Dynamic;
-	using Castle.DynamicProxy;	
+	using Castle.DynamicProxy;
 
 	/// <summary>
 	/// The brain of the project: Windsor IoC Interceptor.
 		/// </summary>
 		/// <param name="invocation"></param>
 		public void Intercept(IInvocation invocation)
-		{			
-			var factory = DbProviderFactories.GetFactory(ConfigurationManager.ConnectionStrings[Setup.connectionStringNames[invocation.Method.DeclaringType]].ProviderName);
-			using (connection = factory.CreateConnection())
+		{
+			try
 			{
+				if (invocation.Method.DeclaringType == typeof(IDisposable))
+				{
+					this.connection.Dispose();
+					return;
+				}
+
+				var factory = DbProviderFactories.GetFactory(ConfigurationManager.ConnectionStrings[Setup.connectionStringNames[invocation.Method.DeclaringType]].ProviderName);
+				this.connection = factory.CreateConnection();
 				connection.ConnectionString = ConfigurationManager.ConnectionStrings[Setup.connectionStringNames[invocation.Method.DeclaringType]].ConnectionString;
 				connection.Open();
 
 								}
 							}
 						}
-						else if (invocation.Method.ReturnType.IsPrimitive || invocation.Method.ReturnType == typeof(string) || (invocation.Method.ReturnType.IsGenericType && invocation.Method.ReturnType.GetGenericArguments()[0].IsPrimitive))
+						else if (invocation.Method.ReturnType.Namespace == "System")
 						{
-							invocation.ReturnValue = command.ExecuteScalar();
+							var result = command.ExecuteScalar();
+							if (result is DBNull)
+								invocation.ReturnValue = null;
+							else
+								invocation.ReturnValue = command.ExecuteScalar();
 						}
 						else if (invocation.Method.ReturnType.GetInterface("IEnumerable") != null)
 						{
 
 						command.ExecuteNonQuery();
 
-						invocation.ReturnValue = Convert.ChangeType(retval.Value, invocation.Method.ReturnType);
-						//invocation.ReturnValue = retval.Value;
+						invocation.ReturnValue = Convert.ChangeType(retval.Value, invocation.Method.ReturnType);						
 					}
 				}
 				else
 					i++;
 				}
 			}
+			finally
+			{
+				if (invocation.Method.DeclaringType.GetInterface("IDisposable") == null) this.connection.Dispose();
+			}
 		}
 	}
 }

File Simple.Data.ExtensionTest/IBehdashtDatabase.cs

+using System;
+using System.Collections.Generic;
+namespace SamaWebAPI
+{
+	public interface IBehdashtDatabase : IDisposable
+	{
+		int? DasGetStTermStatus(
+			string stno,
+			string termCode);
+
+		IEnumerable<object> DasBehdashtGetInfoStudents(string StNo);//string NationalCode, 
+	}
+}