tags:

views:

129

answers:

3

After a pair programming session, an interesting question came up which I think I know the answer for.

Question: Is there any other desired way in ASP.NET MVC to retain 'state' other than writing to database or a text file?

I'm going to define state here to mean that we have a collection of person objects, we create a new one, and go to another page, and expect to see the newly created person. (so no Ajax)

My thoughts are we don't want any kung-fu ViewState or other mechanisms, this framework is about going back to a stateless web.

+1  A: 

What about user session? There are plenty of valid use cases to store things in session. And what about a distributed caching system like memcached? You also seem to leave out the query string - which is an excellent state saver (?page=2). To me those seem like other desirable methods to save state across requests...?

Eric
Thanks.. and this http://stackoverflow.com/questions/490391/how-should-i-integrate-session-state-into-an-asp-net-mvc-application reinforces what you say. It seems like we're really trying to go back to stateless when possible, and to KISS :-)
Dave
A: 

"My thoughts are we don't want any kung-fu ViewState or other mechanisms, this framework is about going back to a stateless web."

The example you provided is pretty easy to do without any sort of "view state kung fu" using capabilities that are already in MVC. "User adds a person and sees that on the next screen." Let me code up a simple PersonController that does exactly what you want:

    public ActionResult Add()
    {
        return View(new Person());
    }

    [HttpPost]
    public ActionResult Add(PeronViewModel myNewPersonViewModel )
    {
        //validate, user entered everything correctly
        if( !ModelState.IsValid ) x
            return View();

         //map model to my database/entity/domain object
         var myNewPerson = new Person()
                               {
                                     FirstName = myNewPersonViewModel.FirstName,
                                     LastName = myNewPersonViewModel.LastName
                               }

        //1. maintains person state, sends the user to the next view in the chain
        //using same action
        if( MyDataLayer.Save( myNewPerson ) )
        {
             var persons = MyDataLayer.GetPersons();
             persons.Add(myNewPersion);

             return View("PersonGrid",persons); 
        }

        //2. pass along the unique id of person to a different action or controller
        //yes, another database call, but probably not a big deal 
        if( MyDataLayer.Save( myNewPerson ) )
             return RedirecToAction("PersonGrid", ...etc pass the int as route value); 

        return View("PersonSaveError", myNewPersonViewModel);
    }

Now what I'm sensing is that you want person on yet another page after PersonSaveSuccess or something else. In that case you probably want to use TempData[""] which is a single serving session and only saves "state" from one request to another or manage the traditional Session[""] yourself somehow.

What is confusing to me is your probably going to the db to get all your persons anyway. If you save a person it should be in your persons collection in the next call to your GetPersons(). If your not using Ajax, than what state are you trying to persist?

jfar
Thanks jfar - am still playing with your code.. back to it tomorrow :-) Although what I'm really getting at is covered in my comments above - we're really going towards stateless architecure when possible.
Dave
A: 

ASP.NET MVC offers a cleaner way of working with session storage using model binding. You can write a custom model binder that can supply instances from session to your action methods. Look it up.

CodeToGlory
Many thanks - http://odetocode.com/Blogs/scott/archive/2009/04/27/6-tips-for-asp-net-mvc-model-binding.aspx this is good.
Dave