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

Archive for April, 2011

How to get index using Lambda Expressions?

Posted by JP on April 28, 2011

class Program
{
    static void Main(string[] args)
    {
        List<int> list = new List<int>();
        list.Add(1);
        list.Add(1);

        list.Where((a, i) => a == i).ToList().ForEach(i => Console.WriteLine(i.ToString()));
        Console.ReadKey(false);
    }
}

Advertisements

Posted in C# | Tagged: , , | Leave a Comment »

How to check condition on all elements using Lambda Expressions?

Posted by JP on April 28, 2011

static void Main(string[] args)
{
    List<int> list = new List<int>();
    list.Add(1);
    list.Add(2);
    list.Add(3);
    list.Add(4);
    list.Add(5);

    bool result = list.All(e => e > 0);
    Console.WriteLine(result.ToString());
    Console.ReadKey(false);
}

Posted in C# | Tagged: , , | Leave a Comment »

Sql Server – Transact Sql to make a column unique

Posted by JP on April 23, 2011

ALTER TABLE TableName
ADD CONSTRAINT NewConstraintName
UNIQUE(ColumnName)

Posted in SQL Server | Tagged: , , | Leave a Comment »

How to shrink Sql Server Database?

Posted by JP on April 21, 2011

The Enterprise Management studio provides an option to shrink the database.

 

Step 1: Open Enterprise Management Studio (i am using sql server 2008) and select database, right click and choose Tasks>Shrink

 

image

Step 2: In the appearing dialogue press Ok to start the shrink process.

image

Posted in SQL Server | Tagged: , , | Leave a Comment »

TDD – Red Green Refactor Example

Posted by JP on April 7, 2011

 

In this tutorial I would like to explain TDD (Test Driven Development) with the common Red Green Refactor example.

A note on TDD

TDD (Test Driven Development) is a software development process in which the unit test will be written first and after that the original code. In TDD, the design and development of the code is through the unit tests.

In the traditional unit tests, the unit test is written after the original code is written. This is only for long term maintainability. But in TDD the unit test is written first and code evolved through it.

Red Green Refactor

Red Green Refactor is an interesting concept in TDD. The stages are given below:

Red – First a failing unit test is created and it results in red status

Green – We will modify the associated code to just make the unit test pass – resulting in green status

Refactor – Once the test is passing we can refactor the code so that the original implementation is done.

Example: In the following example, I would like to take you through the Red Green Refactor steps. Here we are trying to create a Stack data structure which is already in the library of .Net. We can call our stack as MyStack which operates on string and have Push method.

Step 1: Create a red test project

Create a new test project and name it TDDExample.clip_image002

Step 2: Rename the default UnitTest1.cs to MyStackTest.cs

After renaming the file name just add the following unit test.
[TestMethod]

public void MyStack_Push_Test()

{

MyStack myStack = new MyStack();

bool result = myStack.Push(“abcd”);

Assert.AreEqual(true, result);

}

Please note that first we written the unit test.. till now the original MyStack class is not created.

So place the cursor over MyStack and use Ctrl+. (to Generate Class for us). Repeat the same for Push() method to generate the default method. So now the new class will look like:

class MyStack

{

internal bool Push(string p)

{

throw new NotImplementedException();

}

}

Step 3: Run the unit test and get Red

Run the unit test and you can see the test will fail.

clip_image004

Step 4: Modify the method just enough to make it Green

In this step we are just trying to make the unit test pass. We are returning true without adding the implementation inside the method. So the new method will look like:

internal bool Push(string p)

{

return true;

}

Running the test again, we can see the test passes.

clip_image006

Step 5: Refactor

Although the test is passing now, the actual implementation is not right. Now we need to add the right code in our Push() method. This will be the Refactor step of Red Green Refactor style and the unit test should be passing again.

Our actual implementation would be storing the string to an internal list. The final class would look like:

class MyStack

{

private IList<string> _list = new List<string>();

public bool Push(string str)

{

_list.Add(str);

return true;

}

}

Running the unit test will be green again.

clip_image006[1]

Conclusion

In this tutorial we were trying to create a small example of TDD involving Red Green Refactor style.

Posted in C# | Tagged: , , , , | Leave a Comment »

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/

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