Monday, 21 October 2013

ASP.NET 4.5 ScriptManager Improvements in WebForms

The ScriptManger control has undergone some key changes in ASP.NET 4.5 which makes it easier to register, manage and combine scripts using the ASP.NET weboptimizationfeature.

ASP.NET Web optimization framework

The ASP.NET Web optimization framework provides services to improve the performance of your ASP.NET Web applications.

Current services provided by the framework include:

  • bundling - combining multiple scripts or style resources together into a single resource, and thereby requiring browsers to make fewer HTTP requests
  • minification - making scripts or styles smaller using techniques such variable name shortening, white space elimination, etc.

Easy Integration with JQuery and JQueryUI

The default templates for WebForms ship with the following packages “AspNet.ScriptManager.jQuery” and “AspNet.ScriptManager.jQuery.UI.Combined”. These packages make it easier to bring in jquery and jqueryUI libraries and also register them with the ScriptManager. Here is how it works.

<asp:ScriptManager runat="server">
        <Scripts>
            <asp:ScriptReference Name="jquery" />
            <asp:ScriptReference Name="jquery.ui.combined" />            
        </Scripts>
    </asp:ScriptManager>
Read more>>

ASP.NET Web optimization

Friday, 18 October 2013

Learn ASP.NET MVC

If you are new to ASP.NET MVC and looking for some articles which will give you a clear understanding of the ASP.NET MVC Framework here is an interesting list.

From ScottGu's blog @ weblogs.asp.net

ASP.NET MVC Framework (Part1)

URL Routing (Part2)

Passing ViewData from Controllers to Views (Part3)

Handling Form Edit and Post Scenarios (Part4)

Here is another set of articles from www.codeproject.com

Why MVC and What is MVC?, HTML Helper classes

Writing unit tests on MVC projects, Configure MVC routing, Validating MVC routes, Configure MVC outbound routes

Partial Views, Validation using data annotations, Razor, Authentication - Windows, Forms

JSON, jQuery, State Management

Introduction to ASP.NET Web Programming Using the Razor Syntax (C#)

LINQ Sample - Group Products by Category

Following example orders List<Product> by Product.Category and saves the grouped result into a Dictionary<string, List<Product>>.

 public class Product
{
   public string Name { get; set; }
   public string Category { get; set; }
}

  List<Product> products = new List<Product>{
                new Product{ Name = "Beans", Category = "Veg" },
                new Product{ Name = "Apple", Category = "Fruit" },
                new Product{ Name = "Tomato", Category = "Veg" },
                new Product{ Name = "Orange", Category = "Fruit" },
            };

Dictionary<string, List<Product>> productCategories =
                (from p in products
                group p by p.Category into g
                select new { Category = g.Key, Products = g }
                )
.ToDictionary(categories => categories.Category, categories =>
categories.Products.ToList<Product>());    

Now you can order the result by Category name, that is the Key of Dictionary

var productCategoriesOrderd = 
productCategories.OrderBy(productCategory => 
productCategory.Key);

Generic Delegates in C# 3.0

With the introduction of generic delegates in C# 3.0 it is fairly easy to assign lambda expressions to generic delegates and pass those delegates to the extension methods.. Let's take a look at an example of using a few of those generic delegates:

        List<int> numbers = new List { 1, 2, 3, 4, 5, 6, 7 };
        Func<int, bool> where = n => n < 6;
        Func<int, int> select = n => n;
        Func<int, string> orderby = n =>  n % 2 == 0 ? "even" : "odd";
        var nums = numbers.Where(where).OrderBy(orderby).Select(select);

In the above example, we are using three different extension methods: where, orderby and select. The where extension method takes a generic delegate with int parameter and a return type of boolean to identity if a certain element be included in the output sequence. The select extension method takes an integer parameter and returns an integer, but it could return anything that you want the result to be transformed into — before being sent to the output sequence. In the orderby extension method, we are taking the integer parameter and using it to identify if it is even or odd. Based on that, we sort the results. With the introduction of generic delegates in C# 3.0, it is fairly trivial to assign our lambda expressions to generic delegates and pass those delegates to the extension methods.

Generic delegates allow you to define up to 4 parameters and 1 return type so you can have a delegate that may look something like this:
Func<int, bool, string, double, decimal> test;
Read more>>

Expression Lambdas vs Statement Lambdas

Lambda expression is an inline delegate introduced with C # 3.0 language. It’s a concise way to represent an anonymous method. Lambda expressions are particularly helpful for writing LINQ query expressions.

The => operator has the same precedence as assignment (=) and is right-associative.

Expression Lambda

A lambda expression with an expression on the right side is called an expression lambda.

Syntax
(input parameters) => expression

The parentheses are optional only if the lambda has one input parameter; otherwise they are required. Two or more input parameters are separated by commas enclosed in parentheses:

(x, y) => x == y

Sometimes it is difficult or impossible for the compiler to infer the input types. When this occurs, you can specify the types explicitly as shown in the following example:

(int x, string s) => s.Length > x

Specify zero input parameters with empty parentheses:

() => SomeMethod()

Statement Lambda

A statement lambda resembles an expression lambda except that the statement(s) is enclosed in braces:

Syntax
(input parameters) => {statement;}

Below example uses expression lambda and statement lambda to find even numbers from a List<int>

        List<int> numbers = new List<int>{1,2,3,4,5,6,7};
        var evens = numbers.FindAll(n => n % 2 == 0);
        var evens2 = numbers.FindAll((int n) => { return n % 2 == 0; });
Read More>>

dotnetperls/lambda

Exploring-Lambda-Expression-in-C#

Expression Lambda example - Find customers in UK

MSDN says "a lambda expression with an expression on the right side is called an expression lambda." Here is an example of Assume you have a List<Customer> where Customer has the following properties.
Customer {
int CustomerId,
string Name, 
string Country, 
string Email,
string Phone
}

If you want to create a List<Customer> where customer country = UK then you can use the below syntax.

List<Customer> ukCustomers =
Customers.FindAll(customer => customer.Country.ToUpper().Equals("UK"))

If you want to do the same thing using LINQ

List<Customer> ukCustomers = from Customer customer in Customers 
                where customer.Country.ToUpper().Equals("UK") 
                select customer 

LINQ query to create Dictionary

Assume you have a List<Customer> where Customer has the following properties.
Customer {
int CustomerId,
string Name, 
string Country, 
string Email,
string Phone
}

If you want to create a Dictionary of all customers who has an email address, i.e. Dictionary then you can use the below syntax.

Dictionary customerEmails = (from Customer customer in customers
                            where string.IsNullOrEmpty(customer.Email)
 select new { customer.CustomerId, customer.Email})
.ToDictionary(customer => customer.CustomerId, customer => customer.Email);

Below example shows how this can be done using Lambda Expression

Dictionary customerEmails =
Customers.FindAll(customer => !string.IsNullOrEmpty(customer.Email))
.ToDictionary(customer => customer.CustomerId, customer.Email);