Advantage of [ServiceDependency] in WCSF - Instance Reuse

Web Client Software Factory provides with a good feature through [ServiceDependency] attribute.

This attribute allows us to make use of the Dependency Injection and Instance Reuse advantages.

Example

Let us have an example to learn the [ServiceDependency] attribute.

Step 1: Create a new WCSF project

You can open the Visual Studio 2010, and use File>New Project and enter the project name as shown in the image below:

WCSF1.gif

In the appearing wizard after screen above click Finish.

Step 2: Create a Utility class in Shell module

You can view from the solution explorer that there is a Shell module.

Right click on it and add new class named Utility which is having a single method named Greetings();

WCSF2.gif

After the actions the solution explorer looks like above image.

The content of Utility class will be:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ServiceDependencyTest.Shell
{
    public class Utility
    {
        public Utility()
        {
        }

        public string Greetings()
        {
            return "Hello There!";
        }
    }
}


Step 3: Specify the class Utility in global services

The Web Client Software Factory framework uses the term services for instances of classes that implement a particular interface. The services would be accessible through a service locator.

For specifying our class instance as a service we have to modify the ShellModuleInitializer.cs

Note: There are global and module services. The services specified as global would be accessible to all modules in the application and the services specified as module is visible only to the module in which it is declared.

In our case we are going to declare the Utility class in the global services. For this add the line
globalServices.AddNew<Utility, Utility>(); in the method AddGlobalServices(IServiceCollection globalServices)

WCSF3.gif

Step 4: Declare the property with [ServiceDependency]

Now we proceed with declaring a new property named Utility in the DefaultViewPresenter class.

[ServiceDependency]
public Utility Utility
{
    get;
    set;
}


Please note that we marked the property with [ServiceDependency] - this will automatically take care of the instance injection.

The other way of instance creation is using the [CreateNew] attribute.

Step 5: Testing the application

Back to the web application we can place a label and button on the form. On click of the button use the following code.

protected void Button1_Click1(object sender, EventArgs e)
{
    Label1.Text = this._presenter.Utility.Greetings();
}

Now you can run the application and clicking the button you will be able to see the greetings text.

WCSF4.gif

Note: The Model View Presenter (MVP) constraints are violated in the above example by placing button handler code inside the view inorder to make the example more simple.

Instance Reuse

The WCSF provides the flexibility of instance reuse in the above scenario. You can verify it by placing a breakpoint in the constructor of Utility class and pressing button multiple times. There will be only one hit of the breakpoint. For getting the complete feel through running application, just replace the content of Utility class with the following.

public class Utility
{
    public Utility()
    {
        _InstanceCount++;
    }

    private static int _InstanceCount;

    public string Greetings()
    {
        string msg = string.Format("(From instance: {0})", _InstanceCount);

        return "Hello There!" + msg;
    }
}


Using Code

The WCSF 2010 framework is needed to run the application. For installing WCSF please refer to this Article.

Up Next
    Ebook Download
    View all
    Learn
    View all