是否可以创建一个通用方法来代替带有 System.Data.Linq.Mapping.FunctionAttribute 的三种方法?
Is it possible to have a make a generic method which can take the place of three methods with a System.Data.Linq.Mapping.FunctionAttribute?
我的数据上下文 class 中有三种方法,它们都调用具有不同行为的稍微不同的存储过程;追加、更新、覆盖。然而,这三种方法本质上都具有相同的代码。唯一的区别是装饰方法的 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
构建的。
Is there any way I can write a generic method where the function name
to map to is passed in?
如果您的意思是通用,"reusable" 或 "boilerplate":是:
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());
}
我的数据上下文 class 中有三种方法,它们都调用具有不同行为的稍微不同的存储过程;追加、更新、覆盖。然而,这三种方法本质上都具有相同的代码。唯一的区别是装饰方法的 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
构建的。
Is there any way I can write a generic method where the function name to map to is passed in?
如果您的意思是通用,"reusable" 或 "boilerplate":是:
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());
}