?
Solved

MVC3 Mobile Best Practise to store object

Posted on 2012-03-23
6
Medium Priority
?
630 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

752 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