MVC : Does Code to save data in cache or session belongs in controller

asp.net-mvcasp.net-mvc-3asp.net-mvc-4mvc

I'm a bit confused if saving the information to session code below, belongs in the controller action as shown below or should it be part of my Model?

I would add that I have other controller methods that will read this session value later.

  public ActionResult AddFriend(FriendsContext viewModel)
  {
        if (!ModelState.IsValid)
        {                
            return View(viewModel);
        }

        // Start - Confused if the code block below belongs in Controller?

        Friend friend = new Friend();
        friend.FirstName = viewModel.FirstName;
        friend.LastName = viewModel.LastName;
        friend.Email = viewModel.UserEmail;            

        httpContext.Session["latest-friend"] = friend;

        // End Confusion

        return RedirectToAction("Home");
    }

I thought about adding a static utility class in my Model which does something like below, but it just seems stupid to add 2 lines of code in another file.

public static void SaveLatestFriend(Friend friend, HttpContextBase httpContext)
{
    httpContext.Session["latest-friend"] = friend;
}


public static Friend GetLatestFriend(HttpContextBase httpContext)
{
    return httpContext.Session["latest-friend"] as Friend;
}

Best Answer

If it was me, I would create a new class called SessionManager (or something), where you can pass through your data to save the latest friend and retrieve it. That way you've got one spot where all of your session handling is done.