ASP.NET MVC - State and Architecture

After the pair programming session, an interesting question arose, which, it seems to me, I know for an answer.

Question: Is there any other desirable way in ASP.NET MVC to save β€œstate” other than writing to a database or text file?

I am going to determine the state here in order to keep in mind that we have a collection of human objects, we create a new one and go to another page and expect to see the newly created person. (so there is no Ajax)

My thoughts are that we do not want any Kung Fu ViewState or other mechanisms, this structure is intended to return to a stagnant website.

+3
source share
3 answers

My thoughts are that we do not want any Kung Fu ViewState or other mechanisms, this structure is intended to return to a stagnant website.

- " -", , MVC. " ". PersonController, , :

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

[HttpPost]
public ActionResult Add(PersonViewModel myNewPersonViewModel)
{
    //validate, user entered everything correctly
    if(!ModelState.IsValid)
        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);
}

, , PersonSaveSuccess - . , , TempData[""], , Session[""].

, , , , db, . , GetPersons(). Ajax, ?

0

? . , memcached? , , , (? Page = 2). ...?

+1

ASP.NET MVC offers a cleaner way to work with session storage using model binding. You can write a custom mediator that can provide session instances to your action methods. Take a look.

0
source

Source: https://habr.com/ru/post/1734824/


All Articles