Raise the SNR

August 11, 2009

Trying to Create FxCop Rules for Source Code Analysis

Filed under: Development — admin @ 11:16 pm

I started working today on creating a rule in FxCop to enforce the use of curly braces on if and for statements. I know that FxCop is built to analyze IL, not source code, so this should be an interesting experience. I’ll blog about it if I’m successful.

UPDATE: I was correct about FxCop not reading the source code. So I was unable to enforce the curly brace rule. I was able to create a custom rule although it was a painful exercise.

The problem with custom FxCop rules is that Microsoft really doesn’t document how to do it. I sent an e-mail and got a very prompt reply from Brad Abrams, Krzysztof Cwalina, and David Kean. The final response I got about the curly braces was from David Kean:

Unfortunately, you will need to parse the source yourself, FxCop (even with the PDB) doesn’t provide enough information. Braces (like whitespace, comments, etc) are a source-only construct.

Here are some of the websites that I used to write the code.

http://weblogs.asp.net/rosherove/archive/2007/02/24/introducing-fxcopunit-a-framework-for-integrated-fxcop-rule-testing.aspx

http://blogs.interknowlogy.com/timmccarthy/archive/2007/02/13/11543.aspx

http://bordecal.mvps.org/Nicole/FxCop/CustomRuleDetails.htm

Accessing Underlying Data in ObjectDataSource – ASP.NET 2.0

Filed under: Development — admin @ 10:59 pm

I went around a few times on this problem today. I was pulling data into a GridView control from an ObjectDataSource. This ODS represented a Generic List of Data Transfer Objects that were populated using NHibernate. The DTO has a property that is really a foreign key into a reference table. The problem is that there are legacy portions of the system that still use the value of the foreign key as data. I know, I know, I should have refactored the relationship into the DTO. Time pressure being what it is, refactoring that portion of the system was not an option. Instead, I wanted to treat the “Order” field as a lookup into the list contained in a second ObjectDataSource. Then I would display the text for the lookup value in the GridView.

Since I dont yet know the internals of databound controls and datasources, I couldn’t find a way to access the data in the ODS programmatically. So, I came up with a huge hack instead. I decided that I needed functionality that looks like a DropDownList control. I had the value in the DTO, and I wanted the human readable text like the Text in a DropDownList. So I created an invisible DropDownList on my page that was bound to a second ObjectDataSource containing the reference data I needed. Then, I set the SelectedValue of the DropDownList to the Order property in the DTO. Finally, I returned the SelectedText of the DropDownList from a protected method that was called using Eval in the GridView’s databound column.

This works, and is much more performant than my first attempt, but there’s got to be a cleaner way to get the desired results.

Powered by WordPress