Solved

MVC3 Mobile Best Practise to store object

Posted on 2012-03-23
6
622 Views
Last Modified: 2012-04-21
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?
0
Comment
Question by:dgerler
  • 3
  • 3
6 Comments
 
LVL 7

Expert Comment

by:Lalit Chandra
ID: 37758244
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.
0
 

Author Comment

by:dgerler
ID: 37787360
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.
0
 

Author Comment

by:dgerler
ID: 37849753
I still would like to know the best practice.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 7

Expert Comment

by:Lalit Chandra
ID: 37849961
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.
0
 

Author Comment

by:dgerler
ID: 37850786
Can I actually store the object "HandsetInfo" in the session without serializing it?
0
 
LVL 7

Accepted Solution

by:
Lalit Chandra earned 500 total points
ID: 37851546
Yes, the InfoAboutHandset object is been stored into the session.
The storing complexity will be taken care by the Framework itself, So, no need to worry about it's Serialization.
It is the running code,so you can run at your end.

Hope this will fix up you issues.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now