Monday, September 24, 2012

What is new in C# and Visual Basic in Visual Studio 2012



This post discusses the new language features that is introduced in Visual Studio 2012. One of the main things that language team introduced is the idea of producing windows 8 App. These APPs are talking to new API called windows runtime. Small new feature Caller Info Attributes is also added in this release. More about this feature can be read here.
image
This release also brings iterators to Visual Basic. The main discussion in this post is around Asynchronous programming in C# language.
image 
If you want to give importance on responsiveness of client and scalability in server App then you probably call Asynchronous API’s. These API’s take Callbacks as parameters and uses that Callback to notify you about result of available. When you start write these call backs then it is harder maintain the code but with new Async language support that introduced in  Visual Basic and C# and it is easy consume these API’s.
Below is the synchronous code in C#
image
The code basically Searches the movie index in Netflix using OData by taking Year as parameter. While it is downloading the movies using above code , you can notice you can not interact with your user interface. You can scroll along UI and explore the movies only after the search operation is complete. Users expects more responsive apps.
Why the above code is Unresponsive? 
The call to DownloadString is of webclient type and it asks you to wait till it returns string. During this process your UI is going to hang-up and holds the thread until it returns the result back you.
Two different options here, you can put this code on background Thread and it is bit complicated. You may have to marshal and unmarshal the code. The second option is to interact with Asynchronous API there you have an overloaded function DownloadStringAsync, But when you use this method you are no longer take back data as result. You need to sign-up a call-back when a result is available. If you have multiple Async calls then you have to write multiple call-backs. In this case your code is getting more difficult to understand.
Solution
The new overload method is DownloadStringTaskAsync, This method now returns string of type Task, Task of string is object that notify you when result is available. Now you can change the signature of the method QueryMovies and mark this as async. This tells the compiler that this method is pausable and resumeable , they can wait without locking to the UI thread. Now change the return type of method from Movie array to Task<Movie[]> array. Write a word await to get the string from Task of objects from DownloadStringTaskAsync call.
image
Now you can call the above even from while loop
image
The compiler is now able to figure out how to handle the await inside the while loop and resume it when it gets the result. Now you should be able to interact with your UI without any hang-ups.
Reference: Alex Turner talk in Visual Studio launch

Caller Info Attributes in C# 5.0


Caller Info Attributes is a new feature in C# 5.0, Caller Info Attributes instructs the compiler to obtain and feed information into the parameter’s default value. You can tag optional parameters using Caller Info Attributes.
image
Three Caller Info Attributes are
  • [CallerMemberName]  – It applies the caller’s member name
  • [CallerFilePath] -  It applies the path to the caller’s source code file
  • [CallerLineNumber]-  It applies the line number in the caller’s source code file
The below piece of code demonstrates all three attributes
   1: using System;

   2: using System.Runtime.CompilerServices;

   3: class Program

   4: {

   5: static void Main()

   6: {

   7:   Demo();

   8: }

   9: static void Demo (

  10: [CallerMemberName] string memberName = null,

  11: [CallerFilePath] string filePath = null,

  12: [CallerLineNumber] int lineNumber = 0)

  13: {

  14:    Console.WriteLine (memberName);

  15:    Console.WriteLine (filePath);

  16:    Console.WriteLine (lineNumber);

  17: }

  18: }
The output of the code as below
Main
C:\Projects\Program.cs (Assuming your project is located in this path)
16
Caller Info attributes are useful for writing logging functions and for implementing change notification patterns. The following method can be called from property’s set accessor
   1: void RaisePropertyChanged( [CallMemberName] string propertyName = null)

   2: {

   3:  

   4: }
More can be read here

Software Testing with Visual Studio 2012



Microsoft Test Manager is a tool which comes with Visual Studio 2012 , which was built from the scratch to support the needs of Manual testers.  Using this tool you can manage and track test cases that helps both developer and tester.This post gives you an overview on Microsoft Software Testing tools.
image
Back ground
Prior to the release of Visual Studio 2010 launch the testing land scape and testing life cycle was shown as below
image
When you look at the Software Testing land scape, you can see the Manual testers in left , they look at the new test cases and new builds that are coming along and run through their test runs and report bugs back to the developers.
image
Far right end of the picture, You can see developers who are specialists in writing the code. You all know the 70% of the testing on software happens in manual testing team. Manual testers can adapt and respond to the changes in underlining applications. The typical tools that manual testers use are Excel and Word. So we need a tool for Manual testers !!!
We all know when testers do find a bug and fires to developer then developer says it works on my machine, this is the problem that happens all the time
image
There is some time waste in going back and forth between testers and developers to try to understand how the developer recreate the issue, we want to make this happen in automated fashion!
The tools are building up and automatically capturing lot of rich details bugs can give the developer everything they need at their finger tips to understand what happen and importantly fix for it.
Microsoft Test Professional is part of Visual Studio 2012 family
image
Microsoft Test Professional can be purchased as a standalone tool or it includes Visual Studio 2012 premium and ultimate editions.
Build – Deploy – Test
image
Lab Management in VS 2012 can allow you to take code changes that developers modeling in source control and press a single button which will compile those changes and deploy them into one or more environments and run all of your automated tests against those environments.
As a manual tester if you want to test different varieties of environments like legacy operating systems, may in different browsers or may be different database projects. So Lab Management makes manual tester life more easier for creating new environments.
image
By tying all those processes and tools together , you can track the software quality all the way from requirements through their test cases. You can now say how the software quality is doing either up or down over time. You can also on what rate we are automating the test cases.
Reference: Brian Keller talk on Visual Studio launch

Visual Studio 2012 Testing Features for Developers


Developer Testing in Visual Studio 2012 has been improved a lot and it is allowing them to concentrate more on the code they are writing in the application. In addition to the testing framework that comes with Visual Studio 2012, you can easily add third-party testing frameworks in the IDE. This post discusses the new unit and integration testing features for developer in Visual Studio 2012. image
To open the new Test Explorer window, Select the Test Menu then choose windows and click Test Explorer
image
The look and fell of Test Explorer in VS 2012 is different in number of ways. You do not have too many different windows to say same information in new IDE, instead you have one widow that focus on unit and integration testing results.
image
When you select a test case in Test Explorer then you can see the detailed information about the test in below pane. The green bar in test explorer gives you quick view on the out-come.
Integrating Third Party Testing Frameworks
One of the important feature for developers for testing in this release is, it enables you to quickly integrate third-party testing frame-works in VS 2012. So If your team uses N-Unit or XUnit.net or some other testing framework then you can simply install Visual Studio extension.
To open the Extension tools manager, Go to Tools and click Extensions and Updates
image
You can search the framework that you need and download as shown below
image
In Visual Studio 2012 Regard less of which Testing Framework you use, it is fast and scales  up to thousands of test cases.
Developer testing is also very easy to use, If you want to Run or Debug the test that is in your editor then you can right-click on the code and select either run tests or debug tests.
image
You can also Filter the test cases by entering the search term in Test Explorer. You can also group tests with different properties, You can group the tests either by Test Outcome or Duration.
image
If you often require to run unit and integration tests in your solution then with single click in Visual Studio 2012 you can accomplish with Run test after build feature.
image
Now your test will run as part of every build you do.
With a single-click you can analyze and understand various code-metrics about your solution.
image
The Code Metrics Results window will appear as below information like number of line of code , Class Cohesion and Depth of inheritance
image
As it in previous visual studio versions, you can use Code Analysis Tool to quickly find common coding and design mistakes in your code
image
Another new feature in Visual Studio 2012 is Code Clone Analysis, using this feature you can scan your entire solution for duplicate code
image
Code Clone Analysis is smart enough to identify the duplicate code and groups the results with Exact match and medium match.
You can also use code clone feature that matches selected code in editor as shown below
image
Code Coverage Analysis
Code Coverage Analysis tool helps you to know how well your tests exercise your code? Code Coverage Analysis has significantly improved in Visual Studio 2012. To Analyze the code coverage of all the tests in your solution simply click Run drop down in Test Explorer
image
You can browse and explore the Code Coverage Results after completing the run
image
Unit test cases can also run in isolated mode with fakes framework, more information on this can be read here 
Reference: Peter Provost’s talk on Visual Studio launch

Wednesday, September 19, 2012

ASP.NET Web Forms 4.5 new features in Visual Studio 2012


This post discusses about ASP.NET Web Forms 4.5 features, Web Forms in 4.5 allows you to build dynamic web sites quickly and easily. Web Forms generates much cleaner code on client-side with Unobtrusive Validation in this version. You can also build data-centric applications easily with data-binding features. image
Typical Web Form which contains more fields and validation controls can generate more code on client-side. When you run this typical form in browser then you can see the page size as below
image
The reason for this size is because client-side validation is enabled. If you change Unobtrusive validation mode in page load to web forms then you can see the difference.
image
Now re-compile your application and run the page the result is as shown below, Now page size is much smaller than before
image
Strongly typed Data Controls
Take a typical Search Form which shows the results in list view. Inside list view you have item template which is having multiple calls to the EVAL expression as shown below
image
The above method is the standard way of doing data-binding in web forms. You optimize the above using strongly typed data controls.
You can set the Item Type property of List View to the type that you actually data-binding to as shown below
image
Now you can member variables in place of Eval expressions, member variables are now typed and you will get an Intel license on item class.
image 
Model Binding
You may be familiar with Model Binding in ASP.NET MVC, The typical code which you might write in web forms to bind the results in web forms as below
image
The above code talking to the data base by building a query which does a case insensitive search then it coverts into list binds the results to list view. Let us re-write the above code using Model-Binding
image
Now there is no page load and click-handler, The above code is not directly interacting with the page. You can populate the formTerm and qsTerm variables using attributes Control and QueryString in model-binding.
The return result of GetResults method is IQueryable. Because the result is of type IQueryable the control has the ability to modify this before it is rendering it to the page. You can use this in sorting and paging. You can specify the same in markup.
Advantage of using Model Binding As the code is not directly interacting with the page, you can fairly easily unit test the method or even move it to the completely different class.
Support for OpenID in OAuth Logins
image
The AuthConfig.cs file is standard in all new projects that created in ASP.NET 4.5 in Visual Studio 2012. You can see there are number of external services code is commented out and it is ready to use by putting your custom application credentials.
Now you can use external services to login to the application.
image
These are the features which you can try out in Visual Studio 2012.
Reference: Damian Edwards talk in Visual Studio launch.

Agile Development with Visual Studio 2012



Developers want to be more agile and want to cope up with interruptions. This post helps the developers to collaborate with teams using Visual Studio 2012 and focus on what they do as developers.
image
Assume you are in the middle of implementing some requirement or fixing the bug, something else is come may be it is high priority bug or requirement which your manager want you to work on first, I am sure this is common for the developers!
Suspend and Resume work with Visual Studio 2012 
There is a new area of Team Explorer in Visual Studio 2012 called My Work
image
one of the options under My Work  is Suspend & Shelve. When you click Suspend & Shelve it backup all your changes into Team Foundation Server and it also save entire visual studio environment.
image
When you click Resume button then it un-shelve the changes from Team-Foundation server and open up all the windows exactly if where they were before, if you look at the option View Changes in Team Explorer it shows you all pending check in files
image
You can get these pending files in solution explorer using filter option as shown below
image
When you click the Pending Changes Filter then it shows only the files that needs to be checked-in in solution explorer.
Even if you are not sure a particular feature existed in visual studio or not  then you can type the word in Quick Launch bar in Visual Studio IDE then it launches the options windows if it exists in the product.
image
Searching Work Items within Visual Studio
image
Now you can quickly search work items within Visual Studio from Team Explorer window.
As an Agile Developer you may want to improve your code and maintainability , You can then use Analyze menu in Visual Studio and click Analyze Solution for Code Clones
image
What it does is it look-up your entire solution for any blocks of code which look to be similar
image
You can open those files from result window compare and re-factor the code. If you want to know where exactly these files or piece of code in visual studio solution hierarchy then open the file and click sync button in solution explorer then it high-lights the file.
image
You can also do this more granular level by selecting a piece of code in a file and say find the clone. Select the code and right-click on it and then say Find Matching clones in solution
image
what it does is , it looks at your entire solution for anything that similar to the selected lines-of code
image
The results under Medium March category are not necessarily be identical but they may be similar in type of object or operation that you are performing.
I hope the features will definitely helps developers to be more agile in day-to-day coding. More about Visual Studio 2012 IDE features can be read here
Reference: from Brian Keller Visual Studio launch talk

Find a cool free stuff everyday

Giveaway of the Day

Hiren Bharadwa's Posts

DotNetJalps