Solved

MVC3 Mobile Best Practise to store object

Posted on 2012-03-23
6
624 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
daypilot scheduler 3 27
IIS Authorization for Web Service 2 46
Gridview alignment 1 19
Expression Evaluater 3 26
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
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 tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

778 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