I have an existing ASP.NET MVC 2 client application that consumes a RESTful WCF service application for data persistence. A new requirement has come up to support an image attached/associated to one of my existing domain objects (Product).
Currently, the client application calls the service to obtain a list of Products (in the form of a list of lightweight ProductInfo objects) and displays the list to the user. When the user clicks an item in the list, the client calls the service to get the specific Product object which supports editing by the user. When saved, the client posts the updated Product to the service for persistence.
The new requirement calls for me to display the associated image in the list as well as allow the user to set/replace the image when editing the Product. The current image is also displayed in the Product editor. Only one image will be associated with each Product and the image will be required.
-
Is Stream the best way to pass the image data between client and server or should I go with Byte[]?
-
For the list, would it be wise to add a new Image property to ProductInfo of type Stream (or Byte[]) or require a separate call to the service to download the image?
-
Likewise for editing, do I just treat the image data as any other property and pass it back and forth across the wire using an Image property?
While I appreciate Darin’s response and originally headed down that path, I ended up going with the same approach outlined in Pro ASP.NET MVC 2 Framework.
Chapter 6 describes how an image can be uploaded as part of the edit page for a Product object then displayed using a controller action in another page. The only difference in my application is that persistence is handled in a different tier via a RESTful web service interface. However, based on the approach shown in the book, I decided to go with a single DTO that contains properties for the binary image data and string MIME type information.
If I had a different UI or a heavier object to pass around, I would definitely reconsider this approach but for me, this time around, this works great.