locked
What is the best practice for accessing global Silverlight application data RRS feed

  • Question

  • I've got a bunch of Silverlight 5 applications hosted in a website that set some application specific data. One of these datum is a CurrentUser object.

    On startup of any of the apps, they talk to a web service to pull in the current user information. However, this needs to be accessed from all sorts of places in the SL application.

    I thought it made sense to put it on the Application object itself, but I wasn't sure if this was a good practice.

    I put a simple property containing the user in an interface and implemented it on the main application class.

    Then, the rest of the application code accesses it via a static class:

    Public Module ApplicationUtils
    
        Public Property CurrentUser() As WebUser
            Get
                Dim app As ICommonAppData = TryCast(Application.Current, ICommonAppData)
                If (app IsNot Nothing) Then
                    Return app.CurrentUser
                End If
                Return Nothing
            End Get
            Set(value As WebUser)
                Dim app As ICommonAppData = TryCast(Application.Current, ICommonAppData)
                If (app IsNot Nothing) Then
                    app.CurrentUser = value
                End If
    
                Throw New NotImplementedException("Current application does not implement required interface")
            End Set
        End Property
    
    End Module

    I'm trying to find out if this is an accepted practice to access global data, or if there is a complication I'm not realizing here (thread safety issues, etc.). If this is not the right place to store app global data (or perhaps even just user data), where do we put it in a Silverlight project? I had heard that perhaps I needed elevated permissions/trust in order to access Application.Current like this, so is this the case as well?


    Tuesday, March 17, 2015 12:13 AM

Answers

  • I always use Application.Current.Resources to store objects which will be shared across classes in a Silverlight or wpf application.

    Looking at a Silverlight application I have to hand, the UserId is stored for, well obvious reasons:

                if (e.InitParams.ContainsKey("userid"))
                {
                    int UserId = Convert.ToInt32(e.InitParams["userid"]);
                    Application.Current.Resources.Add("UserId", UserId);
                }

    And used:

            public void LoadMyCustomLinksList(Action<ServiceLoadResult<CustomLink>> callback, object state)
            {
                int userId = (int)Application.Current.Resources["UserId"];
                this.Load(base._context.GetListCustomLinksQuery().Where(l=>l.UserId==userId), lo =>
                {
                    callback(this.CreateResult(lo));
                }, state);
            }

    I use resources for whole collections of data in some applications.

    Take a look at how the usercontrols get their data in this:

    https://gallery.technet.microsoft.com/Dynamic-XAML-Composed-View-e087f3c1


    Hope that helps.
    Recent Technet articles: Property List Editing; Dynamic XAML

    Thursday, March 19, 2015 6:06 PM
    Moderator

All replies

  • Hi,

    You could try to use cookies and IsolatedStorage in silverlight to achieve this .

    Here are articles which talks about this ,please have a see:

    https://msdn.microsoft.com/en-us/library/dd920298(v=vs.95).aspx

    http://blogs.msdn.com/b/nikola/archive/2008/04/14/setting-cookies-through-silverlight.aspx

    Best Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.




    Wednesday, March 18, 2015 2:51 AM
  • Thanks for the reply, but I'm trying to find out if using Application.Current is a bad idea. If it is, why? I'll consider the alternatives if somebody can tell me if using Application.Current won't work or shouldn't be used in this manner.

    Chris

    Wednesday, March 18, 2015 2:09 PM
  • I always use Application.Current.Resources to store objects which will be shared across classes in a Silverlight or wpf application.

    Looking at a Silverlight application I have to hand, the UserId is stored for, well obvious reasons:

                if (e.InitParams.ContainsKey("userid"))
                {
                    int UserId = Convert.ToInt32(e.InitParams["userid"]);
                    Application.Current.Resources.Add("UserId", UserId);
                }

    And used:

            public void LoadMyCustomLinksList(Action<ServiceLoadResult<CustomLink>> callback, object state)
            {
                int userId = (int)Application.Current.Resources["UserId"];
                this.Load(base._context.GetListCustomLinksQuery().Where(l=>l.UserId==userId), lo =>
                {
                    callback(this.CreateResult(lo));
                }, state);
            }

    I use resources for whole collections of data in some applications.

    Take a look at how the usercontrols get their data in this:

    https://gallery.technet.microsoft.com/Dynamic-XAML-Composed-View-e087f3c1


    Hope that helps.
    Recent Technet articles: Property List Editing; Dynamic XAML

    Thursday, March 19, 2015 6:06 PM
    Moderator