1
\$\begingroup\$

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;
}
\$\endgroup\$
2
\$\begingroup\$

This is something I always get confused with as well!!

I would probably think it's fine where it is but if you were considering moving it I might suggest using an interface and injecting that into the controller. That way it doesn't matter where the latest friend information is persisted and the controller remains separate from this concern.

Although this might be overkill for your situation and example of this in use might be:

public interface IFriendProvider
{
   Friend GetLatest();
   void SaveLatest(Friend friend);
}

Your controller action might then become:

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

        // where _friendProvider has been supplied through the controller constructor
        // using DI  (See Ninject or Unity for example)
        _friendProvider.SaveLatest(new Friend()
        {
           FirstName = viewModel.FirstName;
           LastName = viewModel.LastName;
           Email = viewModel.UserEmail;            
        });         
}

Your implementation of the friendProvider would be something like:

public class SessionFriendProvider : IFriendProvider
{
   private readonly HttpContext __httpContext;

   // or instead of Context maybe the session object itself?
   public SessionFriendProvider(HttpContextBase context)
   {
      __httpContext = context;
   }

   public void SaveLatest(Friend friend)
   {
       _httpContext.Session["latest-friend"] = friend;
   }

   public Friend GetLatest() 
   {
      return _httpContext.Session["latest-friend"] ?? new Friend();
   }
}
\$\endgroup\$
0
\$\begingroup\$

Yes

Yes it belong s to the controller. If you are interacting with HTTP... stuff then you should do it in your MVC tier in your N-tier application stack. If you would put this thing in the model then it would be in your business logic and then you would have a hard dependency on the HttpContext which is bad becouse what would happen if you would create a WCF service endpoint for your business logic?

MVC tier is just an public service interface which is communication with the users on HTTP channel nothing more. If you don't have an N-tier design in your app then you should get start to clean up your solution and put things were they belong.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.