views:

391

answers:

2

I am still mostly unfamiliar with Inversion of Control (although I am learning about it now) so if that is the solution to my question, just let me know and I'll get back to learning about it.

I have a pair of controllers which need to a Session variable, naturally nothing too special has happen because of how Session works in the first place, but this got me wondering what the cleanest way to share related objects between two separate controllers is. In my specific scenario I have an UploadController and a ProductController which work in conjunction with one another to upload image files. As files are uploaded by the UploadController, data about the upload is stored in the Session. After this happens I need to access that Session data in the ProductController. If I create a get/set property for the Session variable containing my upload information in both controllers I'll be able to access that data, but at the same time I'll be violating all sorts of DRY, not to mention creating a, at best, confusing design where an object is shared and modified by two completely disconnected objects.

What do you suggest?

Exact Context:

A file upload View posts a file to UploadController.ImageWithpreview(), which then reads in the posted file and copies it to a temporary directory. After saving the file, another class produces a thumbnail of the uploaded image. The path to both the original file and the generated thumbnail are then returned with a JsonResult to a javascript callback which updates some dynamic content in a form on the page which can be "Saved" or "Cancelled". Whether the uploaded image is saved or it is skipped, I need to either move or delete both it and the generated thumbnail from the temporary directory. To facilitate this, UploadController keeps track of all of the upload files and their thumbnails in a Session-maintained Queue object.

Back in the View: after the form is populated with a generated thumbnail of the image that was uploaded, the form posts back to the ProductsController where the selected file is identified (currently I store the filename in a Hidden field, which I realize is a horrible vulnerability), and then copied out of the temp directory to a permanent location. Ideally, I would like to simply access the Queue I have stored in the Session so that the form does not need to contain the image location as it does now. This is how I have envisioned my solution, but I'll eagerly listen to any comments or criticisms.

+2  A: 

A couple of solutions come to mind. You could use a "SessionState" class that maps into the request and gets/sets the info as such (I'm doing this from memory so this is unlikely to compile and is meant to convey the point):

internal class SessionState
{
  string ImageName
  {
    get { return HttpContext.Current.Session["ImageName"]; }
    set { HttpContext.Current.Session["ImageName"] = value; }
  }
}

And then from the controller, do something like:

  var sessionState = new SessionState();
  sessionState.ImageName = "xyz";
  /* Or */
  var imageName = sessionState.ImageName;

Alternatively, you could create a controller extension method:

public static class SessionControllerExtensions
{
  public static string GetImageName(this IController controller)
  {
    return HttpContext.Current.Session["ImageName"];
  }

  public static string SetImageName(this IController controller, string imageName)
  {
    HttpContext.Current.Session["ImageName"] = imageName;
  }
}

Then from the controller:

  this.SetImageName("xyz");
  /* or */
  var imageName = this.GetImageName();

This is certainly DRY. That said, I don't particularly like either of these solutions as I prefer to store as little data, if any, in session. But if you're intent is to hold onto all of this information without having to load/discern it from some other source, this is the quickest (dirtiest) way I can think of to do it. I'm quite certain there's a much more elegant solution, but I don't have all of the information about what it is you're trying to do and what the problem domain is.

Keep in mind that when storing information in the session, you will have to dehydrate/rehydrate the objects via serialization and you may not be getting the performance you think you are from doing it this way.

Hope this helps.

EDIT: In response to additional information Not sure on where you're looking to deploy this, but processing images "real-time" is a sure fire way to be hit with a DoS attack. My suggestion to you is as follows -- this is assuming that this is public facing and anyone can upload an image:

1) Allow the user to upload an image. This image goes into the processing queue for background processing by the application or some service. Additionally, the name of the image goes into the user's personal processing queue -- likely a table in the database. Information about background processing in a web app can be found @ http://stackoverflow.com/questions/1134615/schedule-a-job-in-hosted-web-server/1135052#1135052

2) Process these images and, while processing, display a "processing graphic". You can have an ajax request on the product page that checks for images being processed and trys to reload them every X seconds.

3) While an image is being "processed", the user can opt out of processing assuming they're the one that uploaded the image. This is available either on the product page(s) that display the image or on a separate "user queue" view that will allow them to remove the image from consideration.

So, you end up with some more domain objects and those objects are managed by the queue. I'm a strong advocate of convention over configuration so the final destination of the product image(s) should be predefined. Something like:

images/products/{id}.jpg or, if a collection, images/products/{id}/{sequence}.jpg.

You then don't need to know the destination in the form. It's the same for all images.

The queue then needs to know where the temp image was uploaded and what the product id was. The queue worker pops items from the queue, processes them, and stores them accordingly.

I know this sounds a little more "structured" than what you originally intended, but I think it's a little cleaner.

andymeadows
Thank you this input. I have added some more information to my original question if that is of any further use.
Nathan Taylor
The image processing is real time, but exists inside an admin area to the website, so the overall usage of it is pretty minimal. At most, only a few images are likely to be processed per hour across the entire application.Another approach I also considered was simply having the application's Session_End event delete the entire temporary directory for the current session, but I'm not sure how ideal that is.
Nathan Taylor
If your user has uploaded a lot of images that are processing, I don't think you could guarantee that it would be done when their session expires. I would delete it once "processing" has completed. The worker thread(s) should stay alive as long as the queue has items to process so it's safe to assume that. As long as your app pool isn't "slept", your system should continue to process ok. Pretty sure it's covered in the article I linked.
andymeadows
Indeed. Not only is it a low occurrence operation but it's only performed one file at a time. I'll take a look at what you linked all the same.
Nathan Taylor
You're actually right on the money there. It already does exactly that, but the way the image upload action is configured it returns a generic result that contains a pair of file names that are generated from a structure rule. I'm just trying to keep a buffer of all uploads that go through the system whether they're "Products" or "Parts".
Nathan Taylor
Then one "final" suggestion would be to move the queue out of the session and make it application specific. You can store it in-memory or in the database. Make each queue object store image type (products or parts), owner (user id), and temporary location. When an image has been processed, remove it from the temporary location. Have a single temp location for the entire system. This should solve any additional issues if I'm still following you.
andymeadows
+1  A: 

Is there complete equivalence between the UploadController and ProductController?

As files are uploaded by the UploadController, data about the upload is stored in the Session. After this happens I need to access that Session data in the ProductController.

As I read that the UploadControl needs read and write access to Upload data, the ProductController needs only read.

If that's true then you can make it clear by using an immuatable wrapper around the upload information and have the UploadController put that into the session.

The Session itself is by definiton a public shared noticeboard, decouples explicit relationships at the cost of allowing anyone to get and put. You could allow the ProductController to know about the UploadController and hence remove the need for passing the upload information via the session. My instinct is that the upload info is interesting to the public, so using Session is reasonable.

I don't see any DRY violation here, we are explicitly trying to separate responsibilities.

djna
I'm not certain if this qualifies more as a DRY issue or just one of poorly defined component communication. Session is obviously designed to bridge the exact gap I am trying to fill, it just doesn't feel particularly clean in this scenario.
Nathan Taylor