Mhazad's Blog

January 12, 2011

Short cut to generate different property snippets in Visual Studio

Filed under: C#, Visual Studio — Mahmudul Haque Azad @ 4:15 am

These may be familiar to you. But many of you may not know the shortcut mentioned in the 2nd point here 🙂

1. Simple property, type prop and press tab.

2. Simple property with backing field, type propfull and press tab.

3. Simple property with private set, type propg and press tab.

4. Dependency property, type propdp and press tab.

Advertisements

December 24, 2010

Creating Sample Data in Expression blend

Filed under: Uncategorized — Mahmudul Haque Azad @ 11:00 am

In expression blend sample data can be generated easily so that they can be used to design any WPF application close to real one. In this article I shall try to show you how to create sample data (both normal collection and hierarchical collection) step by step. You can click on each image to see it in high resolution.

Creating Normal Collection

Follow the following 6 step to create a normal collection of sample data.

 

 

Creating Hierarchical Collection

 

In order to create hierarchical collection, the first 4 steps mentioned for normal collection will be followed here. After that you need to follow the following 3 more steps to create hierarchical collection of sample data.

 

 

December 13, 2010

WPF property compatible with XAML binding and Code behind binding.

Filed under: C#, Dependency Property, WPF, XAML — Mahmudul Haque Azad @ 7:33 am

WPF and dependency property are indispensable part. Without dependency property and MVVM you are almost no where in WPF development. But improper use of dependency property can create night mare.

Let’s take the following example.

Suppose we have created a custom media player control and we want to bind the Source property from XAML. It is to be noted that once the source property is set we need to do some additional related thing.

Here is the initial code snippet of Source Dependency Property

And here is our XAML

Now if we run our application we shall see that none of the code written after

SetValue(SourceProperty, value);

Will execute.

The reason behind this is If we set the property from XAML the SetValue() method is called directly. So our player will not work if we bind the source property from XAML!

We can easily overcome this problem if we use the following overloaded version of DependencyProperty.Register method.

As you can see that the new FrameworkPropertyMetaData is nothing but a method which is fired once the Source property is changed irrespective whether it is changed from XAML or code behind. Since we are setting the Source property in OnMediaSourceChanged method so now the Set method of source will execute with all business logic! Seems our problem is solved!!

But the fact is it is not solved yet rather it gives birth of another problem. If we set the Source property from the code behind, the Set property of Source along with related code written in Set method will executedtwice, one for setting it from code behind and another from OnMediaSourceChanged method.

The solution of this problem is, keep the Set property free from any business logic (or any code!). Rather whatever code is needed to execute, keep those in some separate method and call that method from OnMediaSourceChanged.

Here comes the code that is fully compatible with XAML binding and code behind.

March 15, 2010

Modifying GhostDoc XML Comment

Filed under: Code Commenting, Documentation — Mahmudul Haque Azad @ 5:25 am

Today while browsing I came across Matthew M. Osborn’s Blog and I found this fantastic line

“Documentation is the coders best and worst friend. It is a two sided sword, it makes your life easier but no one likes writing it. So coders do what we do best and find a way to make the computer do it for us.”

Ideally I use GhostDoc to make comments for my codes and really it works fine for me. From the very first day since I have been using this, I always thought whether I can change the xml documentation as per my need or not.

Today I have found a simple trick to add to custom node to the xml commenting.

Here was my scenario,

All my method/class/event level comment must include author name along with the time when it was written.

For this I need to add the following lines in the Ghost Doc > Options > Custom Texts

<author>$(Environment.UserName)</author>
<datetime>$(Environment.Date)-$(Environment.Time)</datetime>

And the ghost doc generated comment contains author and datetime tag!!!

///<summary>
/// Gets the user by ID.
///</summary>

///<param name=”ID”>The ID.</param>
///<returns></returns>

///<author>Mahmudul.Haque</author>
///<datetime>3/15/2010-11:23 AM</datetime>
private static User GetUserByID(long ID)
{
User user = new User();
return user;
}

January 6, 2010

Five Steps to SVN Merging

Filed under: Source Control, Subversion — Mahmudul Haque Azad @ 8:51 am

Few Points to ponder!

  • Always start on Green Light! i.e. start merging when both Trunk and Branch are fully committed and they are showing green color.
  • Merging always take place within a working copy i.e. in trunk.    
  • In every commit do not forget to add comment in the comment box. Mind it this is important!

 

Use Case for Merging

  • We have a main trunk
  • We have taken a branch named “CallForStandardFeedBack” branch.
  • After taking this branch, codes in both trunk and branch are modified by concerned developers.
  • Now we want to merge “CallForStandardFeedback” branch back to trunk and we have lots of changes both in trunk and merge.

Merging step: 1

  • All files are committed and we have green tick mark in our folder. Now we can start merging.

 

 

 

 

 

 

 

 

Merging step: 2

  • Right click on Trunk go to TortoiseSVN->Merge and Click Merge

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Merging step: 3

  • Select “Merge a range of revisions” and click next.

Merging Step 4

  • Select the value as appropriate in the picture below.


 

 

 

 

 

 

 

 

 

 

 

 

 

 

Explaining ??

  • Range Of Revision: Range of revision is actually all those range of changes that we want to merge with trunk. As we are merging a full branch with trunk so we will select all the ranges like below.


     

 

 

 

 

 

 

 

 

 

 

 

  • Reverse Merge: Reverse Merge is basically to decouple the changes the was committed with trunk before while merging one particular branch with trunk. Say for example we have implemented one menu in one branch and merged it with trunk. After few days it is needed to rollback every code related with that branch. In that case we will use reverse Merge.

Merge Step 5

  • Use the default value in this form and click merge to get the job done!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

After merging you will get the result like below. Now handle only conflicted files and do an integration test!

 

 

 

 

 

 

 

 

 

 

Happy Merging!

January 5, 2010

Database Driven Unit Testing Using VSTS

Filed under: ASP.NET, Software Testing, Visual Studio — Mahmudul Haque Azad @ 7:14 am

The primary concern about Database driven Unit testing is to maintain the test database intact. To elaborate suppose we have an add method that adds customer information in the database and we have a get method that actually returns a list of customer from database.

So we will have to different Unit test method for AddCustomer() and GetCustomer().

Now in order to pass the GetCustomer() method as a test case we have decided that it will return exactly 3 records and we have setup our test database in such a way to meet our test criterion.

The problem will be raised if we test the AddCustomer() method before GetCustomer() Method as AddCustomer() method will add one customer and our GetCustomer() Method will return 4 records that will fail its test case.

In order to overcome such situation we can use the power of TransactionScope Object.

In order to use this you must include System.Transactions in the Unit Test Project references and add System.Transactions in the reference section.

In the class generated by VSTS  Unit Test wizard you can see one region commented like below.


#region Additional test attributes

//

//You can use the following additional attributes as you write your tests:

//

//Use ClassInitialize to run code before running the first test in the class

//[ClassInitialize()]

//public static void MyClassInitialize(TestContext testContext)

//{

//}

//

//Use ClassCleanup to run code after all tests in a class have run

//[ClassCleanup()]

//public static void MyClassCleanup()

//{

//}

//

//Use TestInitialize to run code before running each test

//[TestInitialize()]

//public void MyTestInitialize()

//{

//}

//

//Use TestCleanup to run code after each test has run

//[TestCleanup()]

//public void MyTestCleanup()

//{

//}

//

#endregion

From here just uncomment MyTestInitialize() and MyTestCleanup() method.

Now Declare one global variable of TransactionScope Object write the body of the methods like below


TransactionScope ts;

///Use TestInitialize to run code before

///running each test

[TestInitialize()]

public
void MyTestInitialize()

{

ts = new
TransactionScope();

}

///Use TestCleanup to run code after

///each test has run

[TestCleanup()]

public
void MyTestCleanup()

{

ts.Dispose();

}


After this before executing any Unit Test method MyTestInitialize() method will be called where a new instances of TransactionScope will be created and and after executing the Unit test method the object will be disposed which will ultimately rollback all the changes committed by the Unit test Method keeping our test database intact.

Happy Testing!

September 8, 2009

Magic of JQuery in ASP.NET, Simplifying AJAX

Filed under: AJAX, ASP.NET, JQuery — Tags: , , , , — Mahmudul Haque Azad @ 4:58 pm

JQuery is a powerful tool that can be used to enhance our ASP.NET Application.

In the time of development we often face problem like “need to show certain data based on a button click” or “need to implement tab in pages where each tab contains costly DB operation”.

There is lots of way to do this using traditional AJAX like ASP.NET AJAX. But if we consider the hassle related with UpdatePanel then we can find a few people who will go for it.

Let’s see how we can achieve this short of Magic in our application easily with JQuery!

For simplicity let’s assume that we have one aspx page with one div and one button. After clicking the button one extensive database operation will be held and the div’s inner html will be updated without page refresh.

It’s to be noted that in order to leverage this facility of accessing Page Method through client script we need to add script manger and need to enable page method.

<asp:ScriptManager ID="ScriptManager1" runat="server"
EnablePageMethods="true">
</asp:ScriptManager>

<asp:panel ID="divExtensiveData" runat="server"></asp:panel>

<asp:Button ID="btnFetchData" Text="Fetch Data" runat="server" OnClientClick="fetchData(); return false;" />


Now let’s write one method in code behind i.e. in aspx.cs file that will do the actual db operation.

[WebMethod]
public static string fetchData(string someParameter)
{
  string result = string.Empty;

  /// Do extensive db operation
 
/// Assign value to result
  return result;


Now we will get back to our aspx page and will write one method to access this page method.

<script type="text/javascript">
function fetchData(parameter)
 { 
  PageMethods.fetchData(parameter,dataFetched,dataNotFetched);
 }

function dataFetched(result)
{
 var panelID = '<%=divExtensiveData.ClientID %>';
 
$("#"+panelID).html(result);
}
function dataNotFetched()
{
 var panelID = '<%=divExtensiveData.ClientID %>';
 
$("#"+panelID).html("<h1>Error Occured.</h1>");
}
</script>


Now explaining the page method call from script. In our call to page method  I have given three parameters whereas the page method takes only one. The second one is just  a reference of the method that will be executed after successfully page method call and the third one is the reference of the method that will be called if the page method encounter some error.

Just use this concept and be a magician of JQuery in ASP.NET!!! CodeProject

Blog at WordPress.com.