Tuesday, September 18, 2007

Astoria CTP Refresh for Visual Studio 2008 Beta 2

The Astoria team has just released a refresh of the Astoria Prototype CTP to work with Visual Studio Beta 2 and the Entity Framework Beta 2.
The Astoria September 2007 CTP is now available for download.
This CTP is a refresh of the May CTP/Prototype bits recompiled so they run with Visual Studio 2008/Entity Framework Beta 2.
You can visit the Astoria Team Blog for more information.
Technorati Tags: ,

The post also avalable at: http://blogs.microsoft.co.il/blogs/maordavid/archive/2007/09/18/astoria-ctp-refresh-for-visual-studio-2008-beta-2.aspx

Saturday, September 15, 2007

C# 3.0 var keyword

C# 3.0 has many-a-new features. This post will explain the 'var' keywork and the concept of Implicitly Typed Variables.

The var keyword is not a late-bound or un-typed variable reference. The var keyword always generates a strongly typed variable reference. The main idea is that the developer is not required to define the type of the variable at the time of declaration, but it is the task of the compiler to decide what type of the object the variable is. The compiler infer the type of the variable from the expression used to initialize the variable when it is first declared.

Important points about var keyword:

  • The type of the variable is defined by the value declared and decided at the compile time. (Size depends on the type of the value initialised)
  • Type casting is simple and handled by CLR.
  • Explicit functions for parsing to specific type.
  • Can be used to reference any type in C#.
  • The CLR actually never knows that the var keyword is being used.

What rules shuold you force to use the var keyword?

When you declare variable with the var keyword, you must to always do an initial value assignment.  This is because the var keyword produces a strongly-typed variable declaration. Hence, the compiler needs to be able to infer the type to declare based on its usage.   If you don't, the compiler will produce a compiler error.

   1: var vAge = 32; 
   2: var vHeight = 175.2; 
   3: var vName = “Maor David“;  

 


The compiler will infer the type of the "age", "height" and "name" variables based on the type of their initial assignment value. This means it will generate IL that is absolutely identical to the code below:


   1: int age = 32;
   2: double height = 175.2;
   3: string name = "Maor David";

You can also use it to other data types:



   1: foreach (var vTable in ds.Tables) 
   2: {
   3:     foreach (var vRow in ((DataTable) vTable).Rows) 
   4:     {
   5:  
   6:     }
   7: }

This is equal to:



   1: foreach (DataTable table in ds.Tables)
   2: {
   3:     foreach (DataRow vRow in table.Rows)
   4:     {
   5:     }
   6: }


Technorati Tags: , ,


You can read this post also at Maor David's Blog: http://blogs.microsoft.co.il/blogs/maordavid/archive/2007/09/15/C_2300_-3.0-var-keyword.aspx

Monday, September 10, 2007

Microsoft Launches Translation Service

Microsoft launched a service for automatic translation called Windows Live Translator. The site lets you translate a text limited to 500 words or a web page from English to German, Dutch, French, Spanish, Portuguese, Italian, Korean, Chinese, Japanese, Russian.

Microsoft uses Systran to produce most of the translations, but also offers an option to translate computer-related texts using a machine translation system developed in-house. Microsoft's translation technology has been used to translate technical materials, including MSDN Library.

Saturday, September 8, 2007

Building .NET 3.0 projects from Team Foundation Build 2005 not supported

Building .NET 3.0/3.5 projects from Team Foundation Build 2005 not supported. Team Foundation Build 2005 will always invoke the 2.0 Framework's MSBuild.exe, which does not support building solutions that target the 3.5 Framework.  (The 3.0 Framework shipped with Vista, while the 3.5 Framework is shipping with VS 2008.)

The 3.5 Framework includes new MSBuild bits which allow multi-targeting - i.e. MSBuild 3.5 can target the 2.0 Framework.

We can convert our solution to VS2008 beta 2 and target the solution to .NET framework 2.0. It's not enough. When we'll try to build this solution with Team Foundation Build 2005, we'll get an error MSB5014 - File format version is not recognized.

The MSBuild blog has great post about MSBuild, Orcas, and Multi-targeting. Find it here.

SqlBuildTask timeout

I built a big DB Dude project from MSBuild command line and got a timeout error.

MSBUILD : Build error TSD158: Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.

The default timeout for query execution is 60 seconds and stored in the registry. We can extend the query timeout by changing these values. The registry entries are:

HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\8.0\DBPro\Database\QueryTimeoutSeconds

HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\8.0\DBPro\Database\LongRunningQueryTimeoutSeconds

Friday, September 7, 2007

Lambda expressions introduction

NET 2.0 introduced Anonymous Methods. The idea behind anonymous methods it to write methods inline to the code. They are mainly used for small methods that don't require any need for reuse. For example, we have this code:

   1: bool isGreaterThan5(int n)
   2: {
   3:     return n > 5;
   4: }
   5:  
   6: ...
   7:  
   8: int GetGreatersThanFives(List<int> numbers)
   9: {
  10:     return numbers.Find(isGreaterThan5);
  11: }

We can code it with anonymous method:



   1: int GetGreaterThanFives(List<int> numbers)
   2: {
   3:   return numbers.Find(
   4:         delegate(int n) { return n > 5; }
   5:     );
   6: }

c# 3.0 represent cool thing: Lambda expressions.
Lambda Expressions make things easier by allowing you to avoid the anonymous method and that annoying statement block.

Instead



   1: delegate(int n){ return n > 5; }

We can write:



   1: n =>  n > 5

The form of Lambda expressions is:


argument-list => expression

Parameters to Lambda Expressions


The parameters of a lambda expression can be explicitly or implicitly typed. In an explicitly typed parameter list, the type of each expression is explicitly specified. In an implicitly typed parameter list, the types are inferred from the context in which the lambda expression occurs:



   1: (int x) => x + 100         // explicitly typed parameter
   2:  
   3: (x,y) => return x * y;    // implicitly typed parameter

Lambda Expressions with Multiple Parameters



   1: (x, y) =>  return x * y

Lambda Expressions without Parameters



   1: () => Console.WriteLine("Hello world!")      // No parameters

I think that's enough intro to Lambda Expressions.
I will follow up with a part 2 to show more about this cool fteature: Expression Trees, how LINQ uses it to pass code as parameters and more.

SqlMetal for Linq

SqlMetal builds a data access layer in seconds. The output is not just a first generation data access; SqlMetal's output includes all defined relationships (based foreign keys) between your tables. SqlMetal will produce a class for each table and, optionally, classes for all views, stored procedures and user-defined functions.

SqlMetal can generate strongly typed interfaces for stored procedures and user-defined functions. From the developer point of view, it is now a complete breeze to call either a stored proc and/or a user-defined function.

SqlMetal can generate a DataContext for your entire database with a single command.

This is very useful if you have a large number of tables in your system, as dragging and dropping them onto the designer would have got boring very quickly. You can find SqlMetal at [C:]\Program Files\Microsoft SDKs\Windows\v6.0A\bin\.

Lets create Northwind datacontext:

C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin>sqlmetal /server:.\sqlexpress
/database:northwind  /code:"c:\temp\northwind.cs"

We can then include the class within our project and use it as if the designer had created it. We can also get SqlMetal to include all of our views, functions and stored procedures. (use /sprocs /views /functions to extract these objects).

SQLMetal can also output the dbml (/dbml:file.dbml) or mapping (/map:file.xml) file for the database.

C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin>sqlmetal /server:.\sqlexpress
/database:northwind /dbml:"c:\temp\northwind.dbml"

SqlMetal has also generated an external XML Mapping File that maps the classes generated to the database tables, which is much more complete than what I had done my self...

C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin>sqlmetal /server:.\sqlexpress
/database:northwind /map:"c:\temp\northwind.map"

Conclusion:

The SqlMetal tool is useful to generate C# or VB.NET objects mapped on SQL Server database. It provides classes with members and properties mapped on table and view columns. It reflects the relationships between tables. It provides also a class derived from base class DataContext which maps, on request, functions and stored procedures, making difference between table functions and scalar functions and between stored procedures which returns a rowset and those who perform only operations such as insert, update and delete.

From my point of view: great and useful tool.

 

Technorati Tags:
 
del.icio.us Tags: