Advertisement
Guest User

Untitled

a guest
Feb 17th, 2014
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.56 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Runtime.Remoting.Proxies;
  6. using System.Data.Common;
  7. using ServiceStack.MiniProfiler.Data;
  8. using ServiceStack.MiniProfiler;
  9. using System.Runtime.Remoting.Messaging;
  10. using System.Data;
  11.  
  12. namespace SouthWind.Core.Profiling
  13. {
  14.     public class ProfiledPostgresqlDriver : NHibernate.Driver.NpgsqlDriver
  15.     {
  16.         public override IDbCommand CreateCommand()
  17.         {
  18.             IDbCommand command = base.CreateCommand();
  19.            
  20.             if (Profiler.Current != null)
  21.             {
  22.                 command = DbCommandProxy.CreateProxy(command);                
  23.             }
  24.  
  25.             return command;
  26.         }
  27.     }
  28.  
  29.  
  30.     public class ProfiledMySQLDriver : NHibernate.Driver.MySqlDataDriver
  31.     {
  32.         public override IDbCommand CreateCommand()
  33.         {
  34.             IDbCommand command = base.CreateCommand();
  35.  
  36.             if (Profiler.Current != null)
  37.                 command = DbCommandProxy.CreateProxy(command);
  38.  
  39.             return command;
  40.         }
  41.     }
  42.  
  43.     public class DbCommandProxy : RealProxy
  44.     {
  45.         private DbCommand instance;
  46.         private IDbProfiler profiler;
  47.  
  48.         private DbCommandProxy(DbCommand instance)
  49.             : base(typeof(DbCommand))
  50.         {
  51.             this.instance = instance;
  52.             this.profiler = Profiler.Current as IDbProfiler;
  53.         }
  54.  
  55.         public override IMessage Invoke(IMessage msg)
  56.         {
  57.             IMethodCallMessage methodMessage = new MethodCallMessageWrapper((IMethodCallMessage)msg);            
  58.             var executeType = GetExecuteType(methodMessage);
  59.            
  60.             if (executeType != ExecuteType.None)            
  61.                 profiler.ExecuteStart(instance, executeType);            
  62.  
  63.             object returnValue = methodMessage.MethodBase.Invoke(instance, methodMessage.Args);
  64.  
  65.             if (executeType == ExecuteType.Reader)                            
  66.                 returnValue = new ProfiledDbDataReader((DbDataReader)returnValue, instance.Connection, profiler);            
  67.  
  68.             IMessage returnMessage = new ReturnMessage(returnValue, methodMessage.Args, methodMessage.ArgCount, methodMessage.LogicalCallContext, methodMessage);
  69.  
  70.             if (executeType == ExecuteType.Reader)
  71.                 profiler.ExecuteFinish(instance, executeType, (DbDataReader)returnValue);
  72.             else if (executeType != ExecuteType.None)
  73.                 profiler.ExecuteFinish(instance, executeType, null);
  74.  
  75.             return returnMessage;
  76.         }
  77.  
  78.         private static ExecuteType GetExecuteType(IMethodCallMessage message)
  79.         {
  80.             // for some reason, on Mono, the method name is System.Data.IDbCommand.ExecuteReader
  81.             string msgName = message.MethodName;
  82.             if (msgName.Contains('.'))
  83.                 msgName = msgName.Substring(msgName.LastIndexOf('.') + 1);
  84.             switch (msgName)
  85.             {
  86.                 case "ExecuteNonQuery":
  87.                     return ExecuteType.NonQuery;
  88.                 case "ExecuteReader":
  89.                     return ExecuteType.Reader;
  90.                 case "ExecuteScalar":
  91.                     return ExecuteType.Scalar;
  92.                 default:
  93.                     return ExecuteType.None;
  94.             }
  95.         }
  96.  
  97.         public static IDbCommand CreateProxy(IDbCommand instance)
  98.         {
  99.             var proxy = new DbCommandProxy(instance as DbCommand);
  100.  
  101.             return proxy.GetTransparentProxy() as IDbCommand;
  102.         }
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement