March 2010 Entries

I was once asked in a technical interview what I could tell them about Reflection.   My response, while a little tongue in cheek was that "I can tell you it is one of my favorite topics to talk about"

I did get a laugh out of that and it was a great ice breaker.    Reflection may not be the answer for everything, but it often can be, or maybe even should be.    

I have posted in the past about my favorite CopyTo method.   It can come in several forms and is often very useful.   I explain it further and expand on the basic idea here 

The basic idea is to allow reflection to loop through the properties of two objects and synchronize the ones that are in common.   I love this approach for data binding and passing data across the layers in an application.

Recently I have been working on a project leveraging Data Transfer Objects to pass data through WCF calls.   We won't go into how the architecture got this way, but in essence there is a partial duplicate inheritance hierarchy where there is a related Domain Object for each Data Transfer Object.     The matching objects do not share a common ancestor or common interface but they will have the same properties in common.    By passing the problems with this approach, let's talk about how Reflection and our friendly CopyTo could make the most of this bad situation without having to change too much.

One of the problems is keeping the two sets of objects in synch.   For this particular project, the DO has all of the functionality and the DTO is used to simply transfer data back and forth.    Both sets of object have parallel hierarchies with the same properties being defined at the corresponding levels.   So we end with BaseDO,  BaseDTO, GenericDO, GenericDTO, ProcessAreaDO,  ProcessAreaDTO, SpecializedProcessAreaDO, SpecializedProcessAreaDTO, TableDo, TableDto. and so on.

Without using Reflection and a CopyTo function, tremendous care and effort must be made to keep the corresponding objects in synch.    New properties can be added at any level in the inheritance and must be kept in synch at all subsequent layers.    For this project we have come up with a clever approach of calling a base GetDo or UpdateDto making sure that the same method at each level of inheritance is called.    Each level is responsible for updating the properties at that level.

This is a lot of work and not keeping it in synch can create all manner of problems some of which are very difficult to track down.    The other problem is the type of code that this methods tend to wind up with.

You end up with code like this:

One obvious problem is that this is tedious code.   It is error prone code.    Adding helper functions like GetDtoNullSafe help out immensely, but there is still an easier way.

We can bypass the tedious code, by pass the complex inheritance tricks, and reduce all of this to a single method in the base class.

In the case of this one project, such a change eliminated the need for 20% of the total code base and a whole class of unit test cases that made sure that all of the properties were in synch.

The impact of such a change also needs to include the on going time savings and the improvements in quality that can arise from them.    Developers who are not worried about keeping the properties in synch across mirrored object hierarchies are freed to worry about more important things like implementing business requirements.

 

 

It's intellisense for SQL Server

Anyone who has ever worked with me heard me speak, or read any of writings knows that I am a HUGE fan of Reflector.    By extension, I am a big fan of Red - Gate  

I have recently begun exploring some of their other offerings and came across this jewel.

SQL Prompt is a plug in for Visual Studio and SQL Server Management Studio.    It provides several tools to make dealing with SQL a little easier for your friendly neighborhood developer.

When you a query window in a database, the plug-in kicks in and gathers the metadata for the database that you are in.    As you type a query, you get handy feedback like a list of tables after you type select.    You can select one of the tables, specify * and then tab to expand the select clause to include all of the columns from the selected table.    As you are building up the where clause, you are prompted by the names of columns in the selected tables.

If you spend any time writing ad hoc queries or building stored procedures by hand, this can save you substantial time.

If you are learning a new data model, this can greatly cut down on your frustration level.

The other really cool thing here is Format SQL.   I have searched all over the place for a really good SQL formatter.    Badly formatted SQL is so much harder to read than well formatted SQL.   Unfortunately, management studio offers no support for keeping your SQL well formatted.   

There are many tools available to format your SQL.   Some work better than others.    Some don't work that well at all.   Most will give you some measure of control over how the formatted SQL looks.    SQL Prompt produces good results and is easy to configure.

Sadly no tool is perfect, and what would we be without a wish list.    There are some features that I would like to see:

  • Make it easier to paste SQL in and out of code.    Strip off string builder, etc
  • Automate replacing hard coded values with bind variables or parameters
  • In addition to reformatting SQL, which is a huge refactor, support for other SQL refactors would be nice.    Convert join to sub query and vice versa come to mind

Wish list a side; this is a wonderful tool that easily saves me an hour or more on most weeks.   

Have you Used NDepend?

I have often wanted to use it, but never spent the money on it.   I have developed many tools that try to do pieces of what NDepend does, but never with as much success as they reach.

Put simply, it is a tool that will allow you to udnerstand and monitor the architecture of your software, and it does it in some pretty amazing ways.

One of the most impressive features is something that they call Code Query Language.   It allows you to write queries very similar to SQL to track the performance of various software metrics and use this to identify areas that are out of compliance with your standards and architecture.

For instance, once you have analyzed your project, you can write queries such as :

SELECT METHODS WHERE IsPublic AND CouldBePrivate 

You can also set up such queries to provide warnings if there are records returned.    You can incorporae this into your daily build and compare build against build.

There are over 82 metrics included to allow you to view your code in a variety of angles.

I have often advocated for a "Code Inventory" database to track the state of software and the ROI on software investments.    This tool alone will take you about 90% of the way there.

If you are not using it yet,  I strongly recommend that you do! 

Friends of Redgate  Friend of Red Gate

I recently joined the friend of Red Gate program.   I am very honored to be included in this group.    This program is a big part of Red Gates community outreach.   If you are not familiar with Red Gate, I urge you to check them out.    They have some wonderful tools for the SQL Server community and the DotNet community.    They are also building up some tools for Exchange and Oracle.

I was invited to join this program primarliy because of my work with Simple Talk and promoting one of their newest products, Reflector.

Reflector is a wonderful tool.   I doubt that anyone who has ever used it would argue that point.

Red Gate did a wonderful job taking over the support of Reflector.   I know many people had their doubts.    The initial release under Red Gate should set those fears to rest.   I was very impressed with how their developers interacted with their users during the preview phase!

Red Gate is also a good partner for the community.    They activly support the community, sponsoring Code Camps, sponsoring User Groups, supporting the Forums, etc.

And their tools are pretty amazing as well.