ASP.NET MVC - sharing session state between controllers

I am still mostly not familiar with Inversion of Control (although I just found out about it now), so if this is the solution to my question, just let me know and I will return to that.

I have a couple of controllers that require the Session variable, of course, nothing special happens because Session works in the first place, but I wondered what the cleanest way to share related objects between two separate controllers, In my particular The scripts I have are UploadController and ProductController that work together with others to upload image files. When files are downloaded using UploadController, download data is stored in the session. After that, I need to access the session data in ProductController. If I create a get / set property for the Session variable containing my load information in both controllers, I can access this data, but at the same time I will violate all kinds of DRY, not to mention the creation, at best, an intricate design in which an object is split and changed by two completely disabled objects.

What do you suggest?

Exact context:

File Download View the messages of the UploadController.ImageWithpreview () file, which is then read in the published file and copied to a temporary directory. After saving the file, another class creates a thumbnail of the loaded image. Then, the path to the source file and the generated thumbnail is returned using JsonResult to call a javascript that updates the dynamic content in the form on the page, which can be Saved or Canceled. Regardless of whether the downloaded image is saved or skipped, I need to either move or delete both it and the generated thumbnail from the temporary directory. To facilitate this, UploadController tracks all download files and their thumbnails in a Queue object that supports the session.

Back in the view: after the form is filled with the generated thumbnail of the uploaded image, the form is sent back to ProductController where the selected file is selected (I currently store the file name in the “Hidden” field, which I implement is a terrible vulnerability), and then copied from a temporary directory to a permanent location. Ideally, I would just like to access the queue that I saved in the session so that the form does not contain the image location, as it is now. This is how I presented my decision, but I will look forward to hearing any comments or criticisms.

+6
design-patterns asp.net-mvc inversion-of-control
source share
2 answers

A couple of decisions come to mind. You can use the SessionState class, which maps to the request and receives / sets the information as such (I do this from memory, so this is unlikely to be compiled and intended to transmit a 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 can 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, of course, is DRY. However, I do not particularly like one of these solutions, because I prefer to store at least the data, if any, in the session. But if you intend to stick to all this information without downloading / distinguishing it from any other source, this is the fastest (most dirty) way I can come up with. I am quite sure that there is a much more elegant solution, but I do not have all the information about what you are trying to do and what the problem is.

Keep in mind that when storing information in a session, you will have to dehydrate / rehydrate the objects using serialization, and you may not get the performance that you think this will do.

Hope this helps.

EDIT: In response to additional information I'm not sure where you want to deploy it, but real-time image processing is a reliable fire that can be hit by a DoS attack. My suggestion for you is this: it assumes that it is public, and anyone can upload an image:

1) Allow user to upload image. This image is placed in the processing queue for background processing by an application or some service. In addition, the name of the image is in the user's personal processing queue — probably a table in the database. You can find background processing information in the web application @ Schedule a job on a hosted web server

2) Process these images and, during processing, display the “processing schedule”. You can get ajax request on the product page, which checks processed images and tries to reload them every X seconds.

3) While the image is being processed, the user can refuse processing, believing that this is the one that uploaded the image. This is available either on the product page displaying the image, or in a separate “user queue” view, which will allow them to remove the image for reasons of sorts.

So, you get some more domain objects, and these objects are queued. I am a strong supporter of the configuration agreement, so the final destination of the product image (s) should be predetermined. Something like:

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

You then do not need to know the appointment in the form. This is the same for all images.

Then the queue should know where the temporary image was uploaded and what the product identifier is. The queue worker pushes items out of the queue, processes them, and saves them accordingly.

I know this sounds a bit “structured” than what you originally planned, but I think it is a little cleaner.

+3
source share

Is there complete equivalence between UploadController and ProductController?

When files are downloaded using UploadController, download data is stored in the session. After that, I need to access the session data in ProductController.

Since I read that UploadControl needs reading and writing to load data, ProductController needs only reading.

If this is true, then you can make it understandable by using an immuatable wrapper around the download information and add the UploadController to the session.

The session itself is determined by a publicly accessible bulletin board, separates the explicit relationship due to the fact that it allows someone to receive and deliver. You can let ProductController learn about UploadController and therefore remove the need to transfer download information through the session. My instinct is that the download information is interesting to the public, so using a session is reasonable.

I do not see any DRY violation here, we are clearly trying to share responsibilities.

+1
source share

All Articles