Part 5: WCSF : Power of [ServiceDependency] attribute

In this part I would like to demonstrate the power and advantage of [ServiceDependecy] attribute in WCSF framework.

Advantage

We can restrict the instance creation of classes and make reuse a single instance across page requests.  This would drastically decreases memory utilization and simultaneously reduces the instance management code and makes the easier.

You can use the following class types to be shared using [ServiceDependency]

  • Data Access Classes
  • Web Service Classes
  • Utility Libraries
  • using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

     

    namespace GeneralLibrary

    {

        public class Utility

        {

            private static int _instanceCount;

     

            public Utility()

            {

                _instanceCount++;

            }

     

            public string GetMessage()

            {

                return string.Format("Instance Number: " + _instanceCount.ToString());

            }

        }

    }

    We can open the previous project and work upon it.

    After opening the WebClientApplication1, create a new Class Library project into it.  Let us call it GeneralLibrary.  After creating the project, add a class named Utility.cs into it.  The code should be:

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

     

    namespace GeneralLibrary

    {

        public class Utility

        {

            private static int _instanceCount;

     

            public Utility()

            {

                _instanceCount++;

            }

     

            public string GetMessage()

            {

                return string.Format("Instance Number: " + _instanceCount.ToString());

            }

        }

    }

    Basically, the above class will be counting the instances of itself using a static counter and it is exposing a method named GetMessage() that will give us information on number of instances created.

    Creating the test infrastructure

    For calling the above method we need to place a button and label on the MyData.aspx.  Rename the label to InfoLabel.

    image

    On the button click event place the following code:

    protected void ServiceDependencyCheck_Click(object sender, EventArgs e)

    {

    this.Presenter.ServiceDependencyCheck();

    }

    Place the following code in MyDataPresenter.cs

    public void ServiceDependencyCheck()

    {

          this.View.InfoMessage = Utility.GetMessage();

    }

    Create a new string property in IMyDataView interface

    public interface IMyDataView

    {

               string InfoMessage { get; set; }

    }

    Implement the property back in our MyData.aspx

    public string InfoMessage

    {

        get

        {

            return this.InfoLabel.Text;

        }

        set

        {

            this.InfoLabel.Text = value;

        }

    }

    Now we have to set the instance creation of Utility class inside the module

    Place the following code in ShellModuleInitializer.cs

    globalServices.AddNew<Utility, Utility>();

    inside method AddGlobalServices()

    Testing the Application

    Now you are ready to test the application.  Press F5 and execute the application.  In the appearing screen, press the “Service Dependency Test” button.  You can see the following output.

    image

    Even if you click the button multiple times, only one instance of the Utility class is created.

    Leave a Reply

    Your email address will not be published. Required fields are marked *