我的datacontext类中有三个方法,它们都调用具有不同行为的略有不同的存储过程;追加、更新、覆盖。但是,所有三种方法本质上都具有相同的代码。唯一的区别是装饰方法的System.Data.Linq.Mapping.FunctionAttribute
的"Name"属性。
[Function(Name = "import.usp_MyData_ProcessImportAppend", IsComposable = false)]
或
[Function(Name = "import.usp_MyData_ProcessImportUpdate", IsComposable = false)]
或
[Function(Name = "import.usp_MyData_ProcessImportOverwrite", IsComposable = false)]
从本质上讲,它们看起来都与此相似
/// <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;
}
有什么方法可以编写一个通用方法,其中传入要映射到的函数名称?谢谢。
更新因此,基于Ron和Alex的出色解决方案,我现在有
/// <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;
}
在装饰函数中调用,例如...
[Function(Name = "common.usp_MyData_ProcessImportAppend", IsComposable = false)]
public Int32 ProcessImportAddNewUpdateCurrentOnly(string loadId)
{
return ProcessImport((MethodInfo)(MethodBase.GetCurrentMethod()), loadId);
}
我会为代码重用做这样的事情:
[Function(Name = "import.usp_MyData_ProcessImportAppend", IsComposable = false)]
public Int32 ProcessGradingImport(string loadId)
{
ProcessLoadId(loadId, (MethodInfo)(MethodBase.GetCurrentMethod(), new object[] { loadId });
}
[Function(Name = "import.usp_MyData_ProcessImportUpdate", IsComposable = false)]
public Int32 ProcessGradingImportUpdate(string loadId)
{
ProcessLoadId(loadId, (MethodInfo)(MethodBase.GetCurrentMethod(), new object[] { loadId });
}
[Function(Name = "import.usp_MyData_ProcessImportOverwrite", IsComposable = false)]
public Int32 ProcessGradingImportOverwrite(string loadId)
{
ProcessLoadId(loadId, (MethodInfo)(MethodBase.GetCurrentMethod(), new object[] { loadId });
}
public Int32 ProcessLoadId(string loadId, MethodInfo method, object[] parameters)
{
// Validate parameter
if (String.IsNullOrEmpty(loadId)) throw new ArgumentNullException("loadId");
// Initialise the result and the procedure parametes
Int32 result = 0;
// Call the procedure, and return the result
IExecuteResult executionResult = ExecuteMethodCall(this, methodInfo, parameters);
if (executionResult != null) result = (int)executionResult.ReturnValue;
return result;
}
不幸的是,从 C# 5.0 开始,没有办法按照您的要求进行操作。该属性不允许每个函数有多个副本,因此您所能做的就是分解重用的代码并将其分解一点。删除参数对象可能会进一步减少,因为它只是从loadId
构造的。
有什么方法可以编写一个通用方法,其中函数名称 映射到是传入的?
如果您的意思是通用、"可重用"或"样板":是的:
private Int32 MyBoilerplateImpl(string IdArgument, string argumentName, MethodInfo method)
{
// Validate parameter
if (String.IsNullOrEmpty(IdArgument)) throw new ArgumentNullException(argumentName);
// Initialise the result and the procedure parameters
Int32 result = 0;
object[] parameters = { IdArgument};
// Call the procedure, and return the result
IExecuteResult executionResult = ExecuteMethodCall(this, method, parameters);
if (executionResult != null)
result = (int)executionResult.ReturnValue;
return result;
}
并像这样调用它:
[Function(Name = "import.usp_MyData_ProcessImportAppend", IsComposable = false)]
public Int32 ProcessGradingImport(string loadId)
{
MyBoilerplateImpl(loadId, "loadId", (MethodInfo)(MethodBase.GetCurrentMethod());
}