Link to home
Start Free TrialLog in
Avatar of David Gerler
David GerlerFlag for United States of America

asked on

MVC3 Mobile Best Practise to store object

I am building a mobile website. In Global.asax I use www.handsetdetection.com to get the details of the device. I only want to detect it one time so I want to store the details some way.

What is the best practice / method to store the details? Session Variable, Cookies, some other way?
Avatar of Lalit Chandra
Lalit Chandra
Flag of India image

you can use Singaltal Pattern for this like somewhat as shown below

Public class InfoAboutHandset
{
    // fields to keep information;
}

public class HandsetInfo
{
    private InfoAboutHandset _InfoAboutHandset;
    private static  HandsetInfo _instance;
    private HandsetInfo() { }
    private InfoAboutHandset  GetInfoFromHandsetdetectionCom
   {
        //Fetch information from www.handsetdetection.com
        // Init Info class Object from information that you get from the site;
   }  
    public static  InfoAboutHandset GetInfo()
   {
       if(_instance== null)
      {
                  _instance = new HandsetInfo();
                 _InfoAboutHandset =   _instance.GetInfoFromHandsetdetectionCom();
      }
     return _InfoAboutHandset ;
   }

}

Now, you can use class as follow in your code
 
InfoAboutHandset info =   HandsetInfo.GetInfo();

and, you will get the information that you have fetched from the external site.

Above will make sure that you have a single instance everytime you access the class,and only for the first time it will fetch the information from the External site.

Hope this will fix up your problem.
Avatar of David Gerler

ASKER

Thank you for your reply. However, as I read my question after reading your reply I realize I was not clear.

A singleton class will not work in this instance because I need to detect the handset for each session. In other words, I need the details of the handset to change for each visitor, but I only want to detect it once for each visitor.

As I understand the singleton, it will use the same one for every session.
I still would like to know the best practice.
yes you can't use singleton pattern to store user specific data.For this you can use Session object You can write class like below

 public class InfoAboutHandset
    {
        public string HandsetName { get; set; }
        public string Operator { get; set; }
        public DateTime dataOfPUrchase { get; set; }
    }

    public class UserHandsetInfo
    {
        private HttpContext _HttpContext;
        private const string _Key = "_UserData";
        public UserHandsetInfo(HttpContext context)
        {
            _HttpContext = context;
        }
       
        public InfoAboutHandset Get()
        {
            var info = _HttpContext.Session[_Key] as InfoAboutHandset;
            if (info == null)
            {
                info = GetFromExternalWebSite();
                set(info);
            }
            return info;
        }
        private void set(InfoAboutHandset handsetInfo)
        {
            _HttpContext.Session[_Key] = handsetInfo;
        }
        private InfoAboutHandset GetFromExternalWebSite()
        {
            //Write you own Logic to retive information from external website.
            return new InfoAboutHandset() { HandsetName="Nokia", Operator="Xyz", dataOfPUrchase=DateTime.Now };
        }
    }

Open in new window


In the above GetFromExternalWebSite Method you have to write your own Logic to get the data from the exteran site.

Now  in your consumer class or Controller/Action you can directly use following code to get the value

 
UserHandsetInfo info = new UserHandsetInfo(System.Web.HttpContext.Current);
 InfoAboutHandset handset =info.Get();

Open in new window


Hope the above information might be helpful to you.
Can I actually store the object "HandsetInfo" in the session without serializing it?
ASKER CERTIFIED SOLUTION
Avatar of Lalit Chandra
Lalit Chandra
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial