Johny Bravo
Johny Bravo

Reputation: 424

Session usage in MVC

I have a helper class for session,

namespace ShopCart.BAL
{
    public static class SessionHelper
    {
        public static ShopUser myShopUser
        {
            get
            {
                return HttpContext.Current.Session["myShopUser"] as ShopUser;
            }

            set
            {
                HttpContext.Current.Session["myShopUser"] = value;
            }
        }


    }

}

Further ShopUser class looks like

public class ShopUser 
{

    public string LoginID { get; set; }
    public string SessionID { get; set; }

    public long UserId { get; set; }

    public long ShoppingCartId { get; set; }

    public bool? Status { get; set; }

}

Now I want to know how to set session for particular property? SessionHelper.Status = false; //it creates a new property in SessionHelper How to use above classes to create Sessions?

@mohsen your answer is correct. Further,

public ActionResult Index() {
 ShopLogin objShopLogin = new ShopLogin (); 
objShopLogin .checkUser();
 }

public class ShopLogin {
   public string checkUser() 
  { 
     SessionHelper.myShopUser.Status = false;
  }
}

So as you can see, I am creating class object here, as ShopLogin objShopLogin = new ShopLogin (); Can you tell me, can I use dependency injection here to avoid creating class objects like these?

Upvotes: 1

Views: 454

Answers (1)

mohsen
mohsen

Reputation: 1806

Use

SessionHelper.myShopUser.Status = false

Edit

Yes . Just add static modifier to CheckUser method

public ActionResult Index() {
 ShopLogin.checkUser();
 }

public class ShopLogin {
   public static string checkUser() 
   { 
       SessionHelper.myShopUser.Status = false;
   }
}

Upvotes: 2

Related Questions