Reputation: 153
I have an AttachmentService.cs class that is currently used to upload images to a database and/or a cloud storage container. When uploading to the cloud I have some retry logic in place that I would like to reuse when being called by two separate methods. I would like to pass in each function as a parameter and then call them from within the method they have been passed into. Here are the two signatures I have currently:
C#
//Example: I want to be able to pass the first function and also the second
//I'm sure it can done with generics but can't seem to get it currently
private AttachmentUploadResult UploadAttachmentToGLS(Func<string, Guid, byte?[], string, AttachmentUploadResult> uploadFunction)
private AttachmentUploadResult UploadAttachmentToGLS(Func<AttachmentEntity, AttachmentUploadResult> uploadFunction)
I would like the above code to only have one signature that could take either
Func<string, Guid, byte?[], string, AttachmentUploadResult> uploadFunction
or Func<AttachmentEntity, AttachmentUploadResult> uploadFunction
so that my method could look like something like this:
private AttachmentUploadResult UploadAttachmentToGLS(Func<parameters, AttachmentUploadResult>uploadFunction)
{
//Some retry logic
uploadFunction(parameters);
//Some more retry logic
}
Is there a way in which I can acheive the above? I have read into Func and do not believe this is the correct delegate to use.
Upvotes: 1
Views: 128
Reputation: 63732
The signature of your function can be just Func<AttachmentUploadResult>
- you don't really care about the parameters in the UploadAttachment
method, so you should close over them. The call might then look something like this:
Upload(() => SomeUploadMethod(theActual, parameters, toTheMethod));
It also allows you to hide the details of the implementation from the UploadAttachment
method - all you care about is "this is the function to call when I want to do the upload".
Upvotes: 2