Solved

MVC3 Mobile Best Practise to store object

Posted on 2012-03-23
6
623 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Hidden Field Value 10 63
LINQ - C# to VB convertion 12 48
Entity Framework 7 33
Adware on IIS hosted asp.net website 1 20
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 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

20 Experts available now in Live!

Get 1:1 Help Now