November 2006 - Posts
Over the last few days I've been applying the Visual Studio 2005 Code Analysis (FxCop) to a legacy project of ours to clean up the source in accordance with the guidance embodied in the Code Analysis rules. What should have been a simple job became more complicated, as is usually the case.
The first thing I came up against was what appeared to be strange behaviours in the Code Analysis tool. Different results were returned when Code Analysis was performed on the Debug configuration than the Release configuration. Analysis rules that passed in the Release configuration failed for the Debug configuration. According to this post this can be explained by the difference in IL emitted by compiler in the two different build configurations, though I'm not 100% convinced by this explanation for reasons I'll explain in a moment.
The second issue that caused me much grief was attempting to overload the == operator in a class. This failed the rule that requires validation of parameters passed into public methods (ie checking that the parameters aren't null) before they are used. The problem is that the standard test of parameter == null causes the == operator to be called recursively resulting in a stack overflow. The solution turned out to be to implement the == operator overload as follows. The cast to object is required to ensure that the polymorphism is resolved correctly and that operator == implementation from the base object class is used to resolve whether a parameter is null. Not particulary intuitive, but easy enough once you know how. I had tried using the Object.ReferenceEquals() method to check for null, which functions correctly but still fails the Code Analysis rule.
public static bool operator ==(MyClass a, MyClass b)
{
// If both parameters are null, they are obviously equal
if (((object)a == null) && ((object)b == null))
return true;
// If only one of the parameters is null they are not equal
if (((object)a == null) || ((object)b == null))
return false;
// If both parameters are not null, apply a custom equality test
return a.Val == b.Val;
}
So why am I not convinced about the differences in IL emitted by the compiler as the reason why rules fail in Debug mode but not in Release mode? Mostly because the above operator overloading passes Code Analysis when analysed in Release mode but fails on the requirement for validating parameters when analysed in Debug mode. If in Release mode the compiler optimised the IL to remove the test for a parameter against null, the analysis test would fail because the parameter is not validated before it is used. The only way the test can pass in Release mode is if the test still exists in the generated IL and the only way it can fail in Debug mode if the test is not present. I can't figure out what would cause the test to be removed from the Debug version.
del.ico.us |
Digg It |
Technorati |
StumbleUpon |
Furl |
reddit
If I was one of the ORM vendors in the TechEd exhibition hall I think I'd be packing up and going home! Having attended the session on LINQ to SQL (formerly known as DLINQ) this morning it's obvious that DLINQ will do most of the things most people want from an ORM tool. The bad news for the existing ORM vendors is that it seems to do it in a neater and easier to manage way than current ORM offerings and that Microsoft will ship a bunch of command line and visual design tools that will automate much of the mapping process.
It's clear from what was presented this morning that LINQ to SQL does indeed implement the IdentityMap pattern, the UnitofWork pattern and supports both eager and lazy loading. One-to-one and one-to-many relationships are handled sensibly (and bidirectionally) through properties/fields of mapped objects. Many-to-many relationships can also be handled though not transparently as the intermediate table is always surfaced as a separate class in the mapping. At first glance LINQ to SQL certainly does seem to do everything I want from an ORM tool and seems to do it sensibly. As always though the proof of this particular pudding will be in the using of it. I for one will be downloading the latest CTP and trying it out on a substantial database project at the first chance I get. Watch this space for feedback.
One thing that I've wondered about for some time is why Visual Studio 2003 is more stable and less buggy than Visual Studio 2005. Does this indicate that VS2005 was half-baked and not ready for release when it was pushed out? Having seen some of the sessions here at TechEd I believe this wasn't the case. Although VS2005 introduced some cool new features, I suspect the majority of development effort went into a serious restructuring of the product so it could be easily extended to support lots more cool stuff in the future (e.g. code gen, visual designers, domain specific languages, etc). When you take a look at forthcoming technologies such as LINQ it's abundantly clear that .NET 2.0 and VS2005 were just an intermediate but important step on the road to the much bigger picture. My personal suspicion is that the need to modify Visual Studio to accommodate the eventual goals led to some severe restructuring and this in turn is what is responsible for the issues that exist in it today. Don't get me wrong, it's a fantastic tool; the few rough edges that do exist are a small price to pay for what's coming in the future!
del.ico.us |
Digg It |
Technorati |
StumbleUpon |
Furl |
reddit
Relationships can be tricky, not only human relationships but relationships in code too. I'm thinking in particular of the relational databases here and the mapping of relational data to an object-oriented view of the data in memory.
As an OO developer I like to represent my data entities as objects for several reasons; firstly, it's a model I understand well and secondly, one of the key tenets of OO is encapsulation and keeping data with the code that operates on it. When I'm dealing with data that has been loaded from a database into memory I often need to provide code to operate on it (e.g to perform validation or some other business logic). Organizing the data as an object allows me to place this code in the class that actually contains the data and I know exactly where to go modify the code that operates on this data. However the mapping of relational data to objects is a tricky problem and whole books have been written on the subject (Martin Fowler's Patterns of Enterprise Application Architecture is a good place to start).
As a developer I don't want to get involved in writing all this database mapping code, I'd just like something that does it all this for me so I can get on with the real business of coding my applications. So I've spent quite a bit of time recently looking at available Object Relational Mapping technologies. I've been reasonably impressed with the .netTiers templates for CodeSmith as this builds upon the best practices implemented in the Microsoft Patterns & Practices Enterprise Library. I'm still getting to grips with it and haven't fully figured some of it out, particularly the caching (not sure if it implements the IdentityMap pattern) and lazy loading but so far it looks like a good solution. However there are plenty of other ORM offerings out there and I was pleasantly surprised to see a couple of vendors pushing their offerings at TechEd. There were two offerings in particular that I hadn't come across before so I'll be checking these out just as soon as I get a moment to do so.
The other thing being pushed heavily at TechEd is the .NET 3.0 Framework and in particular the Language Integrated Query framework. This framework provides a very powerful way to query data using SQL-like constructs, but more importantly to map relational data from a SQL database into an in-memory object using the DLINQ API. The ORM vendors at TechEd indicate that their products are LINQ-capable, which set me wondering whether DLINQ reduces the need for ORM tools. As far as I can tell at the moment DLINQ handles the object-relational mapping and persistence nicely but has no support for things such as caching, lazy loading, etc. So I guess there'll still be a need for ORM tools which provide implementations of these standard patterns required for enterprise applications and that DLINQ will just make it a bit easier to do some of the heavy lifting required. I'm attending a presentation on DLINQ tomorrow so hopefully all will be made clearer then.
TechEd-Developers
del.ico.us |
Digg It |
Technorati |
StumbleUpon |
Furl |
reddit
Why is it that the only time I ever get to post articles to my blog is when I step outside my normal daily routine for a while? Although I'd like to blame it on being too busy during my daily life, I suspect this has more to do with a lack of blogging discipline on my part.
I'm currently taking a week out of my normal routine to write blog posts whilst attending TechEd in Barcelona. As usual it's a typically slick Microsoft production, though it doesn't feel as slick as normal and certain indicators suggest a tightening of the budget (no TechEd party, fewer snack stands, fewer handouts from Microsoft, etc). TechEd is the conference to attend to get up to the minute information on current Microsoft tools and technologies and they like to make a big play of just released products. Although Office has RTM'd this week and Vista is due to do so soon, there's a definite feeling of anti-climax in the air because these products weren't quite ready in time and couldn't be given quite as big a push here this week.
The keynote address was interesting. It started off with a big plug for the Imagine Cup, a student programming competition that I've been closely associated with in my day job as an academic since it's inception four years ago. Whilst I think it's great that the Imagine Cup got this publicity as the first item on the agenda, I really don't think it was the right thing to put across to the audience as it has little or no direct interest to most developers. The thinking behind including it in the keynote was apparently to raise awareness of the future developer talent available which the audience may remember and be influenced by at hiring time, which it will do, but I'm still not sure it was the right place to make that point. I just have a feeling that developers paying upwards of £1500 to attend a conference on Microsoft tools and technologies don't want the first thing they hear to be something about a student programming competition; they want to hear about the technology developments that are going to make their lives easier.
The other thing that sat awkwardly about the keynote was the inclusion of a section on the achievement of an 11 year old girl from Pakistan being the youngest person ever to achieve her C# and .NET certification. Although everyone in the room applauded her achievement I know that I, and others in the room, were disgusted by Microsoft's treatment of her. Yes, it's good to laud the IT achievement of a young person from a developing nation but Microsoft managed to turn it into a blatant PR stunt by bringing the girl over to Pakistan to attend TechEd. If they'd just let her attend as reward for her achievement I wouldn't have a problem with that, but they did the typical Microsoft thing of making her appear on stage and do some demos in front of 4500+ developers. Having had students of mine go through that same experience, I know that those 5 minutes she spent on stage will have been preceded by several days of non-stop rehearsal and training. An 11 year old girl should not be doing that, it smacks of exploitation. It's fine for a 19 year old student to do so, they're adult and capable of making their own choices, but it really didn't seem right to me for a child to be sucked into the Microsoft machinery in quite the same way. Maybe I'm just getting deeply cynical in my old age.
On the plus side, I did get to do a bit of sightseeing earlier in the week. Last time I was at TechEd in Barcelona I was with a team of students participating in the Imagine Cup and all I saw was the inside of the conference venue all week. This time I spent a day wandering around Barcelona and I have to say it is a very beautiful city and, by all accounts, unseasonably warm for this time of year. I would upload some of the photos I took the other day but my laptop refuses to read the memory card from my camera L
TechEd-Developers
del.ico.us |
Digg It |
Technorati |
StumbleUpon |
Furl |
reddit