Jean Paul's Blog

There are 2 types of People in the World, One who Likes SharePoint and..

  • Microsoft MVP

  • MindCracker MVP

  • CodeProject MVP

  • eBook on SharePoint 2010

  • eBook on Design Patterns

  • eBook on Windows Azure

  • NLayers Framework @ CodePlex

  • MSDN Forums

  • .Net vs. Java

    Due to Public Demand

Advantage of [ServiceDependency] in WCSF – Instance Reuse

Posted by Jean Paul on April 6, 2011

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.


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:


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();


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 4: 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)


Step 5: Declare the property with [ServiceDependency]

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


public Utility Utility





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 6: 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.


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()




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

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s