Disciplined Application State for WPF
April 30, 2015 Leave a comment
In the last article a concept for implementing an organized technique for managing session and application state for an ASP.NET application was proposed.
The same can actually be done for a WPF application, though such global properties would be most likely be on the minimal side of things. Nonetheless, there are times where having access to a property at application level can be quite convenient.
Unlike an ASP.NET application, the only capability that you have in this respect to a WPF application is that of application level state.
Like the previous proposal where you had two classes within a class library, the same is true for WPF application state. You have a class that contains the public properties you want the application to manage and the actual state management class itself.
Thus, we would have a “StateProperties” class as shown below…
Imports System.Windows Public Class StateProperties #Region "Constructor(s)" Public Sub New() End Sub #End Region #Region "Public Properties - Items" Public Property APPLICATION_DATABASE_CONNECTION_STRING() As String Get Dim loStateManagement As New Classes.StateManagement() Return (loStateManagement.Get_Property("spsApplicationDatabaseConnectionString")) End Get Set(ByVal Value As String) Dim loStateManagement As New Classes.StateManagement() loStateManagement.Save_Property("spsApplicationDatabaseConnectionString", Value) End Set End Property #End Region End Class
The above class could have as many properties as one would require for their application. And as noted it calls the
actual management class, “StateManagement” as shown below…
Imports System.Windows Friend Class StateManagement #Region "Constructor(s)" Friend Sub New() End Sub #End Region #Region "Friend Methods" Friend Sub Save_Property(psKey As String, poValue As Object) Application.Current.Properties(psKey) = poValue End Sub Friend Function Get_Property(psKey As String) As Object Return (Application.Current.Properties(psKey)) End Function Friend Sub Remove_Property(psKey As String) Application.Current.Properties.Remove(psKey) End Sub #End Region End Class
Note that the “StateManagement” class has three simple functions; “Save_Property”, “Get_Property”, and “Remove_Property”, all of which are self explanatory.
Again, as with the same technique suggested for ASP.NET applications, this concept may add a little more work to your application development but I believe the cleanliness and organizing capability of the it adds nicely to a well planned application…