Rex T
Rex T

Reputation: 77

Passing Complex Object correctly without using TempData / ViewBag / ViewData

For a website, consider this scenario:

  1. Users input data, including a ID, and submit
  2. A related controller will handle the request. At this point, one complex object will be created (by calling web services, with the ID) for a series of operations. e.g validation, etc
  3. Return user a specific View
  4. User input another set of data and submit
  5. Another controller will handle this request. It requires the complex object used in step 2.

and so on...

Currently, in step 5, I will use the ID to call the web service again for getting the required complex object.

Is there any correct/efficient way, without using TempData/ViewBag/ViewData, so that I can reuse the complex object, once it is created, in step 2?

Edit: Session is not allowed as well.

Upvotes: 0

Views: 1353

Answers (2)

Kien Chu
Kien Chu

Reputation: 4895

Normally, for web application, there're multiple options to store complex object depending on your need. I don't think there is a BEST way of doing, only the most suitable way and every solutions will come with PROS and CONS


SERVER SIDE

  • Session (I know you said cannot use session, but I just want to include it anyway): first option comes to mind, suitable for most web application. Since the modern web development are more on STATELESS, a lot of people want to avoid using Session at all cost. However, there're some specific infrastructure configuration to support session in STATELESS application such as distributed session or sticky session or you can save the session in a dedicated server or database.

    • PROS: easy to use, support web application naturally
    • CONS: need to configure a lot of things to work with STATELESS application
  • ANOTHER Dedicated server (Before anyone ask, I put it in the SERVER SIDE section even though it's another SERVER, but to me, whatever in our control is SERVER SIDE): a few options for you to choose here, first option could be to set up a cache server (Redis?) and retrieve/save using key (similar to session), or you can simply write an application to retrieve/save using your own logic.

    • PROS: reusability, extendability, works with all applications not just web, have its own scope
    • CONS: difficult to setup
  • Database: not a obvious choice, but database do support this kind of requirement

    • PROS: reusability, extendability, works with all applications not just web
    • CONS: performance issue
  • Other in-memory options (TempData, ViewBag, etc):

    • PROS: easy to use, well-supported with ASP.NET MVC
    • CONS: somtimes it's hard to pass around multiple views

CLIENT SIDE

  • There are so many options here to choose like use hidden fields, cookie, localStorage, sessionStorage, etc or even a simple query string will work
  • PROS: speed (since you don't need client-server transportation)
  • CONS: security (you cannot trust anything from client-side), not doing well with too complex object (heavy object), security (sensitive data), etc

SUGGESTED SOLUTION

I hope I understand your issue correctly but in my opinion, you should not store complex object, simply store the ID of the complex object in place of your choice, and make a query every time you need the object. So that your object is always up-to-date and you don't waste resource to store the complex object.

Hope it helps you.

Upvotes: 2

iuliu.net
iuliu.net

Reputation: 7145

If you want to project an object to a certain view, then forget about any storage (TempData/ViewBag/ViewData/Session) and then post the same object to another controller, the best way you can re-construct your object is to store the object's properties as hidden <input> controls.

Upvotes: 0

Related Questions