I have three methods in my datacontext class which all call slightly different stored procedures having different behaviours; APPEND, UPDATE, OVERWRITE. However all three methods have in essence the same code. The only difference is the "Name" property of the System.Data.Linq.Mapping.FunctionAttribute
which decorates the methods.
[Function(Name = "import.usp_MyData_ProcessImportAppend", IsComposable = false)]
or
[Function(Name = "import.usp_MyData_ProcessImportUpdate", IsComposable = false)]
or
[Function(Name = "import.usp_MyData_ProcessImportOverwrite", IsComposable = false)]
In essence they all look similar to this
/// <summary>
/// Processes the import.
/// </summary>
/// <param name="loadId">The load id.</param>
/// <exception cref="System.ArgumentNullException">loadId</exception>
[Function(Name = "import.usp_MyData_ProcessImportAppend", IsComposable = false)]
public Int32 ProcessGradingImport(string loadId)
{
// Validate parameter
if (String.IsNullOrEmpty(loadId)) throw new ArgumentNullException("loadId");
// Initialise the result and the procedure parametes
Int32 result = 0;
object[] parameters = { loadId };
// Call the procedure, and return the result
IExecuteResult executionResult = ExecuteMethodCall(this, (MethodInfo)(MethodBase.GetCurrentMethod()), parameters);
if (executionResult != null) result = (int)executionResult.ReturnValue;
return result;
}
Is there any way I can write a generic method where the function name to map to is passed in? Thank you.
UPDATE So based upon the excellent solutions by Ron and Alex, I now have
/// <summary>
/// Process imports.
/// </summary>
/// <param name="methodInfo">The method information.</param>
/// <param name="loadId">The load identifier.</param>
/// <returns></returns>
/// <exception cref="System.ArgumentNullException">loadId</exception>
private Int32 ProcessImport(MethodInfo methodInfo, string loadId)
{
// Validate parameter
if (String.IsNullOrEmpty(loadId)) throw new ArgumentNullException("loadId");
// Initialise the result and the procedure parametes
Int32 result = 0;
object[] parameters = { loadId };
// Call the procedure, and return the result
IExecuteResult executionResult = ExecuteMethodCall(this, methodInfo, parameters);
if (executionResult != null) result = (int)executionResult.ReturnValue;
return result;
}
called in decorated function like...
[Function(Name = "common.usp_MyData_ProcessImportAppend", IsComposable = false)]
public Int32 ProcessImportAddNewUpdateCurrentOnly(string loadId)
{
return ProcessImport((MethodInfo)(MethodBase.GetCurrentMethod()), loadId);
}