c# - 是否可以使用 System.Data.Linq.Mapping.FunctionAttribute 制作一个通用方法来代替三种方法?

标签 c# linq entity-framework datacontext function-attributes

我的数据上下文类中有三种方法,它们都调用具有不同行为的稍微不同的存储过程;追加、更新、覆盖。然而,这三种方法本质上都具有相同的代码。唯一的区别是装饰方法的 System.Data.Linq.Mapping.FunctionAttribute 的“名称”属性。

[函数(名称 = "import.usp_MyData_ProcessImportAppend", IsComposable = false)]

[函数(名称 = "import.usp_MyData_ProcessImportUpdate", IsComposable = false)]

[函数(名称 = "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 构建的。

关于c# - 是否可以使用 System.Data.Linq.Mapping.FunctionAttribute 制作一个通用方法来代替三种方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29948465/

相关文章:

c# - 我应该如何封装这个多维枚举?

c# - 远程服务器 (403) 使用 WebClient 时出现禁止错误

linq - 表达式<T> 的作用是什么?

c# - 如何返回一个查询多个表的实体

c# - Azure 不会在发布 EFCore 2 应用程序时创建数据库架构

c# - Newtonsoft JSON 在运行时获取特定的嵌套属性

c# - Linq to Excel 不检索第一个填充行

c# - 如何从 List<IEnumerable<ItemClass>> 到 IEnumerable<ItemClass> 进行选择?

c# - 与数组相比,使用 IEnumerable 有哪些优势?

entity-framework - 更改所有实体的标识列名称