问题
What is the best way of supporting optional data passed to a C# function?
I have web service function in .Net which defines 5 arguments:
[WebMethod]
public string UploadFile( string wsURL
, byte[] incomingArray
, string FileName
, string RecordTypeName
, MetaData[] metaDataArray)
The code of this function is not too long (but not trivial either) and there is only one place in the the function where I perform this test if there is any MetaData[] to be processed:
if (metaDataArray.Length > 0)
{
Update update = BuildMetaData(metaDataArray);
treq2.Items = new Operation[] { sru, cin, update, fetch};
}
else
{
treq2.Items = new Operation[] { sru, cin, fetch};
}
I needed a quick and dirty version of the above which only takes 4 arguments (i.e. no "Metadata" array as a final argument) so I cloned the whole function and removed the IF-ELSE block refering to metadata. Ugly I know.
[WebMethod]
public string UploadFileBasic( string wsURL
, byte[] incomingArray
, string FileName
, string RecordTypeName)
Now I want to do things better and I am looking for advice on the best way to support this. I do not want to burden the client program with creating an empty array as a 5th parameter...I want to have my web service functions to be smart enough to handle this optional data. Thanks.
回答1:
Change your check in the method that takes 5 arguments to (note, that you should be checking if the value is null anyway).
if (metaDataArray != null && metaDataArray.Length > 0)
{
Update update = BuildMetaData(metaDataArray);
treq2.Items = new Operation[] { sru, cin, update, fetch };
}
else
{
treq2.Items = new Operation[] { sru, cin, fetch};
}
Then, simply have your 4 argument version call the 5 argument version internally with the metaDataArray argument null.
[WebMethod]
public string UploadFileBasic( string wsURL,
byte[] incomingArray,
string FileName,
string RecordTypeName)
{
return UploadFile( wsUrl, incomingArray, fileName, RecordTypeName, null );
}
回答2:
private static readonly MetaData[] EmptyMetaData = new MetaData[0];
[WebMethod]
public string UploadFile(string wsURL
, byte[] incomingArray
, string fileName
, string recordTypeName)
{
return UploadFile(wsURL, incomingArray, fileName, recordTypeName, EmptyMetaData)
}
回答3:
How about putting all the arguments including the MetaData Array into a single class and use it as argument to the web service:
public class UploadFileAgrument
{
public string wsURL;
public byte[] incomingArray;
public string FileName;
public string RecordTypeName;
public MetaData[] metaDataArray;
}
[WebMethod]
public string UploadFile(UploadFileAgrument fileToUpload)
{
if(fileToUpload.metaDataArray!=null && metaDataArray.Length > 0)
{
}
else
{
}
}
回答4:
[WebMethod]
public string UploadFileBasic( string wsURL
, byte[] incomingArray
, string FileName
, string RecordTypeName)
{
return UploadFile(wsURL, incomingArray, FileName, RecordTypeName, new MetaData[0]);
}
Then the UploadFile method handles everything, but you're able to expose two interfaces depending on what the consumer wants.
来源:https://stackoverflow.com/questions/1155826/best-way-of-making-an-argument-optional-for-a-c-sharp-webmethod