What's new in Visual Studio.NET 2008.

What's new in C# 3.0

  1. Implicit typed local variables

    Local variables can be declared as type var, whose actual type of the variable is determined by the compiler based on the data schema (see Listing 1). It's mainly used to store anonymous types in LINQ.
     
           

            // This is an integer

            var nId = 1234567;

            //This is a string

            var strFullname = "John Charles Olamendy Turruellas";

    Listing 1
     

  2. Automatic properties

    Sometimes when you are implementing the logic associated with classes, you need to write the getters/setters of our properties. Properties are best practices to support the encapsulation concept in the Object-Oriented Programming paradigm. C# compiler provides an elegant way to make your code more robust using properties called automatic properties. It allows you to avoid having to manually declare a private field and write the underlying getter/setter logic. The compiler does the work for you (see Listing 2).
       

    Public Class Customer

        {

            public string Fullname

            { get; set; }

            public string ContactAddress

            { get; set; }

        }

    Listing 2
     

  3. Object Initializers
    When you create an instance of the business entity Customer, you write the code to call the class constructor and set the properties of the instance (Listing 3).
     

    Customer objCustomer = new Customer();

    objCustomer.Fullname = "John";

    objCustomer.ContactAddress = "My address";

    Listing 3

    Object Initializers allow you to re-write the previous code in a concise way (see Listing 4). 

     

    Customer objCustomer = new Customer { Fullname="John", ContactAddress="My address"};  

    Listing 4
     

  4. Collection Initializers

    Object Initializers is a great feature to write code efficiently. C# also adds a feature to initialize collections of objects called Collection Initializers following the same concepts of Object Initializers (see Listing 5).
               

    List<Customer> objCustomerList = new List<Customer>

                {

                    new Customer{Fullname="John Olamendy", ContactAddress="Address1"},

                    new Customer{Fullname="Mary Perez", ContactAddress="Address2"},

                    new Customer{Fullname="Anthony Frances", ContactAddress="Address3"}

                };

    Listing 5
     

  5. Extension methods allow developers to add new methods to existing CLR types without subclassing and recompiling the original class. You can realize by defining a static class with a static method containing the extension method. You need to use the keyword this before the CLR type you want to extend. To use the new defined extension, you need to use the import keyword to import the namespace containing the extension class.

    Let's define an extension method to check whether a given string object is empty or not (see Listing 6).

     

    public static class CheckString

    {

          public static bool MyExtensionMethod_IsEmpty(this string value)

          {

                bool bResult = true;

     

                if (value != null && !value.Equals(""))

                {

                    bResult = false;

                }

     

                return bResult;

          }

    }

    Listing 6

    Now let's use the extension method in a scenario (see Listing 7). 

    string strCustomerName = "John C. Olamendy";

    if (strCustomerName.MyExtensionMethod_IsEmpty())

    {

        System.Console.WriteLine("The string is empty");

    }

    else

    {

        System.Console.WriteLine("The string is is not empty");

    } 

    Listing 7

    One common built-in extension shipped in Microsoft.NET is inside the namespace System.Linq and allows easy querying of the data by using Linq query operators. Let's suppose we want to query the list of customer already defined  (objCustomerList) in order to get customers whose names begins with the letter J. You need to use the Where extension method provided by System.Linq to retrieve the result set (see Listing 8). 

    IEnumerable<Customer> rsCustomer = objCustomerList.Where(p => p.Fullname.StartsWith("J"));

    Listing 8

    The statement for the query is written using the principles of "Lambda expression", which is a natural evolution of anonymous methods.
     

  6. Anonymous Type is a way to encapsulate properties of an object without defining the underlying class at runtime. The type name and properties are inferred by the compiler (Listing 9).
     

    var objProduct = new { ProductID=1, Name="My Product", UnitPrice=10.2};

    System.Console.WriteLine("The product info is: Id={0}, Name={1}, UnitPrice={2}", objProduct.ProductID, objProduct.Name, objProduct.UnitPrice);

     
    Listing 9

    One common scenario where we can use Anonymous Type is when we want to query and work with data using LINQ expressions, and we want to define the schema of the result set in-line. Let's suppose that we want to query a database in order to search for products that satisfy some conditions such as UnitPrice great than a value in order to have an insight of expensive products (see Listing 10). This syntax allows using the dynamic language flexibility without losing the strong-type language support for compile-time checking and intellisense in Visual Studio.
     

    var rsProduct = from p in db.Products

                    where p.UnitPrice > 10000.00

                    select new

                    {

                        Id = p.ProductID,

                        Name = p.Name,

                        Price = p.UnitPrice

                    };

     

    foreach (var objProduct in rsProduct)

    {

        System.Console.WriteLine("Expensive products. ID={0}, Name={1}, Price={2}", objProduct.Id, objProduct.Name, objProduct.Price); 

    }

    Listing 10

     

  7. Lambda expression is an evolution of delegates in order to write in-line anonymous blocks in C#. It's very helpful when writing Linq query expressions by defining wrapping methods that can be passed as arguments to the evaluations. As illustrative purposes, we're going to query the list of customer (objCustomerList) defined before. This collection has new extension methods such as Where and Average which can be used along with Lambda expressions to query the array of entities.

    Let's suppose that we want to retrieve information for a particular customer; then we write the C# code along with Lambda expression as a filter as shown (see Listing 11).
     

    IEnumerable<Customer> rsCustomer = objCustomerList.Where(p => p.Fullname == "John Olamendy");

    Listing 11

    This expression is semantically equivalent to anonymous methods following the rule params=>expression. That is, the p is parameter of the anonymous method referencing to every instance of Customer in the collection. In this case, we don't need to declare the parameter type because it's inferred by the compiler from the types in the collections. The expression is normal C# statement that, in this case, returns a Boolean value. Let's re-write the previous lambda expression into an anonymous method (see Listing 12).
     

    IEnumerable<Customer> rsCustomer = objCustomerList.Where

    (

       delegate(Customer objTemp)

       {

         return objTemp.Fullname == "John Olamendy";

       }

    ); 

    Listing 12

    I would like to illustrate the evolution from delegates (in early C# language), through anonymous methods (in C# 2.0) to lambda expression (in C# 3.0) using an example taking from the Visual Stduio 2008 documentation (see Listing 13).

     
    class Test
    {

        delegate void TestDelegate(string s);

        static void M(string s)

        {

            Console.WriteLine(s);

        }

     

        static void Main(string[] args)

        {

            // Original delegate syntax required

            // initialization with a named method.

            TestDelegate testdelA = new TestDelegate(M);

     

            // C# 2.0: A delegate can be initialized with

            // inline code, called an "anonymous method." This

            // method takes a string as an input parameter.

            TestDelegate testDelB = delegate(string s) { Console.WriteLine(s); };

     

            // C# 3.0. A delegate can be initialized with

            // a lambda expression. The lambda also takes a string

            // as an input parameter (x). The type of x is inferred by the compiler.

            TestDelegate testDelC = (x) => { Console.WriteLine(x); };

     

            // Invoke the delegates.

            testdelA("Hello. My name is M and I write lines.");

            testDelB("That's nothing. I'm anonymous and ");

            testDelC("I'm a famous author.");

     

            // Keep console window open in debug mode.

            Console.WriteLine("Press any key to exit.");

            Console.ReadKey();

        }

    }

    /* Output:

        Hello. My name is M and I write lines.

        That's nothing. I'm anonymous and

        I'm a famous author.

        Press any key to exit.

     */ 

    Listing 13

What's new in Web applications

  1. Rich Web designer

    Split View Support in order to show both the HTML/ASP.NET code and the graphical representation like Micromedia Dreamweaver (see Figure 1).

    1.gif

    Figure 1

    Rich support to CSS including a CSS, CSS Manager and Preview panes that allows working with CSS styles easier  than ever. The Manage Styles pane allows easily creating and managing styles. In the following figure, you can see a new created CSS style named firstStyle applied to the previous figure. In the bottom of the pane, you can visualize a preview of the style (see Figure 2).   

           2.gif
      
            Figure 2

    If you click on the CSS Properties tab, you can see the rules associated to the
    style (actual values, inherited values and  overridden values) (see Figure 3).


            3.gif


                Figure 3

  2. ASP.NET Ajax and JavaScript

    This new release of Microsoft.NET and Visual Studio include framework support, libraries, Intellisense and debug to develop Ajax and JavaScript applications. In the Figure 4, you can see the JavaScript Intellisense support in Visual Studio 2008. 

    Intellisense enhances the ASP.NET Ajax concepts such as classes, properties and enums. The new Visual Studio.NET 2008 support to Ajax principles(asynchronous communication and partial-page updates) includes server controls such as ScriptManager, UpdatePanel, UpdateProgress and Timer controls, Ajax library to write client-based applications based on the  object-oriented paradigm and enabling to develop custom components, server classes to develop server controls that renders Ajax components and access to Web services, ASP.NET authentication, roles management and profile application services from Web applications.

          4.gif

            Figure 4

    You can use a set of extenders that are part of the Ajax Control Toolkit to enhance the existing ASP.NET Web controls with  Ajax features (see Listing 14).

          <
    asp:scriptmanager id="ScriptManager1" runat="server"></asp:scriptmanager>
          <
    asp:textbox id="TextBox1" runat="server"></asp:textbox>
         
    <cc1:autocompleteextender id="AutoCompleteExtender1" runat="server" targetcontrolid="TextBox1"      
          servicepath
    ="MyWebService.asmx" servicemethod="GetExtenderList" minimumprefixlength="1">
         
    </cc1:autocompleteextender>

           Listing 14
  3. Web application Project

    The aspnet_merge utility allows compiling multiple assemblies into a single assembly for production use as an alternative way of deployment.

  4. Multi-targeting Web applications

    Applications can target NET 2.0, 3.0 or 3.5.
  5. New controls such as LiveView control and LinqDataSource as common interface between Linq and other ASP.NET data sources.

What's new in enterprise application development

  1. Linq is Microsoft.NET framework extension that adds native query capabilities to .NET languages using syntaxes similar to SQL. The data source is read and encapsulated as objects as well as the underlying queries are executed over collection of entities and the result set is also a collection of in-memory objects that can be enumerated. It defines a set of operators that can be used to query, project and filter data in arrays, enumerable-based classes, XML, relational database and other third-party data sources.

    Let's illustrate Linq concepts with some examples. Suppose you need to retrieve information about a specific customer (John Olamendy). The Linq statement is shown bellow (see Listing 15).

    var query = from objCustomer in objCustomerList
                where objCustomer.Fullname == "John Olamendy"
                select objCustomer;
    IEnumerable<Customer> rsCustomer = query.ToList<Customer>(); 

    Listing 15

    Let's define a projection using Linq as shown bellow (see Listing 16).

    var query = from objCustomer in objCustomerList
                where objCustomer.Fullname == "John Olamendy"
                select new
                {objCustomer.Fullname};

    Listing 16

    Visual Studio also has an O/R Designer (Object/Relational Designer) for Linq to SQL projects which assist developers to map objects in your solution domain to relational database objects such as tables, views, stored procedures and functions. This designer provides a visual design surface to create instance of DataContext (metadata about the database systems and the underlying connection) and entity classes. It's remarkable to say that up to this version of O/R Designer, we can only map 1:1 relationships, that is, an entity class is mapped to only one table in the relational schema. You cannot map complex mapping such as an entity class that is mapped to a join of table or several tables in the relational schema.

    You can invoke the O/R Designer by adding a new Linq to SQL classes item to the project. Basically, you can create entity classes to map objects in the relational schema by dragging and dropping objects in database systems onto the O/R Designer surface (see Figure 5).

    5.gif

    Figure 5

  2. Introduction of the concepts of occasionally connected application which is a client application that uses data from a database system but it's not always connected. You can access to your local database cache on the client and then periodically the local data is synchronized with the remote database system on the server. To add this feature to your solution, open Add New Item dialog box and select the Local Database Cache icon from the Templates listbox (see Figure 6) which opens a Configure Data Synchronization designer for creating a local SQL Server 2005 Compact Edition (SSCE) database and a synchronization file (.sync).

           6.gif

              Figure 6

  3. Project designer support to develop WPF applications when you can select the WPF Application, WPF Browser application, WPF User Control Library, WPF Custom.

  4. Visual Studio Tools for Office (VSTO) was an add-in in early version of Visual Studio 2005. If you have installed Office  2003 or 2007, then the setup of Visual Studio 2008 installs VSTO 2008. You can see different Office Project types in the  
    Template listbox on the New Project dialog box (see Figure 7).


             7.gif

                Figure 7

    You can create applications for several business scenarios such as customize Word and Excel documents, create sequential or state machine workflows for SharePoint, customize the Office 2007 Ribbon, extend form and create custom task panes.

  5. WCF integrated features that supply stubs for required interfaces and classes as well as generate default app.config files for services that implement WS-* stack of protocols. After you select a WCF Project Type and WCF Service Library template, then the underlying code and configuration files are autogenerated by decorating the public interface with the ServiceContract attribute and the messages and
    underlying data representing the payload with DataContract, DataMember  attributes (Figure 8).

            8.gif

               Figure 8

  6. You can develop report-based applications using Wizards. Visual Studio 2008 includes two new project templates for creating reporting applications. Now you can see the Reporting templates in the New Project dialog box (see Figure 9).

           9.gif

            Figure 9

    Then a Report Wizard dialog box that drives you through the process of creating a Report Template for you (see Figure 10).

           10.gif

             Figure 10

  7.  Test-driven approach

    Visual Studio 2008 Professional now includes an automated unit testing environment to generate a test suite (test stub for each class and method, parameter values) for projects in order to instantiate the classes and call for the underlying methods. You can create a test project by right-clicking on Test | New Test and selecting Unit Test from Templates on the Add New Test windows (see Figure 11). Finally, the output is evaluated through assertions. When you follow the test-driven approach, a unit test project is created and added to the project to be tested. Then, the classes and methods, you want to test, are selected. Next, press F5 to build and run the tests and display the results.

           11.gif

             Figure 11

  8. In Visual Studio 2008, you can also see a new menu in the main menu named Analyze menu. There are mainly three commands: Run Code Analysis, Calculate Code Metrics and Profiling. The Code Analysis menu provides a report with the main changes to do in order to find errors and comply with best practices of the technology. The Code Metrics menu helps users find and act upon complex area of the application through metrics that are applied to your code. Let's see an example of running Code Metrics over our examples. In this report, we have some metrics such as Class Coupling, Depth in Inheritance, Cyclomatic Complexity, Lines of Code, and Maintainability Index (see Figure 12).

    Class Coupling indicates the number of dependencies between entities of your problem domain. Lower number indicates candidates for possible reuse.

    Depth in Inheritance indicates the number of classes that are above in the inheritance tree from a particular class.

    Cyclomatic Complexity indicates the total number of individual path through the code. It's calculated by counting the number of decision points (if, switch, do, while, foreach, for) and adding 1.

    Lines of Code indicate the total number of lines of code excluding white space, comments, braces, declaration of members, types and namespaces.

    Maintainability Index is an index from 0 to 100 indicating the overall maintainability of a member or a type (at assembly level,it's the total number of maintainability index of all types within it). It's calculated from other metrics such as Halstead Volume (which factors in the number and use of operands and operators), Cyclomatic Complexity and Lines of Code. Lower values indicate complex and hard to maintain. The report of this field also indicates the status with an icon.

          2.gif

            Figure 12

Conclusion

In this article, we covered the new features of Visual Studio 2008 showing several business scenarios where we can use them effectively in order to develop better solutions and improve the productivity.


Similar Articles