Earn Money online as a Photographer

Monday, May 27, 2013

New Features in Microsoft .NET Framework 4

A lot of new features and enhancements have been introduced in .NET Framework 4. The most important of them include the following:

  • The Dynamic Language Runtime
  • Managed Extensibility Framework
  • Support for Parallel Computing
  • ADO.NET Improvements
  • WCF Improvements
  • WWF Improvements

The Dynamic Language Runtime (DLR)

The Dynamic Language Runtime (DLR) is built on top of the Common Language Runtime (CLR) and opens the door for a common set of technologies and tools to interoperate inside a common framework.  The Dynamic Language Runtime environment enables the developers to implement applications using dynamic languages like Python and Ruby on top of the .NET Framework runtime. It is a runtime environment built on top of the managed environment of the CLR that provides support for implementing dynamic features in statically typed languages, sharing of libraries and objects and fast dynamic dispatch and invocation.

Dynamically typed languages such as Python, Ruby, and JavaScript can reside alongside familiar statically typed .NET languages such as C#, Managed C++, and Visual Basic. The CLR provides a common platform where statically typed languages (e.g. C#, VB) can reside and interoperate, while the DLR sits on top of the CLR and provides a common platform for dynamically typed languages to reside and interoperate.
The Wikipedia states: "The Dynamic Language Runtime (DLR) from Microsoft is an ongoing effort to bring a set of services that run on top of the Common Language Runtime (CLR) and provides language services for several different dynamic languages." Reference: http://en.wikipedia.org/wiki/Dynamic_Language_Runtime
The DLR adds a set of services that facilitate dynamic language implementations on .NET's managed platform. These services include support for a dynamic type system, a standard hosting model, and fast dynamic code generation. In addition, the DLR lets dynamic and static languages interoperate bi-directionally.
The new System.Dynamic namespace in Microsoft .NET Framework 4.0 provides support for the Dynamic Language Runtime environment. The MSDN states: "The dynamic language runtime (DLR) is a runtime environment that adds a set of services for dynamic languages to the common language runtime (CLR). The DLR makes it easier to develop dynamic languages to run on the .NET Framework and to add dynamic features to statically typed languages." Reference:http://msdn.microsoft.com/en-us/library/dd233052.aspx
The Dynamic Language Runtime is comprised of the following three layers:
1.       The .NET Language Integration Layer
2.       The Dynamic Language Runtime Code Components
3.       Language Binders
The Dynamic Language Runtime environment provides the following services:
  • Dynamic Method Dispatch
  • Dynamic Code Generation
  • Hosting API

Managed Extensibility Framework

The Managed Extensibility Framework is a new framework that can be used to implement lightweight, extensible and composable applications that can leverage the features and benefits of the CLR. You can use the Managed Extensibility Framework (MEF) to enable extensions to be reused within the same application or across applications. You can know more on the Managed Extensibility Framework from this MSDN article: http://msdn.microsoft.com/en-us/library/dd460648.aspx

Support for Parallel Computing

Microsoft .NET Framework 4 provides support for a parallel computing model - you can now write multithreaded applications and asynchronous code with much power and simplicity. You now have the Parallel Extensions library (previously known as Parallel Framework Extensions PFX, a managed concurrency library for designing and implementing applications that can take advantage of parallel hardware in your system. The Parallel Extensions library comprises of a task parallelism component called Task Parallel Library and Parallel LINQ. Parallel LINQ is a concurrency execution engine that is used to execute LINQ queries on multicore processors.

ADO.NET Improvements

With .NET Framework 4, you have support for implementing model first development and persistent ignorant objects. These are also known as Plain Old CLR Objects (POCO) objects. These POCO entities or persistent ignorant objects enable you to design classes removes the need of having persistence specific concerns in your entity classes. You now also have support for using functions in your LINQ queries. You can know more on ADO.NET improvements in .NET 4 from this link: http://msdn.microsoft.com/en-us/library/ex6y04yf.aspx

Improvements in Windows Communication Foundation

Windows Communication Foundation (WCF) is a platform that can be used to design and implement platform independent, scalable services. It is a framework that provides a platform for unification of a number of enterprise technologies under one roof. Windows Communication Foundation (WCF) was first introduced as part of .NET Framework 3.0.
The improvements in WCF in .NET Framework 4 are:
  • Support for Simplified configuration
  • Support for Standard endpoints
  • Support for Simplified IIS Hosting
  • Support for enhanced REST features
  • Support for Workflow services
  • Support for Routing service

Improvements in Windows Workflow Foundation

Windows Workflow Foundation provides you a simplified framework for design and development of workflow based applications using .NET Framework. Windows Workflow Foundation was also first introduced as part of Microsoft .NET Framework 3.0. The improvements in Windows Workflow Foundation in .NET Framework 4 include the following:
  • Improved extensibility in the WF Designer
  • Better hosting, persistence and tracking features
  • Improved workflow activity model
  • Improved built-in activity library

Monday, December 21, 2009

.NET Framework 3.5 Features

Visual Studio will now support targeting multiple versions of the .NET Framework, and we will be able to start taking advantage of the new features Visual Studio provides without having to always upgrade their existing projects and deployed applications to use a new version of the .NET Framework library. Using VS 2008, we can create in any of the .Net Framework Versions 2.0, 3.0, or 3.5. The VS 2008 multi-targeting support only works with .NET 2.0, .NET 3.0 and .NET 3.5.

Web Designer and CSS Support
VS 2008 includes a significantly improved HTML web designer. This delivers support for split-view editing, nested master pages, and great CSS integration.
Split View Editing: VS 2008 adds support for a new "split-view" mode when working on pages. This allows us to see both the HTML source and the Design View at the same-time, and easily have any changes we make in one view been updated in the other.
CSS Style Manager: VS 2008 supports a new tool window inside the IDE called "Manage Styles". This shows all of the CSS style sheets, and their corresponding rules, for the page we are currently editing. It can be used both when we are in design-view, as well as when in source view on a page.
CSS Properties Window: One of the other cool new CSS features that is also supported in both design and source view is the new CSS Properties Window. When we select an HTML element or ASP.NET server control, the CSS property window will show us all of the CSS settings currently applied to it. We can also change any of the values using the CSS property grid.
CSS Source View Intellisense: The HTML designer supports the ability to select an element or control in design-view, and graphically select a rule from the CSS list to apply to it.

Nested Master Page Support

One of the feature is the ability to have "nested master pages" - where we can create a root master page for a site that defines a site's overall layout, and then create nested master pages that are based on the root master and further customize the layout. This nested master page feature is extremely flexible, and enables developers and designers to quickly and very cleanly make changes to the layout and organization of a site with minimal code changes and no content duplication. VS 2008 fully supports nested master pages, and makes using them super easy.

ASP.NET AJAX and JavaScript Support

.NET 3.5 has ASP.NET AJAX built-in (and adds new features like UpdatePanel support with WebParts, WCF support for JSON (JavaScript Object Notation), and a number of bug fixes and performance improvements). VS 2008 also has great support for integrating JavaScript and AJAX into the applications.

JavaScript Intellisense

One of the features that we will really like with VS 2008 is its built-in support for JavaScript intellisense. This is enabled in both the free Visual Web Developer 2008 Express edition as well as in Visual Studio, and makes using JavaScript and building AJAX applications significantly easier.

Intellisense for External JavaScript Libraries

VS 2008 supports intellisense not just for in-line script, but also for externally referenced JavaScript files.

Adding Intellisense Hints to JavaScript

function btnClick()
    /// <summary>This method show confirmation message and returns
true/false value. </summary>
    /// <returns>string</returns>
    var conf = confirm("Do you want to click more?");
    return conf;

Intellisense within External JavaScript files
One of the interesting characteristics about external JavaScript files is that they can call and use the JavaScript functions and variables declared within other JavaScript files that a page loads

Calling Web Services using ASP.NET AJAX

function callWebService()

JavaScript Debugging in ASP.NET pages
One of the annoying things with VS 2005 is that we have to first run our ASP.NET pages before we can set JavaScript breakpoints in them in the debugger. VS 2008 makes this much better by adding new support that allows us to set client-side JavaScript breakpoints directly within our server-side .aspx and .master source files.

Automatic Properties
Automatic properties allow us to avoid having to manually declare a private field and write the get/set logic -- instead the compiler can automate creating the private field and the default get/set operations for us.

public class Person {
        public string FirstName { get; set; }
        public string LastName  { get; set; }        
        public int    Age       { get; set; }

When the C# "Orcas" compiler encounters an empty get/set property implementation like above, it will now automatically generate a private field within class, and implement a public getter and setter property implementation to it. This means that -- unlike public fields -- we can in the future add validation logic within our property setter implementation without having to change any external component that references our class.

Object Initializers
In Previous versions, we write:

Person person = new Person();
person.FirstName = "Gyan";
person.LastName = "Singh";

Now we can write the above code as:

Person person = new Person { FirstName="Gyan", LastName="Singh" };

The compiler will then automatically generate the appropriate property setter code that preserves the same semantic meaning as the previous.

Collection Initializers
With previous version wr write as:

List<Person> people = new List<Person>();
people.Add( new Person { FirstName = "Anoop", LastName = "jain} );
people.Add( new Person { FirstName = "Chandra", LastName = "Prakash" } );
people.Add( new Person { FirstName = "Gyan", LastName = "Singh" } );
Now we can write above code as:
List<Person> people = new List<Person>(){
      new Person { FirstName = "Anoop", LastName = "Jain" },
      new Person { FirstName = "Chandra", LastName = "Prakash" },
      new Person { FirstName = "Gyan", LastName = "Singh" }

When the compiler encounters the above syntax, it will automatically generate the collection insert code like the previous sample for us.

Extension Methods
Extension methods allow us to add new methods to the public contract of an existing CLR type, without having to sub-class it or recompile the original type. Extension Methods enable a variety of useful scenarios, and help make possible the really powerful LINQ query framework that is being introduced with .NET 3.5. Define Extention Methods as:

public static class ExtensionMethodTest
    public static bool IsValidEmailAddress(this string s)
        Regex regex = new Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");
        return regex.IsMatch(s);
And now we can call it as:
string email = Request.QueryString["email"];
if ( EmailValidator.IsValid(email) ) 

LINQ To In-Memory Objects
//Create a list of person

System.Collections.Generic.List<Person> person = new System.Collections
.Generic.List<Person> { new Person { FirstName = "A", LastName = "X",
Age = 15 }, new Person { FirstName = "B", LastName = "Y", Age = 17 },
new Person { FirstName = "C", LastName = "Z", Age = 24 } }; //Retrieve person whose name starts with "A", display count and FirstName
in textbox IEnumerable<Person> queryResult; queryResult = person.Where(p => p.FirstName.StartsWith("A")); txtPerson.Text = "Total Count: " + queryResult.Count().ToString() +
Environment.NewLine + "1. " + queryResult.First().FirstName + ", 2. "
+ queryResult.Last().FirstName;

<?xml version="1.0" encoding="utf-8" ?> 
       <person age="15">
       <person age="17">
       <person age="24">
//Using LINQ Extension Methods against an XML File
XDocument people = XDocument.Load(@"C:\Desktop\Venetian\LINQTEST.xml");
//Casting to XElement
System.Collections.Generic.IEnumerable<XElement> xmlResult;
xmlResult = people.Descendants("person").Where
(p=>p.Element("firstname").Value.StartsWith("A")); txtPerson.Text += Environment.NewLine + "XML File" + Environment.NewLine
+ "2.Total Count: " + xmlResult.Count().ToString() + Environment.NewLine
+ "Seq 1. "
+ xmlResult.First().FirstNode + ", Seq 2. " + xmlResult.Last().FirstNode

//LINQ TO SQL Example

LinqToSqlExampleDataContext dbTask = new LinqToSqlExampleDataContext();
var query = from p in dbTask.UserTasks
            where p.EmployeeID == 5263
            select new {p.EmployeeID,p.Task.Tsk_Message,p.UT_ViewDateTime
,p.Task.Tsk_TypeCode,p.Task.Tsk_NavigationURL}; gridView.DataSource = query; gridView.DataBind();

Lambda Expressions
Lambda Expressions provide a more concise, functional syntax for writing anonymous methods. They are super useful when writing LINQ query expressions - since they provide a very compact and type-safe way to write functions that can be passed as arguments for subsequent evaluation. The p =&gt; expressions in above examples is Lambda expression.
In C# a lambda expression is syntactically written as a parameter list, followed by a =&gt; token, and then followed by the expression or statement block to execute when the expression is invoked:
params =&gt; expression
So when we wrote the lambda expression: p =&gt; p.FirstName == "AAA"
We were indicating that the Lambda we were defining took a parameter "p", and that the expression of code to run returns whether the p.FirstName value equals "AAA". The fact that we named the parameter "p" is irrelevant - we could just have easily named it "o", "x", "foo" or any other name we wanted. Lambda parameter types can be inferred at both compile-time and by the Visual Studio's intellisense engine (meaning we get full intellisense and compile-time checking when writing lambdas).

Query Syntax
Query syntax is a convenient declarative shorthand for expressing queries using the standard LINQ query operators. It offers a syntax that increases the readability and clarity of expressing queries in code, and can be easy to read and write correctly. Visual Studio provides complete intellisense and compile-time checking support for query syntax. Under the covers the C# and VB compilers take query syntax expressions and translate them into explicit method invocation code that utilizes the new Extension Method and Lambda Expression language features in "Orcas". We can use query syntax to perform a LINQ query over the collection and fetch only those people whose LastName starts with the letter "Y", sorted by the people's FirstName (in ascending order):
//query syntax
objResult = from p in person
where p.LastName.StartsWith("Y")
orderby p.FirstName
select p;
The query syntax expression above is semantically equivalent to the below code that uses LINQ extension methods and lambda expressions explicitly:
objResult = person.Where(p =&gt; p.FirstName.StartsWith("A")).OrderBy(p =&gt; p.FirstName);
The benefit with using the query syntax approach is that it ends up being a little easier to read and write. This is especially true as the expression gets richer and more descriptive.
Every syntactic query expression in C# begins with a "from" clause and ends with either a "select" or "group" clause. The "from" clause indicates what data we want to query. The "select" clause indicates what data we want returned, and what shape it should be in.

Anonymous Types
Anonymous types are a convenient language feature of C# and VB that enable developers to concisely define inline CLR types within code, without having to explicitly define a formal class declaration of the type. Anonymous types are particularly useful when querying and transforming/projecting/shaping data with LINQ.