.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Advantage of [ServiceDependency] in WCSF - Instance Reuse

Posted By:Jean Paul       Posted Date: April 05, 2011    Points: 200    Category: ASP.NET    URL: http://www.dotnetspark.com  

Instance Reusing is an advantage WCSF provides. Web Client Software Factory provides with a good feature through [ServiceDependency] attribute.
 

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:

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.

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

    {

        _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 http://www.c-sharpcorner.com/UploadFile/40e97e/4402/


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend