Archive for the 'dotnet' Category

LINQ Performance versus ADO.NET

Sunday, August 19th, 2007


Last week I presented Deeper in LINQ for the Wisconsin .NET User Group and concluded with a speed test. The speed test compared ADO.NET to LINQ. The ADO.NET method called a stored procedure while the LINQ method generated the query inline. The results showed that LINQ is not as fast. The overhead is due to the LINQ for SQL provider that has to parse the query within the CLR and then generate the SQL that it sends over to SQL Server. The generated SQL is identical to what is in the stored procedure used by the ADO.NET method. The difference in timing is purely from preparing the query. And since LINQ to SQL uses ADO.NET once it generates the SQL it cannot possibly run faster than a direct ADO.NET implementation. The speed test was simply to show the difference.

[ Download the sample project: DeeperInLINQ.zip ]

In the attached screenshot you can see three tests. I wanted to make a LINQ sample that runs much faster so I added the second test to show how long it would take to have LINQ call the stored procedure just like the ADO.NET method and avoid the overhead of preparing the query. It seems that even though the original LINQ method is running the same query over and over it still has to carry out the preparation for the query each time. This is clearly an area that could be tuned prior to the .NET 3.5 release. In the meantime, the call to the stored procedure allows the new LINQ method to perform nearly as well as the ADO.NET version. If this particular method was performing poorly in a real application, breaking out the work to a stored procedure would be a good tuning option. But most of the time you will want to continue with inline LINQ queries so you get the productivity benefits that it provides through Intellisense and other features.

(more...)

Tuesday Presentation: Deeper in LINQ

Monday, August 13th, 2007

Tuesday night I will be presenting Deeper in LINQ for the Wisconsin .NET User Group. Below is the summary for the presentation.

Brennan Stehling will continue covering LINQ following Dave Bost's presentation earlier this year. This presentation will dig into more detail about querying databases, XML and collections of objects. The presentation will compare the performance of LINQ and ADO.NET queries and show the advantages of LINQ over conventional ADO.NET programming. The presentation will also look into databinding LINQ results to web forms in ASP.NET using the new LinqDataSource.

Since the presentation in January there have been some changes to LINQ. You can now get the Beta 2 release of the .NET 3.5 runtime and Visual Studio 2008. I will be doing a short review of the updated IDE before getting into the LINQ samples I have prepared. The presentation should answer all of your questions about LINQ and help you start using it right away.

For the meeting time and location, see the website. (www.wi-ineta.org)

A Bug with Reflection and Inheritance

Wednesday, June 27th, 2007

Last week a bug in the String object was found. It was apparently due to a compiler optimization which will not be fixed for a while. But we have come this far without discovering it, so we can wait a little longer.

:)

The last few days I have been trying to fix a bug that was making absolutely no sense to me. It turns out my problem is another known bug in the .NET framework. I am using reflection to automatically bind data values to objects which inherit from a base class. The base class actually does all of the work while the inheriting classes define properties which line up with the data values from the database. I confirm that the name and type of the property is the same as the data value but when the SetValue method on the PropertyInfo object was called it threw a TargetException.

TargetException: Object does not match target type.

(more...)

Custom Exceptions for the Real World

Thursday, June 21st, 2007

In response to the freshly coined ADD methologies I decided to assemble the following list of custom exceptions for the real world. I do not recommend using them, but sometimes I come across code and I wish I could just throw one of these exceptions.

LegacyCodeException

Sometimes you run across a class that is thousands of lines long with most of the work done in a single public method. It may even use several member variables all over the place so it is hard to keep track of everything as you look through the code. When I have to update code like this to add a new feature or fix a bug I wish I could just wrap it in a try...catch block and if it catches an exception I would like to re-throw it inside of a LegacyCodeException.

(more...)

Subversion Bridge for CodePlex in Progress

Thursday, June 21st, 2007

The project is called SvnBridge and it will work for CodePlex as well as your own Team System environment which is an unexpected bonus. The documentation explains that the bridge works entirely on the client-side so you do not have to install anything onto the server. This functionality was supposed to be available on CodePlex by now but it appears they have some issues to work out.

Once this is done it will be a great addition to Team System. I would like to have TortoiseSVN installed so that I can get updates from within Windows Explorer instead of firing up Visual Studio for everything.

I have suggested to people that you can use Subversion for more than just managing software. I know of one company that is managing their office documents with Subversion. It gives them version control for their spreadsheets so they can access old versions and even create branches. With SvnBridge this could be done with Team System transparently. I just hope the update to Team System this year adds many of the features most developers really miss after using Subversion. In the meantime at least we have the Team System Power Tools.

Using Third Party Assemblies

Saturday, June 16th, 2007

There has been some discussion on using third party assemblies after a post from Scott Hanselman called Some Guiding Principles for Software Development. One of the rules suggests that you should buy instead of build whenever you can while another rule suggests that you should limit the compile-time dependencies on code you do not own. In response to these guidelines the creator of BlogEngine.NET, Mads Kristensen, wrote Think twice before using any third-party assemblies that also encourages not using third party assemblies along with a list of sample scenarios. It appears that he avoids third party assemblies more aggressively than I do. I try to follow a few simple rules when considering a third party dependency.

  • Can it be done easily with just the standard .NET Framework?
  • Can I get the source code for the project?
  • Does the project include sample projects as a reference?
  • Is there sufficient documentation available to get started quickly?
  • Is the project active with regularly planned releases?
  • Is there any significant value in building it from scratch?

There are projects released by Microsoft that meet these requirements, such as the Enterprise Library and the ASP.NET AJAX Control Toolkit. What I like about these two projects is they are hosted on CodePlex and all of the source code is freely available. There is also plenty of documentation and working sample projects as a part of the installation that make it very easy to get started.

(more...)

Startup with DotNetKicks.com

Friday, June 15th, 2007

One of the nice little features of Visual Studio is the list of headlines that comes up on the startup screen. It uses an RSS feed from MSDN by default but you can set it to something else, such as DotNetKicks.com. Specifically, you want to enter the URL for the RSS feed:

http://www.dotnetkicks.com/feeds/rss

Simply open your Options from the Tools menu and select the Startup node. The screenshot below shows how it looks on my system.

And once you have the headlines coming in from DotNetKicks.com you will want to help make it better by contributing new kicks from your favorite blogs. Here are a few suggestions:

While I look at DotNetKicks.com for the occasional reading material I also use a blog aggregator to group many blogs together into a central point. I was using Rojo.com which has been plagued with instability so I have switched over to Google Reader which has been working great. I just wish there was more going on with DotNetKicks.com, so go read and kick those blogs!

Preparing for .NET 3.5

Monday, June 11th, 2007

The .NET 3.5 is coming fast. I have not really been terribly excited about it until this past week. I am starting to see things that are very interesting. I am already trying out LINQ with the CTP. The 3.5 release will be much more significant than the 3.0 release which essentially was just an augmentation of the framework. The 3.5 release will update the runtime and the compiler to support the new language features for C# and VB.NET.

To help you get your head around it you can could read this blog entry on .NET 3.5 which includes a diagram that lays out all of the changes. And to get a jump start on LINQ you can get into Blinq. You can simply point Blinq at a database and it will generate a new website with all of the code you need to update the data. The generated code is very useful as training material.

To immerse yourself completely in .NET 3.5 you can download and install Visual Studio "Orcas" which is currently marked as Beta 1. You can expect that there will be a Beta 2 as usual which will soon be followed by the final release by November. I would hope that the Beta 2 release comes within the next month or two.

SubSonic and Automation with MSBuild

Sunday, June 10th, 2007

SubSonic is a very useful project that you can use to generate a data access layer by reading your database schema. The command-line utility called SubCommander can generate the code in C# or VB.NET. The command-line tool can also generate scripts for the database to help you with versioning.

These commands can take a few parameters which may be different from project to project. To make it easy to keep track of multiple projects you can use MSBuild to automate the SubSonic tasks. I have prepared a sample project and video to walk through how it works. And if you are new to MSBuild, you can read 7 Steps to MSBuild to get a jump start.

Subversion in Visual Studio, Finally

Friday, June 8th, 2007


All this time that I have worked with .NET in Visual Studio there has not been a good way to integrate support for CVS or Subversion. For Subversion there was AnkhSVN which attempted to implement an SCC provider for Visual Studio. It never was fully embraced by developers due limitations of what could be done with SCC which was designed to support Visual Source Safe. The SCC interface is also something you must license if you want the full documentation. Bottom line, it is not something Microsoft makes easy to implement.

So it was good see a new add-in called VisualSVN which appears to take care of all of those concerns and implements good support for Subversion in Visual Studio. The FAQ explains that they do not even bother trying to implement an SCC provider.

(more...)