Tuesday, April 26, 2011

LINQ at Glance - Part 3

Standard Query Operators

Sequence-->Sequence
Filtering : IEnumerable<TSource>?IEnumerable<TSource>
Returns a subset of the original elements
Where, Take, TakeWhile, Skip, SkipWhile, Distinct
Projecting:
IEnumerable<TSource>?IEnumerable<TResult>
Transfors each element with Lambda Expression.
Select, SelectMany
Joining:IEnumerable<TOuter>, IEnumerable<TInner>?IEnumerable<TResult>
Meshes the elements of once sequence to another
Join, GroupJoin
Ordering:
IEnumerable<TSource>?IOrderedEnumerable<TSource>
Returnes Reordering the sequence
OrderBy, ThenBy, Reverse
Grouping:
IEnumerable<TSource>?IEnumerable<IGrouping<TSource,TElement>>
Groups the sequence into sub sequences
GroupBY
Set operators:IEnumerable<TSource>, IEnumerable<TSource>?IEnumerable<TSource>
Takes two sequences of same type and returns commanality,sum or difference
Concat, Union, Intersect, Except
Zip operator:
IEnumerable<TFirst>, IEnumerable<TSecond>?IEnumerable<TResult>
Enumerate each elemement in step and apply function over each element pair.
Conversion methods: Import:IEnumerable?IEnumerable<TResult>
OfType, Cast
Conversion methods: Export
IEnumerable<TSource>?An array, list, dictionary, lookup, or sequence
ToArray, ToList, ToDictionary, ToLookup, AsEnumerable, AsQueryable

Sequence--> Element or ScalarElement operators:
IEnumerable<TSource>?TSource
First, FirstOrDefault, Last, LastOrDefault, Single, SingleOrDefault,
ElementAt, ElementAtOrDefault, DefaultIfEmpty
Aggregation methods:IEnumerable<TSource>?scalar
Aggregate, Average, Count, LongCount, Sum, Max, Min
Quantifiers:
IEnumerable<TSource>?bool
All, Any, Contains, SequenceEqual

Nothing-->SequenceGeneration methods
void?IEnumerable<TResult>
Manufactures a simple sequence.
Empty, Range, Repeat

Monday, April 25, 2011

LINQ at a Glance - Part 2

Intepreted Queries
-------------------
Interpreted Queries are descriptive
They operate over sequences that implement IQueryable<> interface.
IQueryable is an extension of IEnumerable with additional methods for constructing Expression Trees.
They resolve to query operators defined in Queryable class.
They emit expression trees that gets interpreted in runtime.
LINQ to SQL and Entity Framework implement IQueryable interface.
Using AsQueryable extension method it is possible to generate IQueryable Wrapper around IEnumerable<> sequence.
Execution of Interpreted Queries
 Interpreted Queries follow a differed execution model.
 Under the covers, interpreted queries differ from local queries in how they execute.
 When you enumerate over an interpreted query, the outermost sequence runs a program that traverses the entire expression tree, processing it as a unit.
 In case of LINQ to SQL, LINQ to SQL translates the expression tree to a SQL statement, which it
 then executes, yielding the results as a sequence.
Compiling Expression Trees - One can convert an expression tree to a delegete using Compile() method.
The AsQueryable operator lets you write whole queries that can run over either local or remote sequences

LINQ at a Glance - Part 1


Basic unit of data in LINQ are sequences and elements.

sequences are enumerable values and elements are items in the sequence.

Local Queries
local sequence -  Queries that operate over local sequences are called local queries or LINQ-to-objects queries.
query operator - Query operator is a method that transforms the sequence.
standard query operators -  Where Select etc.

Data can be queried in following ways using LINQ

Fluent Syntax
Chaining Query Operators
Use Lambda Expressions

Query Expressions/Query Syntax
Query Expression always start with from keyword and ends with select or group keyword.
Range Variables --> The identified immediately following the from keyword syntax is called range variable.
Query expression allows us to introduce new range variables using let into and additional from caluse

Friday, April 1, 2011

MS-TechEd 2011 Key Take-aways

Below are the list of key take-aways in the recently concluded MS-TechEd 2011
·         Microsoft Cloud Infrastructure
o   Most of the sessions were centered around Microsoft Cloud Infrastructure. Check by previous post for the more details.
·         Windows Server AppFabric - http://msdn.microsoft.com/en-us/library/ee677312.aspx
o   Fulfills the Application Server Role in Windows Server
o   Provides following Services
1.       Caching Services – Distributed Caching (Previously CodeNamed “Velocity”)
2.       Hosting Services
1.       Workflow Persistence
2.       Monitoring Service
1.       Health Monitoring
2.       Uses High Performant ETW for tracing
3.       Using Administrative console we could control the level of the Trace Information.
4.       We can also add the custom trace information programatically
·         SQL Server 2008 R2 – StreamInsight -   http://www.microsoft.com/sqlserver/2008/en/us/r2-complex-event.aspx
o   Microsoft’s platform to build applications for streaming data
o   A comprehensive development platform for building event-driven applications.
o   Helps us to Query data on the fly data from different event sources using LINQ Queries.
o   Can be used in source data from Devices, Sensors etc and get time series data.
o   OSI Soft PI Historian has released Adapters for StreamInsight. http://blogs.msdn.com/b/streaminsight/archive/2011/03/21/osisoft-pi-adapter-release-candidate.aspx
·         IE9
o   Support to HTML5
o   Makes use of GPU
o   Microsoft Claims and demoed with a HTML5 page that IE9 is performing better than Google Chrome.
o   Not supported in Windows XP
o   They have created as many as 90+ demos to demonstrate the capabilities for IE9. http://ie.microsoft.com/testdrive/
·         Web Farm Framework 2.0 http://www.iis.net/download/WebFarmFramework
o   Microsoft product that enables you to easily provision and mange a farm of web servers.
o   Using Application Request Routing there was a demo on Load balancing with Servers deployed in WFF 2.0.
·         Windows 7 Sensor Platform http://msdn.microsoft.com/en-us/windows/ee658252.aspx
o   Using this Platform the was a demo on how to control the application using Light Sensors with managed code.
·         Infragistics DataVisualization controls
o   Infragistics showed the demo on DataVisualization controls that were included from release on 10.3 onwards
o   High performance controls that are less memory intensive.
o   These set of controls are available for WPF and Silverlight only
·         Windows Products for Integration
o   Biztalk2010 for Enterprise application integration.
o   Enables organizations to use BizTalk capabilities from both Windows Server AppFabric and Windows Azure AppFabric (http://www.microsoft.com/biztalk/en/us/appfabricconnect.aspx)

Microsoft Cloud Infrastructure in a Nutshell

Windows Azure - Operating System for Cloud
Windows Azure is the cloud services operating system that serves as the development, service hosting, and service management environment for the Windows Azure Platform. Windows Azure provides you on-demand compute and storage to host, scale, and manage web applications and services on the internet in Microsoft data centers.
Windows Azure Provides Following Services,
  • Windows Azure Compute Service
A Windows Azure compute service is built from one or more roles. A role defines a component that may run in the execution environment; within Windows Azure, a service may run one or more instances of a role.
Windows Azure supports the following three types of roles:

    • A web role is customized for web application programming, as supported by Internet Information Services (IIS) 7 and ASP.NET. Web roles run in full IIS 7.0.
    • A worker role is useful for generalized development, and may perform background processing for a web role.
    • A virtual machine (VM) role provides a user-customized image to make it easier to move existing Windows Server applications to the Windows Azure hosting environment.
A hosted service may be comprised of any combination of role types, and may include multiple roles of each type.
  • Windows Azure Storage Services
The Windows Azure storage services provide persistent, durable storage in the cloud. To access the storage services, you must have a storage account, which is provided through the Windows Azure Platform Management Portal .
The fundamental storage services include:
·         Blob service, for storing text or binary data
·         Queue service, for reliable, persistent messaging between services
·         Table service, for structured storage that can be queried
The Windows Azure SDK offers a REST API and a managed API for working with the storage services. You may access the storage services from within a service running in Windows Azure or directly over the Internet from any application that can send and receive data over HTTP/HTTPS.

Windows Azure Connect (In CTP)

Do you want to connect the on-premise server to Windows Azure Role directly at network level?

Windows Azure Connect provides a simple and easy-to-manage mechanism to setup IP-based network connectivity between on-premises and Windows Azure resources. We can connect on-premises Servers to the roles running in Windows Azure.

Windows Azure AppFabric (In CTP)

Windows Azure AppFabric is a middleware platform and a suite of services that makes it easy to build and manage reliable, scalable, fast and secure composite applications that run on Windows Azure. Windows Azure AppFabric consists of the following services:

  • The AppFabric Service Bus connects local, firewalled applications and data with applications in the cloud, rich desktop applications, and smart, Web-enabled devices anywhere in the world.
  • AppFabric Access Control is a claims-based access control service that can be used on most Web-enabled devices to build interoperable, federated authentication and authorization into any connected application.

  • AppFabric Caching provides caching services in cloud

SQL Azure (SQL Server On Cloud)

Microsoft SQL Azure extends SQL Server capabilities to the cloud. SQL Azure offers a relational database service called Microsoft SQL Azure Database. Using SQL Azure, you can easily provision and deploy relational database solutions. Benefits include manageability, high availability, scalability, a familiar development model, and a relational data model.

Windows Azure Platform AppliancePrivate Cloud Infrastructure
Windows Azure™ platform appliance is a cloud platform that customers can deploy in their own datacenter, across hundreds to thousands of servers. The Windows Azure platform appliance consists of Windows Azure, SQL Azure and a Microsoft-specified configuration of network, storage and server hardware.
The appliance is currently in Limited Production Release to a small set of customers and partners. Not Released as Commercial Product yet.

Thursday, March 3, 2011

dynamic vs reflection performance comparison

  • Visual C# 2010 introduces a new keyword, dynamic which is a static type, but an object of type dynamic bypasses static type checking. So C# can use the features of dynamic language.
  • The same feature can be used to invoke methods dynamically from .net assemblies.
  • Until C# 3.0 in order to invoke methods dynamically from .net assemblies we were using reflection.
  • From C# 4.0 we can achieve this using dynamic keyword elegantly.
  • For example if we want to call a method named Test from Class Class1 dynamically using dynamic keyword.
 Assembly ass = Assembly.Load("MyAssembly2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
 dynamic obj = ass.CreateInstance("MyAssembly2.Class1");
 obj.Test();
This code would compile in C# 4.0 as the Test() method will be resolved in runtime.
I tried to compare the performance impact of using dynamic keyword by invoking the method 900000 times in a loop Following is the responses for different scenarios.
  • Static : 10 milliseconds
  • Through Reflection : 1635 milliseconds
  • Through dynamic : 115 milliseconds
Dynamic Method invocation is around 15 Times faster than Reflection code.
It looks better to use dynamic in places where reflection is used as it is more performing and elegant.

Sunday, December 26, 2010

Partial Keyeword and C#

Partial Keyeword and C#
  • Partial keyword can be used with  class, struct, or interface
  • The partial keyword indicates that other parts of the class, struct, or interface can be defined in the namespace
  • All the parts must use the partial keyword.
  • All the parts must have the same accessibility, such as public, private, and so on.
  • If any part is declared abstract, then the whole type is considered abstract.
  • If any part is declared sealed, then the whole type is considered sealed.
  • If any part declares a base type, then the whole type inherits that class.
  • All the parts that specify a base class must agree, but parts that omit a base class still inherit the base type.
  • Parts can specify different base interfaces, and the final type implements all the interfaces listed by all the partial declarations.
  • Any class, struct, or interface members declared in a partial definition are available to all the other parts. The final type is the combination of all the parts at compile time.
  • At compile time, attributes of partial-type definitions are merged.
  • The following are merged from all the partial-type definitions:
    • XML comments
    • interfaces
    • generic-type parameter attributes
    • class attributes
    • member
Restrictions
  • All partial-type definitions meant to be parts of the same type must be modified with partial.
  • The partial modifier can only appear immediately before the keywords class, struct, or interface.
  • All partial-type definitions meant to be parts of the same type must be defined in the same assembly and the same module (.exe or .dll file). Partial definitions cannot span multiple modules.
  • The class name and generic-type parameters must match on all partial-type definitions. Generic types can be partial. Each partial declaration must use the same parameter names in the same order.
  • The following keywords on a partial-type definition are optional, but if present on one partial-type definition, cannot conflict with the keywords specified on another partial definition for the same type:
    • public
    • private
    • protected
    • internal
    • abstract
    • sealed
    • base class
    • new modifier (nested parts)
    • generic constraints