Labels

Friday, September 3, 2010

State Management - In Silverlight

State management is the process to Maintain State And Page Information over multiple requests for the same or different pages.
Being HTTP Stateless - Web applications do not automatically indicate whether the multiple requests are all from the same client or even whether a single browser instance is still actively viewing a page or site.
A new instance of the Web page class is created each time the page is requested and with each round trip to the server pages are destroyed and re-created.

ASP.NET includes several options that help to preserve data on both a Per-Page basis, Per-User basis and an Application-Wide basis.

Client-Side - State Management
Server-Side - State Management
·         View state
·         Control state
·         Hidden fields
·         Cookies

·         Application State
·         Session State.
·         Profile properties
·         Database support


In Silverlight application everything is compiled in one XAP file and when a user requests this is copied to the client machine.
So there are no server round trips and page request every time, this reduces the need of State Managment. But in typical business application, State Management may be required.

State Management

·         Ordinarily, when the user visits/revisits to a Page,(Using either Forward/Backward/History/Navigation/Refresh), the Page is re-created from scratch.
·         i.e. When the user leaves the page, the page object is discarded from memory.
·         Thus a page is not able to maintain it’s Controls State (for example, a text box). i.e. They will reset to their default values on a return visit.
·         Similarly, any member variables in the page class are reset to their initial values.

State Management can be taken in two ways.

·         Maintaing State – Page Level - Local to the Page
·         Maintaing State – Application Level - Global to the Application

Page Level – Local to Page?

·         This means, storing the page state in a way that it cannot be accessed on some other page. i.e. available to the page only.  
·         Thus whenever the user visits/revisits to that Page, the page state (Control’s state & Class state) can be restored.

Application Level – Global to Application?

·         This means, storing the state in a way that it can be accessed from any page in the application.
·         This is achieved by storing DTO objects (e.g. UserInfo, ProductInfo) at the application level.
·         Thus whenever the user requires to use the stored information on any page, it would simply be required to fetch the information from the DTO object, stored at the application level.



State Management – Page Level
State Management – Application Level

·         Using Caching Feature
·         Using Page-Object State

·         Using Application State
·         Using Isolated Storage



Page Level – Caching Feature:

·         Require NavigationCacheMode(Page Control)/CacheMode (UserControl) property.

o    Disabled - Default value - Means no caching is performed.
o    Required - Frame will keep the page object in memory after the user navigates away. If the user returns, the already instantiated object is used instead of a newly created instance.
o    Enabled - Pages will be cached–up to a point. The key detail is the Frame.CacheSize property, which sets the maximum number of optional pages that can be cached.

Note:
·         This state get cleared when user clicks ‘Refresh’ in browser.
·         This state is not shared among differnt machines.
·         This state is not shared even between different browser windows, on the same machine.




<navigation:Page x:Class="StateManagement.CachedPage"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"               
                 xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"                
                 NavigationCacheMode="Required">
    <StackPanel x:Name="LayoutRoot" Background="White">
        <TextBlock TextWrapping="Wrap">This page is cached, so the text below is preserved.</TextBlock>
        <TextBox Margin="3"></TextBox>
    </StackPanel>
</navigation:Page>




Page Level – Object State:

Note:
·         This state get cleared when user clicks ‘Refresh’ in browser.
·         This state is not shared among differnt machines.
·         This state is not shared even between different browser windows, on the same machine.

e.g.



public partial class ProductCachedPage : Page
    {
        public static Product ProductState { get; set; }

        public ProductCachedPage()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            // Store the Form data.
            Product productStateStore = new Product();
            productStateStore.ProductId = Convert.ToInt16(txtProductId.Text);
            productStateStore.ModelNumber = txtModelNumber.Text;
            productStateStore.ModelName = txtModelName.Text;
            productStateStore.UnitCost = Convert.ToDouble(txtUnitCost.Text);
            productStateStore.Description = txtDescription.Text;

            ProductCachedPage.ProductState = productStateStore;           
            base.OnNavigatedFrom(e);
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // Retrieve the Form data.
            if (ProductCachedPage.ProductState != null)
            {
                Product objProduct = ProductCachedPage.ProductState;
                txtProductId.Text = objProduct.ProductId.ToString();
                txtModelNumber.Text = objProduct.ModelNumber;
                txtModelName.Text = objProduct.ModelName;
                txtUnitCost.Text = objProduct.UnitCost.ToString();
                txtDescription.Text = objProduct.Description;
            }
            base.OnNavigatedTo(e);
        }
    }



Application Level – Object State:

Note:
·         This state get cleared when user clicks ‘Refresh’ in browser.
·         This state is not shared among differnt machines.
·         This state is not shared even between different browser windows, on the same machine.

e.g.



public partial class ApplicationStatePage: Page
    {
        public static Product ProductState { get; set; }

        public ApplicationStatePage()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            // Store the Form data.
            Product productStateStore = new Product();            
            productStateStore.ProductId = Convert.ToInt16(txtProductId.Text);
            productStateStore.ModelNumber = txtModelNumber.Text;
            productStateStore.ModelName = txtModelName.Text;
            productStateStore.UnitCost = Convert.ToDouble(txtUnitCost.Text);
            productStateStore.Description = txtDescription.Text;

      App.Current.Resources.Add("ProductInfo", productStateStore);
            base.OnNavigatedFrom(e);
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // Retrieve the Form data.
            Product productStateStore = (Product)App.Current.Resources["ProductInfo"];

            if (productStateStore!= null)
            {               
                txtProductId.Text = productStateStore.ProductId.ToString();
                txtModelNumber.Text = productStateStore.ModelNumber;
                txtModelName.Text = productStateStore.ModelName;
                txtUnitCost.Text = productStateStore.UnitCost.ToString();
                txtDescription.Text = productStateStore.Description;
            }
            base.OnNavigatedTo(e);
        }
    }




Application Level – Isolated Storage:

Note:
·         This state does not get cleared when user clicks ‘Refresh’ in browser.
·         This state is not shared among different machines.
·         This state is shared even between different browser windows, on the same machine.

Imp:

·         The state is restored even when user browses the application on another day. i.e. It works like a Profile.
·         To stop this restoration you need to clear the state, on the application exit event. But as a side effect, the state will start clearing on ‘Refresh’.





public partial class ApplicationStatePage: Page
    {
        public static Product ProductState { get; set; }

        public ApplicationStatePage()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            // Store the Form data.
            Product productStateStore = new Product();            
            productStateStore.ProductId = Convert.ToInt16(txtProductId.Text);
            productStateStore.ModelNumber = txtModelNumber.Text;
            productStateStore.ModelName = txtModelName.Text;
            productStateStore.UnitCost = Convert.ToDouble(txtUnitCost.Text);
            productStateStore.Description = txtDescription.Text;

            IsolatedStorageSettings.ApplicationSettings["ProductInfo "] = productStateStore;
            base.OnNavigatedFrom(e);
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // Retrieve the Form data.
            Product productStateStore = (Product) IsolatedStorageSettings.ApplicationSettings["ProductInfo "];
            if (productStateStore!= null)
            {               
                txtProductId.Text = productStateStore.ProductId.ToString();
                txtModelNumber.Text = productStateStore.ModelNumber;
                txtModelName.Text = productStateStore.ModelName;
                txtUnitCost.Text = productStateStore.UnitCost.ToString();
                txtDescription.Text = productStateStore.Description;
            }
            base.OnNavigatedTo(e);
        }
    }




Hope this helps.

Thanks & Regards,
Arun Manglick

No comments:

Post a Comment