c# - 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? -


i have 3 methods in datacontext class call different stored procedures having different behaviours; append, update, overwrite. 3 methods have in essence same code. difference "name" property of system.data.linq.mapping.functionattribute decorates 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 similar this

/// <summary> /// processes 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 result , procedure parametes     int32 result = 0;     object[] parameters = { loadid };      // call procedure, , return result     iexecuteresult executionresult = executemethodcall(this, (methodinfo)(methodbase.getcurrentmethod()), parameters);     if (executionresult != null) result = (int)executionresult.returnvalue;     return result; } 

is there way can write generic method function name map passed in? thank you.

update based upon excellent solutions ron , alex, 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 result , procedure parametes     int32 result = 0;     object[] parameters = { loadid };      // call procedure, , return 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); } 

i code reuse:

[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 result , procedure parametes     int32 result = 0;      // call procedure, , return result     iexecuteresult executionresult = executemethodcall(this, methodinfo, parameters);     if (executionresult != null) result = (int)executionresult.returnvalue;     return result; } 

unfortunately of c# 5.0, there isn't way asking. attribute doesn't allow multiple copies per function, can break out reused code , factor down little bit. reduced further remove parameters object since constructed loadid anyway.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -