Duck Typing in C#
The duck typing library is a .NET class library written in C# that enables duck typing. Duck typing is a principle of dynamic typing in which an object’s current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class. (For a detailed explanation, see the Wikipedia article.)
Beyond simple duck typing, this library has evolved to include different forms of variance in class members and other advanced features.
Very cool: “Although there is a performance hit when a new proxy type is generated, use of the casted object is virtually as fast to call as the object itself. Reflection.Emit is used to emit IL that directly calls the original object.”
Posted by Hartmut Wilms at 20:13
Issue Dynamic LINQ Queries to SQL Databases with LINQPad
Can’t wait for C# 3.0 and LINQ? Well you don’t have to! Dynamically query SQL databases today in LINQ: no more struggling with antiquated SQL. Download LINQPad and kiss goodbye to SQL Management Studio: LINQPad supports LINQ to objects, LINQ to SQL and LINQ to XML—in fact, everything in C# 3.0 and .NET Framework 3.5. LINQPad is also a terrific learning tool for experimenting with this exciting new technology.
Joseph Albahari, co-author of C# 3.0 in a Nutshell, has published LINQPad (Beta) on his website. LINQPad requires .NET Framework 3.5 Beta 2 and allows to issue dynamic LINQ queries to SQL Server databases. It’s a great way to get acquainted with LINQ and to get to know its workings. Got, get it!
Posted by Hartmut Wilms at 19:26
Astoria Updated for Visual Studio Beta 2
Posted by Hartmut Wilms at 14:00
MSBuild vs. NAnt
Tomas Restrepo has posted a nice comparison of MSBuild and NAnt. Although I’ve been using MSBuild for a long time, I’m thinking of giving NAnt a try. MSBuild is missing some useful tasks, especially concerning deployment of web applications and configuring IIS. Community tasks are available, but they are very often lacking properties in order to customize the tasks to my requirements/likings:
Posted by Hartmut Wilms at 13:55
Castle RC3 has been released
Castle is an open source project for .net that aspires to simplify the development of enterprise and web applications. Offering a set of tools (working together or independently) and integration with others open source projects, Castle helps you get more done with less code and in less time.
The main components of Castle are:
- MonoRail - an MVC web framework inspired by ActionPack (Ruby on Rails)
- ActiveRecord - an ORM based on NHibernate
- Windsor - an inversion of control container
Posted by Hartmut Wilms at 13:40
Know your LINQ
Ian Griffith writes about the problems he ran into while using LINQ for real. In LINQ to SQL, Aggregates, EntitySet, and Quantum Mechanics he starts with Aggregates and null values:
The first issue is that an aggregate might return null. How would that happen? One way is if your query finds no rows. Here’s an example: […]decimal maxPrice = ctx.Products. Where(product => product.Color == "Puce").
Max(product => product.ListPrice);
This code fragment will throw an InvalidOperationException due to the assignment of a null value to a decimal. The problem lies in the signature of the Max extension method, which return a TResult instead of a TResult? (nullable return type). This is by design, because TResult wouldn’t allow to use reference types in your lambdas. Ian provides a rather simple solution to the problem:
If we want
Maxto return us a nullable type, we need to make sure the selector we provide returns one. That’s fairly straightforward:decimal? maxPrice = ctx.Products. Where(product => product.Color == "Puce"). Max(product => (decimal?) product.ListPrice);
We’re forcing the lambda passed to
Maxto return a nullable
decimal. This in turn causes
Maxto return a nullable
decimal. So now, when this aggregate query evaluates to
maxPricegets set to
null– no more error.
The rest of his article discusses issues when combining queries in order to use aggregate functions across relationships. Be sure to read every line of the article, it’s worth your time. There’s no sense of summarizing his words, because the topic is a rather complex one. The one thing to point out is that using LINQ requires to know LINQ:
The critical point here is to know what LINQ is doing for you. The ability to follow relationships between tables using simple property syntax in C# can simplify some code considerably. With appropriately-formed queries, LINQ to SQL’s smart transformation from C# to SQL will generate efficient queries when you use these relationships. But if you’re not aware of what the LINQ operations you’re using do, it’s easy to cause big performance problems.
As always, a sound understanding of your tools is essential.
There’s nothing left to say.