Invoking a variable number of methods through one method
I have a WCF service that stores and processes images. Consuming applications can download images by invok开发者_如何学运维ing Bitmap Download(int fileid)
, and can also modify them by invoking other methods such as Bitmap Crop(Bitmap image, x1, x2, y1, y2)
, Bitmap Resize(Bitmap image, int height)
and Bitmap Rotate(Bitmap image, float angle)
.
The problem here is that when a client wants to call any of the three image manipulation methods above (Crop, Resize, Rotate), they must first download it and then invoke the manipulative methods successively, each time sending the image back to the WCF service and then getting a new System.Drawing.Bitmap
object as the return parameter.
The question is, how can I avoid sending the image back and forth? Is there a way I can invoke a variable number of methods through one method? If I'm still not clear enough, then this is what I want to do (I know it isn't valid code):
Bitmap bmp = Download(2, Resize(500, 200), Rotate(90.00));
I designed a similar API in the past that accepted a list of "Actions" as it's arguments. Something like:
List<Action> actions = new List<Action>();
a1 = new Action();
a1.action = Action.ActionType.Resize;
a1.params.add(500);
a1.params.add;
actions.add(a1);
a2 = new Action();
a2.action = Action.ActionTypes.Rotate;
a2.params.add(90);
actions.add(a2);
Bitmap bmp = Download(actions);
I would probably do something like:
public Bitmap Download(int image, Dictionary<string, string[]> operations)
{
// fetch image here
ForEach(KeyValuePair<string, string[]> kvp in operations)
{
switch(kvp.Key)
{
case "Crop":
...etc...etc
}
}
}
The dictionary keys could be a method names and the value could be whatever parameters you needed to pass to the method.
To be honest, this is a bit odd functionality for a WCF service. Most clients out there are smart enough to manipulate images on their own, and don't need a server to do it.
You could have the client upload the image to the server and have the WCF service return a unique ID. Then all your successive calls would send the unique ID instead of a full bitmap, telling the server to perform operations on the image that was just uploaded. When you are done manipulating the image, call Download again and the client will redownload the modified image.
Your server would need to temporarily cache these images and eventually delete them after a certain timespan, if you wanted to prevent these images from actually being stored on the server long-term.
Here's another way of going about this. The method can accept an array of objects, each of which contains DataMembers specific to the type of image operation it represents.
public void Download(int fileid, params object[] operations);
On the client side, we invoke it like so...
Bitmap edit = Download(1, new CropOperation(1, 2, 3, 4), new RotateOperation(57));
CropOperation
and RotateOperation
are both simple DataContract
classes.
On the server side, we do GetType()
on each object in object[] operations
, after which we're able to access the object's members and invoke the appropriate method.
精彩评论