/// <summary> /// Builds a new instance of the class <see cref="DbTypeMapping"/>. /// </summary> /// <param name="clrType">CLI type.</param> /// <param name="dbType">Database type.</param> /// <param name="preference">Preference (higher is better).</param> protected DbTypeMapping(Type clrType, DbCompactType dbType, int preference) { if (clrType == null) { throw new ArgumentNullException("clrType"); } this.clrType = clrType; this.dbType = dbType; this.preference = preference; }
/// <summary> /// Method called instead of the body of the modified method. We put our own implementation /// here: calling the stored procedure that has the same name and signature as the /// called method. /// </summary> /// <param name="aspectArgs">Event arguments specifying which method is being /// executed, on which object instance and with which parameters. /// <remarks> public override void OnInvoke(MethodInterceptionArgs args) { // Get a connection. DbConnection connection = dbProviderFactory.CreateConnection(); connection.ConnectionString = this.connectionString; // Get a command and set it up. DbCommand command = connection.CreateCommand(); command.CommandText = args.Method.Name; command.CommandType = CommandType.StoredProcedure; // Add parameters. ParameterInfo[] methodParameters = args.Method.GetParameters(); for (int i = 0; i < methodParameters.Length; i++) { ParameterInfo methodParameter = methodParameters[i]; // If the parameter is ByRef, get the element type. Type parameterType = methodParameter.ParameterType; if (parameterType.IsByRef) { parameterType = parameterType.GetElementType(); } // Create and set up the parameter. DbParameter commandParameter = dbProviderFactory.CreateParameter(); commandParameter.ParameterName = methodParameter.Name; if (methodParameter.ParameterType.IsByRef) { if (methodParameter.IsIn && methodParameter.IsOut) { commandParameter.Direction = ParameterDirection.InputOutput; } else { commandParameter.Direction = ParameterDirection.Output; } } else { commandParameter.Direction = ParameterDirection.Input; } DbCompactType dbType = DbTypeMapping.GetPreferredMapping(parameterType).DbCompactType; commandParameter.DbType = dbType.DbType; commandParameter.Size = dbType.Size == DbTypeMapping.FreeSize ? 1000 : dbType.Size; // If the parameter is input, set its value. if (methodParameter.IsIn || methodParameter.Attributes == ParameterAttributes.None) { commandParameter.Value = args.Arguments[i]; } // Finally add the parameter to the command. command.Parameters.Add(commandParameter); } connection.Open(); try { // Execute the command. command.ExecuteNonQuery(); // Write back the output parameters. for (int i = 0; i < methodParameters.Length; i++) { ParameterInfo methodParameter = methodParameters[i]; if (methodParameter.ParameterType.IsByRef) { args.Arguments[i] = Convert.ChangeType(command.Parameters[i].Value, methodParameter.ParameterType.GetElementType()); } } } finally { connection.Close(); } }