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

Posts Tagged ‘wcsf’

Advantage of [ServiceDependency] in WCSF – Instance Reuse

Posted by JP 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.

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:

clip_image002

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

clip_image004

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)

clip_image006

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.

clip_image008

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/

Advertisements

Posted in ASP.NET | Tagged: , , , , | Leave a Comment »

Web Client Software Factory (WCSF) and Model View Presenter (MVP)

Posted by JP on November 30, 2010

MVP

One of the remarkable point of WCSF framework is the support for Model View Presenter (MVP).  MVP was quite popular and it is really flexible provided with a little learning curve involved.

Model constitutes the application specific Logic.

View would be basically the web page we are dealing with.

Presenter is the code that wires the view and model

Advantages

Decoupling: MVP and MVC provides the decoupling of user interface from the application logic. This makes the code more manageable and flexible for unit testing.

Multiple Views: Another advantage would be support for multiple views. Suppose you have a model of account transactions. Depending on the user type, you can show it in a chart page view or a table view.

Easier Replace: We can easily replace one portion of code without affecting the other. Suppose there is a change in the html page, we can do the massive change with less impact on the model and presenter code. Similarly if there is a change in data fetch service we can do that in the presenter, without even touching the view/model.

Implementation

In reality, the View would be an interface that is implemented by a web page. Model and Presenter should be actual classes which are tied to the View interface.clip_image002

Example

After setting up the IDE correctly using previous article, we can try to create a web page having one textbox and button. Here the textbox provides the Name of model.

On clicking the button the data will be saved to session.  Let us see what is the infrastructure needed for that.

Step 1: Create new Page

Right click on our previous WCSF project and use the context menu “Web Client Factory”> Add Page with Presenter

clip_image004

The following wizard will appear:

clip_image006

Enter the view name as “MyData” and click Finish.

Right click on the MyData.aspx and make it Default page.

Step 2: Add Data property to IDefaultView

Open the IMyDataView.cs from Shell project and add the following property inside it.

clip_image008

As the MVP provides decoupling of user controls from code, we need to create properties for each user interface element. Depending on the Property Type we will be having different UI controls.

Property Type

UI Control

String

TextBox

Bool

CheckBox

List

ListBox

List<Entity>

GridView

Step 3: Implement the Data property

Now we need to implement the new Data property inside our aspx page.

First we have to create a textbox in the webpage and connect it to the Data property.

For this place a new textbox and name it txtData.  Place a button too aside.

clip_image010

Create the property named Data inside the MyData.aspx page like following.

clip_image012

Step 4: Build the solution

If the build succeeds we are in good position. You can try executing the solution. From the presenter we can access the current value of Data using View.Data property.

Posted in ASP.NET, Web Client Software Factory | Tagged: , | Leave a Comment »

Installation of WCSF for Visual Studio 2010

Posted by JP on November 25, 2010

Step 1: First we have to install Guidance Automation Extensions for 2010. Guidance Automation provides the infrastructure for automated project creation actions inside Visual Studio IDE.

http://visualstudiogallery.msdn.microsoft.com/en-us/25e4b5e9-65e4-4950-967d-5f1e6a9dcbeb

clip_image002

Click on the Install button to proced.

Step 2: After installing the GAX 2010, you can proceed with downloading the actual WCSF 2010.

http://visualstudiogallery.msdn.microsoft.com/en-us/8fc2c9d6-5282-404b-9c58-e72b326b1e26

clip_image004

Click the Install button to proceed with and you could see the status below.

clip_image006

Validating the Installation

You can validate the installation by running VS 2010 and creating a new WCSF solution.

Execute VS 2010 and choose File > New Project

You should be able to select the project option below:

clip_image008

Select “Web Client Solution” from the dialog and enter the project name and click Ok button.

Now you will be prompted with further WCSF project options

clip_image010

Click Finish to complete the wizard. This will create 1 Shell Module and 1 website with all the necessary library file references added.

Back in the IDE you can try executing the project. You should be able to see the application below.

clip_image012

Posted in ASP.NET, Web Client Software Factory | Tagged: , , | Leave a Comment »

Part 5: WCSF : Power of [ServiceDependency] attribute

Posted by JP on November 11, 2010

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.

    Posted in ASP.NET | Tagged: , , | Leave a Comment »

    Part 2 of 7 : Installing WCSF

    Posted by JP on November 10, 2010

    We can download “Web Client Software Factory – February 2008” from the following link and install it.

    http://www.microsoft.com/downloads/en/details.aspx?FamilyId=47755838-049B-4291-AFEA-A379AE28D7B3&displaylang=en

    Additional Requirements

    WCSF requires following things to be installed.

    • Windows XP/Vista/2003
    • Visual Studio 2008
    • .Net 3.5

    Verification

    After installation you will be able to see the following new project type.

    image

    Posted in ASP.NET | Tagged: , , | Leave a Comment »

    Part 1: Web Client Software Factory

    Posted by JP on November 10, 2010

    Web Client Software Factory (WCSF) is a framework for Web Client creation with proven practices and patterns.  It is built up on ASP.NET.

    Advantages of WCSF

    • Model View Presenter (MVP) support
    • Automatic Service Instance Creation resulting in reduced coding and memory overheads
    • Hides Complexity in traditional ASP.NET application
    • More support for Unit Tests
    • Reusability through Modules

    Reference

    http://msdn.microsoft.com/en-us/library/ff648752.aspx

    Posted in ASP.NET, Web Client Software Factory | Tagged: , , | Leave a Comment »

    Part 4: WCSF : Adapting to Model View Presenter (MVP) architecture

    Posted by JP on November 9, 2010

    One of the cool benefits of WCSF is the support of Model View Presenter (MVP). 

    The advantages of MVP are many:

    • Isolation of User Interface management code through Presenter
    • Isolation of Business Logic code through Controller
    • Improved Unit Test support for code

    Note: The MVP here also contains a Controller class

    Learn by Example

    From the previous part, we can try to create a web page having one textbox and button.

    On clicking the button the data will be saved to session.  Let us see what are the infrastructure needed for that.

    Step 1: Create new Page

    Right click on our previous WCSF project and use the context menu “Web Client Factory”> Add Page with Presenter

    image

    The following wizard will appear:

    image

    Enter the view name as “MyData” and click Finish.

    Right click on the MyData.aspx and make it Default page.

    Step 2: Add Data property to IDefaultView

    Open the IMyDataView.cs from Shell project and add the following property inside it.

    image

    As the MVP provides decoupling of user controls from code, we need to create properties for each user interface element.

    This could be like Name, Address, HasLicense etc. for user controls like TextBox, RadioButton etc.

    For DataGridView we will need to declare properties like IList<Employee> etc.

    Step 3: Implement the Data property

    Now we need to implement the new Data property inside our aspx page.

    First we have to create a textbox in the webpage and connect it to the Data property.

    For this place a new textbox and name it txtData.  Place a button too aside.

    image

    Create the property named Data inside the MyData.aspx page like following.

    image

    Step 4: Build the solution

    If the build succeeds we are in good position.

    Posted in ASP.NET, Web Client Software Factory | Tagged: , , | Leave a Comment »

    Part 3: WCSF : How to create a new solution?

    Posted by JP on November 9, 2010

    This article shows how to create a new web project in Web Client Software Factory.

    Step1: Select “Web Client Solution”

    Open your Visual Studio and select File>New Project.  Select the Web Client Solution from the category shown in image.

    image

    Click Ok and you will get the following wizard.

    image

    Click Finish to end the wizard.

    Posted in ASP.NET, Web Client Software Factory | Tagged: , , | Leave a Comment »