Navigation

Archive

<October 2008>
SunMonTueWedThuFriSat
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

Categories

Blogroll

Contact

Send mail to the author(s) Email Me

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way

Sign In
Copyright ©  2009   Jeff Klawiter . All rights reserved.
My Amazon.com Wish List

Pick a theme:
# Friday, October 24, 2008
by Jeff Klawiter - Friday, October 24, 2008 9:25:44 AM (Central Standard Time, UTC-06:00)
So today I was tasked with helping a client move an old custom VB6 app to a new Windows 2003 server. The company that created it was asking for an arm and a leg for a simple transfer. After we moved the entire program directory over everything seemed to be fine until we encountered an "ActiveX component can't create object" error. I was perplexed on how to track down the issue. There were not accompanying DLLs or OCX files. I tried Process Explorer to see if I could find any failed openings and the strings contained in the exe.

After some searching on the net I found this lovely page http://www.cryer.co.uk/brian/windows/trbl_nt_axccco.htm . It details on how to use Sysinternals Regmon to track down issues with failed ActiveX creation. Basicall you watch your program for registery key openings and look for any failed opens in the HKCR/Classes path. After running the program and encountering the error I found it.
HCKR\Classes\cdonts

When I saw that come up I immediately knew what to do. cdonts is no longer included with windows server. It is an emailing library using cdo. I've had to install it on new Windows 2003 servers more than once so old ASP classic sites could run. All I needed to do after figuring it out was copy the cdonts.dll over to the C:\windows\system32\ directory and run regsvr32 on it. Everything was golden.

I've been using Sysinternals for years. I'm always amazed at what new ways I can use their utilities to track down issues. Process Explorer has been a godsend for me over the years. After discovering it, removing malware from computers took me less than half the time it used to. It gives me so much information I'm not sure how I got by without it.

The Sysinternals Suite is a must have for any serious windows programmer or administrator. It ranges from programs to monitor network connections to being able to see and suspend individual threads in a program.

Comments [0] #      blog  |  kick it on DotNetKicks.com
# Thursday, October 23, 2008
by Jeff Klawiter - Thursday, October 23, 2008 8:15:23 PM (Central Standard Time, UTC-06:00)
The other day I was posed with a problem to implement a "Send as Attachment" feature to an existing application. I thought, well this should be easy I've seen many other do it before. I quoted the work at a few hours, figuring some quick searching would give my my answers. I was wrong.

Sending an email using the default email client is very easy with the mailto: protocol. The problem lies in adding an attachment. Common mail programs like Outlook and Thunderbird do not support the attachment option. So, I started looking at MAPI the native API for doing this. I could not find any .NET wrappers. I did find that there were controls created by MS for VB6. Most of the .NET examples I found used COM interop with the ActiveX controls. But after trying these controls (which look like they were last updated in 98) I found them to not work in XP SP3. I'd get random exceptions and was never able to actually get an email dialog to pop up.

After failing with the ActiveX controls I was out of time. The MAPI native commands may have worked but it would take too long to set up the native calls and test it. After discussing with the client we ended up coming up with a bit hackish but workable solution. I would use the mailto: protocol and then launch explorer with the attachment selected. To them this is a minor annoyance that is acceptable. To me this an unfortunate sign of the times.

Things that were once "easy" for native applications become complicated and buggy for managed. It seems too often that I must make use native calls or old unsuported COM objects to get the job done. While MS has done a great job building up the .NET Framework. It seems that many native API's still remain unwrapped. Furthermore some places I read that the MAPI api is unstable on Vista. It seems that some API's are slipping through the cracks.


Comments [0] #      Rant  |  kick it on DotNetKicks.com
# Monday, October 13, 2008
by Jeff Klawiter - Monday, October 13, 2008 12:22:40 PM (Central Standard Time, UTC-06:00)
There are blogs on MSDN popping up that SilverLight 2 is indeed gold and will be public tomorrow Oct 14th. They also announced Silverlight Tools for Eclipse with the promise of cross platform development for Silverlight. I think this is a huge step in the right direction for MS. By giving developers on OS's other than windows to do Silverlight Development in a fairly competent IDE it will give them a better position against Flex.

I have done some playing around in Silverlight 2 and really loved it. I'm just hoping for a Silverlight 2 project to come our way at Sierra Bravo.

I look forward to downloading the public bits and seeing what I can do with the full version.


Comments [0] #       |  kick it on DotNetKicks.com
# Sunday, October 12, 2008
by Jeff Klawiter - Sunday, October 12, 2008 11:59:53 AM (Central Standard Time, UTC-06:00)
Well Code Camp 5 is in the bag. It was a large turn out and many great talks. My talk however drew no interest. Oh well, I tried and will try again. I'm thinking of making my post about LINQ and Refactoring into a talk.

The talks I went to:
The Intersection of F# and LINQ
This was a great talk about how F# takes LINQ and adds much more value to it with it's functional nature. The demonstration was a ray tracer that used LINQ to build up all of the reflected pixels. It was quite impressive. Combine it with the Arc talk and I am now very interested in Functional programming.

The Arc Programming Language
I had seen this talk a few days earlier. The speaker was hilarious and did a great job of bringing excitement back to lisp. Arc is a new dialect of Lisp that aims to cut down on parenthesis and add shorthand for common operations. He added objects to Arc with six lines of code, quite impressive.

BOO! A Wrist-Friendly Language for the CLI
BOO! is a another language along the lines of Lisp where it can redefine parts of itself through macros. The difference is it is statically typed like C#. It does offer quite a bit of reduction in code written. The presenter Justin Chase did a good job showing many aspects of the language. I also won a shirt for figuring out that the mystery function was calculating a fibonocci sequence. The code for it was quite cool, a,b = b + a or something like that.

Building MyTube with Microsoft Silverlight 2
Jeff Brand from Microsoft was giving this talk. It was a basic overview on SilverLight, the purpose for it and how to implement a simple youtube interface. I didn't stay for the entire thing, I had to go get ready for my talk.

My talk "Pick/Multivalue 101"
No one that was interested showed. Not that I was surprised but still a bit disappointed.  While giving the talk at my work we had over 15 people show. One of my co-workers showed and the other guy that showed was just looking for a place to sit. So instead we talked about LINQ, customized programming in Linux and a bit about Multivalue

I went to the speakers after party, got to gossip with some people from Magenic. Learned that Justin Chase left Magenic a few weeks ago to work on the Blend team at MS. He's a lucky guy and seemed rather excited for the features they are working on that he can't talk about. I'm sure some of them may come up soon at PDC. I did hear some gossip earlier in the day that SilverLight 2 RTM is going to be released as early as tomorrow (Oct 13th).


Comments [0] #      blog  |  kick it on DotNetKicks.com
# Monday, September 29, 2008
by Jeff Klawiter - Monday, September 29, 2008 3:28:37 PM (Central Standard Time, UTC-06:00)
Being a developer is one of the luckiest professions in the world. Just take a minute and think about this: How many professions out there are you paid to learn? Seriously, if we wish to succeed we must learn. Many times we need to learn during the project. We are given the opportunity and often encouraged to learn new ways of doing something, come up with innovative ideas, implement new technologies. Many companies offer compensation for developers to get certified, keep up, to learn. I've also found that many of the people I know that went to college for CS came out ill prepared for the real development world.

I was lucky in some ways. I got a job running websites during high school, I was paid to learn and mature as a programming. I wasn't lucky that pay sucked and I had to get food from charity at times. But there was always being able to learn on the job and the outlook that real developers jobs paid very well.

Over the years as I've matured as a developer I've learned quite a lot. I strive to keep on learning, branching out into new areas. I've found that my inner urge to learn is also what has allowed me to succeed. Below are some of my tips to being a successful programmer

Be passionate

The one thing I've seen in every great developer I've ever met is passion. Passion for the job, passion for learning, passion for being challenged. There is no way to improve without having a need for it. If being a developer is just a job, just a way to make money, I'm sorry but you should find something else to do. I've seen many developers that have that attitude and they just don't make it. They make mistakes and never learn from them, they have difficulties completing anything on time, and wind up getting fired quite a bit.

Never be afraid to try something new

While sometimes it's hard to break out of things you know that work, learning that things can/cannot be done another way expands your experience and keeps your mind trying to work outside the box.

Keep up with the Joneses

This one can be very hard if you have a full schedule. But trying to keep up with the new technologies will expose you to new ideas, new ways of tackling problems. One cannot grow if one does not see another way.

Pursue certifications

Some people see certifications as a croc. They see that lots of tech schools teach for the test and thats it. I can understand that. I have persued certifications as a professional goal. Going through the MCTS certifications for ASP.NET and Winforms has exposed me to many things I probably wouldn't have known about in .NET. I try to read through the entire training kit books MS puts out for the certifications.

Gaining the certifications are also good for ones ego. Having the goal to set out and earn the certification can keep you discleplined and give you a sense of accomplishment. It's something you can show your bosses and peers that you do indeed know what you're talking about. It gives you more bargaining power when negotiating a raise or looking for a new job. While some may not help you as much as you'd hope, having both real world experience and certifications can be a 1-2 combo.

Microsoft's certification track is the best I've seen. You can start out by getting Technological specific certifications with the MCTS series. You can get just certified in ASP.NET, Windows Applications, Windows Mobile.. and on. Then you can expand on those with Professional certifications with the MCPD's. These ones include application lifecycle components. Professional ways of breaking down an application into logical, component and class diagrams to implementation of architectures and finishing with deployment and maintenance.

Learn new languages

This one can be hard for people that only use one language at work. Learning a new language again helps you get exposed to new ideas and ways of tackling problems.

The hardest thing is actually finding something to do in that language to learn it. I've found that the best way is to have something you implement in every language. Whether it's some tool you've developed or a little game. I had the Al Bhed translator. I originally developed it in PHP to help me out with the game Final Fantasy 10. I then tried doing it in JavaScript, then Java, then C++, XUL (firefox extension) and then in C#.NET Winform. It gave me a common goal in each of the languages and forced me to find out how to do it in each one. They all had their own strengths and weaknesses. PHP's Array functions made some of it very easy to implement, where javascript was a bit clunky. C# wasn't as elegant as the PHP but it was much faster in execution.

Try to find languages that have a different goal than just another application. Languages like F# for example. While it's possible to write every day applications with it, it's real power lies in analyzing and transforming data. Learning it opens up a world of mathmatics in new ways.

Learn new platforms 

Simply this means do not just be a web developer or an application developer. Continuously expanding the platforms you know can increase your understanding of how other people develop. I started out as a web programmer and over the years I've written programs for the commandline, windows services, web services, mobile phones, automated scripts, desktop applications, rich internet apps (flash, silverlight), dynamic web sites and more. I'm currently picking up XNA and trying to learn what programming a game is like. I'm learning a newfound respect for game programmers.

Seek out challenges

If your current job is not challenging you find a way to be challenged. Sometimes even playing around things outside of work is enough. Go to your boss and see if theres ways you can increase your workload, or get into more complex projects. Express an interest in new things. If you are stuck at a job where that is impossible, I recommend it may be time to look elsewhere. Get some certifications and new skills under your belt and start looking. There are some great tech jobs out there, good places to find a place to be challenged are in start ups or smaller custom software companies. Places like where I work at, Sierra Bravo. When I joined I had 6 years experience but still would consider myself a novice programmer. Within the next year I worked on more than 20 projects, on many platforms and felt like I had finally become a professional developer.

Now being on the verge of 4 years later I feel like I'm an accomplished developer, I'm able to start incorporating more advanced concepts in to my projects. I can see a much larger picture. As the years have gone on the scope of the picture I can see has expanded. I once could only see the single web page I was working on. Then I was able to start understanding the system of web pages I would design. I then started graduating onto being able to write a complete web site. That is when I decided that desktop applications were next. The scope expanded to understanding state, threads, and desktop experience. My first applications were cluttered, did not have much for layers. I started to learn more about the concept of breaking out applications into libraries and layers. These days I've graduated to an architect that can take the project proposal. Break it down to sections, logical components and build the applications architecture. Define the database, the data layers, the data objects and the front end. I can incorporate n-tier designs, abstract out business logic to a web service. Write API's that work for websites as well as windows mobile.

Really it all boils down to is: Have passion for your job, passion to learn new things and keep yourself challenged. Never be afraid to learn new skills. In this day an age a developer needs to know more than just one platform. They need to understand a larger scope to make it.

Comments [0] #      blog  |  kick it on DotNetKicks.com
by Jeff Klawiter - Monday, September 29, 2008 1:14:14 PM (Central Standard Time, UTC-06:00)
Ok in the last few years I've had to take over a number of websites written in VB.NET, or I should say written in VB but somehow using .NET. My issue really isn't with VB.NET itself. It's obvious the developers that did these sites (none of them affiliated in any way) came from doing classic ASP and moved onto ASP.NET. The problem is they continued with their classic ASP practices and did not seem to come close to grasping what ASP.NET really was.

Issues I've had:
  • None of the sites will compile in Visual Studio
  • All of the sites were running in ASP.NET 2.0 but none used master pages. They all used Server-Side Includes (which should be banned) to include common blocks.
  • No use of controls or classes.
  • No use of code-behind
  • All SQL is dynamic and full of SQL Injection vulnerabilities
  • All used include files which set up global variables and functions to be used in the page lifecycle (one of the main reasons the sites wont compile)
  • One of the sites had every folder set up as it's own application in IIS. Their hosting provider has no interface for even seeing this, the developer used to call in to have this set up. More on this site later (refered to as BS1)
  • Most of the sites only make use of the most basic controls
  • One site (BS1) tried to use more advanced controls like Formview but failed miserably. In this instance, used all over the site, the developer made a formview, used an EditTemplate and bound a dataset to the formview. Sounds decent so far right? The issue came in after I made some changes that my data was not being updated in the dataset. Tracking down the issue was very hard to do when you have no "Go to Definition" and there are tons of "Include Files" that are copied and pasted throughout the site. I found that there was a  "Magic" function that did what databinding is supposed to do. It looked for any controls in the formview that matched the exact name of a column and bound the data.
  • BS1 includes so many functions that recreate .NET functionality that his developer seemed to give up on getting to work within an hour or trying. I want to go home sick everytime a change comes up on this site.
Basically on every one of these sites we have pitched rewrites. Most of them on grounds of security, which is completely true. One of the sites we've had to take down due to rampant SQL Injections. It wont be up until the client decides on which overhaul to rewrite proposal they want to to sign off on.

If the BS1 rewrite ever happens I hope I am not the one to do it. While the site needs it, the business rules and database behind it are majorly complex and in some points completely bonkers. Redesigning the database will be completely out of the question due to the amount of time to migrate that and other business apps the client uses to work with it.

After all of the things I've seen I'm scared of how many classic ASP developers have moved to ASP.NET without taking the time to understand what ASP.NET (or even .NET) is all about. The thing that scares me the most is that it has been 6 years since .NET was released. Some of the sites were even written starting with ASP.NET 2.0. Yet the developers probably used Visual Studio as nothing more than a bloated notepad.

I came from doing site development in PHP 3/4. Classic ASP and PHP 3/4 development were alike in many ways. One would use server side includes to include headers/footers for the page. There would more than likely be a main include script that set up all the functions/classes and global variables to be used on the page. All the HTML was generated with inline code. I can understand moving from an environtment like that to ASP.NET you may start out recreating what you know.

What I don't understand why these developers never felt a need to move beyond that when there is overwhelming evidence that ASP.NET can do so much more. The old way is horrible to maintain, can take a long time to develop and there are much better practices out there. While I was a PHP developer I started using Smarty templates. I loved having templates that abstracted the presentation from the real code. Smarty has become one of the defacto standards in PHP development over the years. It gave me the introduction to using "controls" and "master pages". It made reuse easier and presentation cleaner. To go so long continuing the same bad practices is unfathomable to me.

Another thing that amazes me is ASP.NET actually works with server side includes. The dynamic compiler for the site will included the code and build a DLL for the page. I'm a bit pissy at MS for even allowing this. The included code is compiled into every DLL and whenever a change is made every one of those pages must be recompiled.

Comments [0] #      Rant  |  kick it on DotNetKicks.com
# Wednesday, September 24, 2008
by Jeff Klawiter - Wednesday, September 24, 2008 8:49:02 AM (Central Standard Time, UTC-06:00)
My company just launched a new Recruitment site http://www.nerdery.com.

We are willing to pay you $50 for sending in a decent code sample and making it to the interview. I personally love working here, I get paid to learn. As I've said before I felt like I learned more in the first year working here than in the 5 years I did prior. The availability of so many cutting edge projects keeps us always on our toes. When I joined we had 11 employees, now we are up to 75 and are still going strong. The demand for us keeps going up and we are adding new partners in other parts of the US to keep it that way.

The site has some testimonials and videos . Alas I was on vacation when they did the videos so I didn't get my butt on there.

If you're looking for a fast paced job in the programming industry and live in the midwest, come check us out.


Digg!

Comments [0] #       |  kick it on DotNetKicks.com
# Friday, September 19, 2008
by Jeff Klawiter - Friday, September 19, 2008 8:42:40 AM (Central Standard Time, UTC-06:00)
The company I work for, Sierra Bravo Corp, got its start connecting legacy PICK systems to the modern world. They accomplished this via a proprietary client-server protocol we internally call db_server (Official name is SierraDBC or BravoConnecter). Sure there are other PICK connection technologies out there none of them support multiple PICK systems and OS Platforms. Currently we support : D3, Universe, UniData, JBase, MvBase and others I don't remember right now.

I maintain the .NET client library. It was originally a port of the Java library which was a port of the COM library. It's come quite a long way since then. Recently we've found the need for compression to be added for some of our client. One in particular has many employees out in the field connecting via AirCards where bandwidth availability can be a problem. On top of that the application needs to retrieve quite a bit of real time data from the home office. Retrieving 2mb of client history is not unheard of.

After the server developer added compression to the result stream I started working on the client end. I figured it should be a snap. If we have compression turned on, just pass in a DeflateStream (fed by the NetworkStream of the TcpClient connection) to the StreamReader we already use to retrieve the results.

StreamReader ResultReader;
if (IsConnectionTypeSet(ConnectionOptionTypes.EnableCompression))
{
    DeflateStream dfs = new DeflateStream(ns,CompressionMode.Decompress);
    ResultReader = new StreamReader(dfs, Encoding.ASCII, false);
}
else
{
    ResultReader = new StreamReader(ns, Encoding.ASCII, false);
}
One would think that's all that it would take, right?

I was sadly mistaken. I would receive an exception (System.IO.InvalidDataException:"Block length does not match with its complement.") every time I would try to read from the stream.

The issue lies in the use of zlib for the compressed stream. zlib and DEFLATE use the same algorithm for compression. The difference is zlib sends two bytes of header data. So all the answers I found were to pop off the first two bytes of the stream.
StreamReader ResultReader;
if (IsConnectionTypeSet(ConnectionOptionTypes.EnableCompression))
{
    DeflateStream dfs = new DeflateStream(ns,CompressionMode.Decompress);
    ResultReader = new StreamReader(dfs, Encoding.ASCII, false);
    ns.ReadByte();
    ns.ReadByte();
}
else
{
    ResultReader = new StreamReader(ns, Encoding.ASCII, false);
}
 This worked just fine but I was annoyed about how ugly it looked. I needed to move that ugliness out of there. So I wrote a ZlibStream class to do this for me. I made it for only Decompressing streams since I didn't have the budget to go and actually implement the zlib headers.

[ZlibStream.cs]
    /// <summary>
    /// Supports decompressing a DeflateStream created by the zlib library
    /// </summary>
    class ZlibStream : DeflateStream
    {
        #region Fields

        private bool HasRead = false;

        #endregion

        #region Constructors
        /// <summary>
        /// Initiates ZlibStream in Decompress mode
        /// </summary>
        /// <param name="stream">One of the System.IO.Compression.CompressionMode values that indicates the action to take</param>
        public ZlibStream(Stream stream)
            : base(stream, CompressionMode.Decompress)
        {

        }
        /// <summary>
        /// Initiates ZlibStream in Decompress mode
        /// </summary>
        /// <param name="stream">One of the System.IO.Compression.CompressionMode values that indicates the action to take</param>
        /// <param name="leaveOpen">true to leave the stream open; otherwise, false.</param>
        public ZlibStream(Stream stream, bool leaveOpen)
            : base(stream, CompressionMode.Decompress, leaveOpen)
        {

        }
        #endregion

        #region Public Methods

        public override int Read(byte[] array, int offset, int count)
        {
            if (HasRead == false)
            {
                this.BaseStream.ReadByte();
                this.BaseStream.ReadByte();
                this.HasRead = true;
            }
            return base.Read(array, offset, count);
        }

        #endregion

    }
As you can see it is very simple. Since all the StreamReader does is call the Read method I just needed to remove those bytes during the first pass.  This class is extremely simple and limited in its functionality. Doing Asynchronous reads with BeginRead will not work. I checked via reflector and it does not use the DeflateStream.Read method. It handles the BaseStream's Read methods on its own.

The one thing I may need to expand is doing the popping of the two bytes. Currently it's not checking to see if the stream has any bytes to read. So far in testing this hasn't been a problem. I'm going to try and see if I can create a situation where the bytes are not available initially. I have a suspicion that the ReadByte() waits for a Byte to be available and errors out on the Timeout value

So my result is as elegant as can be
StreamReader ResultReader;
if (IsConnectionTypeSet(ConnectionOptionTypes.EnableCompression))
{
    ZlibStream dfs = new ZlibStream(ns);
    ResultReader = new StreamReader(dfs, Encoding.ASCII, false);
}
else
{
    ResultReader = new StreamReader(ns, Encoding.ASCII, false);
}

Comments [0] #      C#  |  kick it on DotNetKicks.com
# Saturday, August 23, 2008
by Jeff Klawiter - Saturday, August 23, 2008 2:39:02 PM (Central Standard Time, UTC-06:00)

Over the summer I was able to run my first large project in .NET 3.5. I had a chance to put to use all the new features and learned quite a bit on the way. I've blogged a bit about this project before. The project contained 2 data backends a local SQL database and a 3rd-party ASMX service. I took the approach of having a business object library that contain only class definitions that I had full control over. I split out the backends to their own libraries with a main Datalayer library that handled the communication with the two underneath it.

Initially I started coding the two bottom layers to do the object instantiation inline in the LINQ queries. As the layers grew I began to refactor much of the instantiation to methods that mapped the layer objects to the business objects.

Converting an inline expression like this

var result = from id in dc.OrderDetails
             where id.OrderID == OrderId
             select new DataObjects.OrderItem()
             {
                 PartID = id.PartID,
                 Price = id.Price,
                 Quantity = id.Quantity
             };

To this

var result = from id in dc.OrderDetails
             where id.OrderID == OrderId
             select MapOrderDetailToOrderItem(id);

Using the LINQ to SQL Classes was a perfect fit. They are by far much easier to use as an ORM than SQL Datasets and DataAdapters are. When retrieving lists such as items on an order the relationship propertis on the SQL objects made it extremely easy to have clean data access.

After completing the project I started thinking about what the performance impact of the refactoring had on the data access. So I decided to run some tests. Initially I thought that the inline instantiation would probably be faster since it was constructing the object in the expression instead of getting the SQL objects and then passing it to a function.

I wrote a quick program to do some performance testing on both implementations. I set up examples using a common real world call. Retrieving an order from a database with the line items on the order Below you can see the inline call and the refactored call.

[Tests.cs]
    static class Tests
    {
        public static void RunInlineInitialization()
        {
            using (SqlOrdersDataContext dc = new SqlOrdersDataContext())
            {
                var result = from o in dc.OrderHeaders
                             select new DataObjects.Order()
                             {
                                 CustomerID = o.CustomerID,
                                 OrderDate = o.OrderDate,
                                 OrderID = o.OrderID,
                                 OrderTotal = o.Total,
                                 ShippingAddress1 = o.ShippingAddress1,
                                 ShippingAddress2 = o.ShippingAddress2,
                                 ShippingCity = o.ShippingCity,
                                 ShippingDate = o.ShippingDate,
                                 ShippingMethod = o.ShippingMethod,
                                 ShippingState = o.ShippingState,
                                 ShippingTotal = o.ShippingTotal,
                                 ShippingZip = o.ShippingZip,
                                 SubTotal = o.SubTotal,
                                 TaxTotal = o.TaxTotal,
                                 TrackingNumber = o.TrackingNumber,
                                 Details = o.OrderDetails.Select(od => new DataObjects.OrderItem()
                                 {
                                     PartID = od.PartID,
                                     Price = od.Price,
                                     Quantity = od.Quantity
                                 }).ToList()
                             };
                DataObjects.Order order = result.FirstOrDefault();
            }
        }
        public static void RunRefactoredInitialization()
        {
            using (SqlOrdersDataContext dc = new SqlOrdersDataContext())
            {
                var result = from o in dc.OrderHeaders
                             select MapOrderHeaderToDataObjectOrder(o);
                DataObjects.Order order = result.FirstOrDefault();
            }
        }

        private static LinqTest.DataObjects.Order MapOrderHeaderToDataObjectOrder(OrderHeader o)
        {
            return new DataObjects.Order()
            {
                CustomerID = o.CustomerID,
                OrderDate = o.OrderDate,
                OrderID = o.OrderID,
                OrderTotal = o.Total,
                ShippingAddress1 = o.ShippingAddress1,
                ShippingAddress2 = o.ShippingAddress2,
                ShippingCity = o.ShippingCity,
                ShippingDate = o.ShippingDate,
                ShippingMethod = o.ShippingMethod,
                ShippingState = o.ShippingState,
                ShippingTotal = o.ShippingTotal,
                ShippingZip = o.ShippingZip,
                SubTotal = o.SubTotal,
                TaxTotal = o.TaxTotal,
                TrackingNumber = o.TrackingNumber,
                Details = MapOrderDetailToDataObjectOrderItem(o)
            };
        }

        private static List<LinqTest.DataObjects.OrderItem> MapOrderDetailToDataObjectOrderItem(OrderHeader o)
        {
            return o.OrderDetails.Select(od => new DataObjects.OrderItem()
            {
                PartID = od.PartID,
                Price = od.Price,
                Quantity = od.Quantity
            }).ToList();
        }
    }

As you can see both public methods do the same thing. The second test was refactored easily using the Refactor-Extract Method menu item in Visual Studio. I load the orders from the database, take the SQL OrderHeader object and map it to the business object. The Details property on the Order object is simply a generic list of OrderItems. To retrieve them I do a quick lambda expression to query the OrderDetails relationship property. 

For some the refactoring goes without saying. Modularizing code like this makes it more maintainable and reusable. This concept can be foreign to some procedural programmers. With Visual Studio and addins like Resharper refactoring becomes so easy it's almost an afterthought to do it. For anyone that still doesn't see the benefit with refactoring, I hope this article will help you.

The testing program is pretty simple, pass in the amount of iterations and a boolean to turn pre-JITing on or off.

[Program.cs - (some code removed for brevity)]
        static System.Diagnostics.Stopwatch stp = new System.Diagnostics.Stopwatch();
        static int Runs = 10;
        static bool PreJitRoutines = false;
        
        static void Main(string[] args)
        {
            ProcessCommandLineArguments(args);

            //Lets get JIT over all the methods in question
            if (PreJitRoutines)
            {
                PreJitTestRoutines();
            }
            //Display Current Selected Options
            Console.WriteLine("Number of Runs: {0}", Runs);
            Console.WriteLine("Pre JIT Enabled: {0}", PreJitRoutines);
            
            //Run and Measure Inline Test
            stp.Start();
            for (int i = 0; i <= Runs; i++)
            {
                Tests.RunInlineInitialization();
            }
            stp.Stop();
            //Display Test Results
            Console.WriteLine("Inline Initialization Test: {0} , Average: {1}", stp.Elapsed, new TimeSpan(stp.ElapsedTicks/Runs));
           
            //Save Result for later calculations
            TimeSpan FirstRun = stp.Elapsed;
            
            //Reset StopWatch
            stp.Reset();

            //Run and Measure Refactored Test
            stp.Start();
            for (int i = 0; i <= Runs; i++)
            {
                Tests.RunRefactoredInitialization();
            }
            stp.Stop();
            //Display Refactored test results
            Console.WriteLine("Refactored Initialization Test: {0} , Average: {1}", stp.Elapsed, new TimeSpan(stp.ElapsedTicks / Runs));
            
            //Perform and report comparisons between tests
            if (FirstRun.CompareTo(stp.Elapsed)<0)
                Console.WriteLine("Inline Construction Faster: {0:f}", stp.Elapsed.TotalMilliseconds / FirstRun.TotalMilliseconds);
            else
                Console.WriteLine("Refactored Construction Faster: {0:f}", FirstRun.TotalMilliseconds / stp.Elapsed.TotalMilliseconds);

        }

I ran the tests in release mode with iterations of 1, 10, 100 and 1000.

>LinqTest.exe 1  true
Number of Runs: 1
Pre JIT Enabled:  True
Inline  Initialization Test: 00:00:00.0124051 , Average: 00:00:00.0177619
Refactored  Initialization Test: 00:00:00.0121951 , Average: 00:00:00.0174613
Refactored  Construction Faster: 1.02


>LinqTest.exe  10 true
Number of Runs: 10
Pre JIT Enabled:  True
Inline  Initialization Test: 00:00:00.0701888 , Average: 00:00:00.0100497
Refactored  Initialization Test: 00:00:00.0650985 , Average: 00:00:00.0093209
Refactored  Construction Faster: 1.08


>LinqTest.exe  100 true
Number of Runs:  100
Pre JIT Enabled:  True
Inline  Initialization Test: 00:00:00.6291376 , Average: 00:00:00.0090081
Refactored  Initialization Test: 00:00:00.5354964 , Average: 00:00:00.0076673
Refactored  Construction Faster: 1.17


>LinqTest.exe  1000 true
Number of Runs:  1000
Pre JIT Enabled:  True
Inline  Initialization Test: 00:00:06.2699034 , Average: 00:00:00.0089773
Refactored  Initialization Test: 00:00:05.3725538 , Average: 00:00:00.0076925
Refactored  Construction Faster: 1.17

As you can see at 1 iteration there's barely a difference. As we move up the scale the refactored code does consistently outperform the inline expression. This outcome was different from my initial hypothesis. I decided to dig a bit deeper and find out why. So I pulled out ILDasm to see what was going on. I was surprised to see that the IL generated for inline test was twice as long as the refactored test. Looking at the code it became clear what was going on.

Inline IL
 IL_0042:   stloc.3
 IL_0043:   ldloc.3
 IL_0044:   ldc.i4.0
 IL_0045:   ldtoken    method instance void  LinqTest.DataObjects.Order::set_CustomerID(int32)
 IL_004a:   call       class [mscorlib]System.Reflection.MethodBase  [mscorlib]System.Reflection.MethodBase::GetMethodFromHandle(valuetype  [mscorlib]System.RuntimeMethodHandle)
 IL_004f:   castclass  [mscorlib]System.Reflection.MethodInfo
 IL_0054:   ldloc.2
 IL_0055:   ldtoken    method instance int32  LinqTest.OrderHeader::get_CustomerID()
 IL_005a:   call       class [mscorlib]System.Reflection.MethodBase  [mscorlib]System.Reflection.MethodBase::GetMethodFromHandle(valuetype  [mscorlib]System.RuntimeMethodHandle)
 IL_005f:   castclass  [mscorlib]System.Reflection.MethodInfo
 IL_0064:   call       class  [System.Core]System.Linq.Expressions.MemberExpression  [System.Core]System.Linq.Expressions.Expression::Property(class  [System.Core]System.Linq.Expressions.Expression,
 class  [mscorlib]System.Reflection.MethodInfo)
 IL_0069:   call       class  [System.Core]System.Linq.Expressions.MemberAssignment  [System.Core]System.Linq.Expressions.Expression::Bind(class  [mscorlib]System.Reflection.MethodInfo,
 class  [System.Core]System.Linq.Expressions.Expression)
Refactored IL
 IL_0005:   stloc.0
 IL_0006:   ldloc.0
 IL_0007:   ldarg.0
 IL_0008:   callvirt   instance int32 LinqTest.OrderHeader::get_CustomerID()
 IL_000d:   callvirt   instance void  LinqTest.DataObjects.Order::set_CustomerID(int32)

The inline call uses reflection on the objects to build the instantiation into the expression tree. It has to load the information about the SQL OrderHeader.CustomerID property via reflection. It then does the same thing for Order.CustomerID on the business object. After that it takes the value loaded from the sql object and binds it to the business object. 

The refactored code skips the reflection entirely. Since the method is expecting an order object LINQ to SQL just needs to do what it does best, load data from the database and map it to the ORM object. The refactored methods just need to do straight property assignment

Now most of this testing was doing with the C# LINQ syntax and not the chained function calls. I'm going to dig a bit deeper and recreate this with pure lambda expressions and see how that stacks up. I have a feeling they will probably perform close to the refactored examples

Another thing that makes me curious is the plateau reached and the differences between 1 to 100 iterations. I have a sneaking suspiscion that some of this maybe related to the JIT compiler and the garbage collector optimizing for the pattern of execution. I'll probably throw in some garbage collection counters to see how different they are.

So for now the moral of the story is refactoring is not only good for code reuse, simplicity it can also help increase performance. Without moving the mapping into another method other calls would have increased JIT time due to having more code than needed. The mapping call only needs to be JITed once.

kick it on DotNetKicks.com
Comments [2] #      LINQ | Performance  |  kick it on DotNetKicks.com
# Friday, August 22, 2008
by Jeff Klawiter - Friday, August 22, 2008 12:58:20 PM (Central Standard Time, UTC-06:00)
I figured I should mention this. I will be speaking at the Twin Cities Code Camp this fall. My talk is going to be on the PICK/Multivalue systems that are still in use today. It is one of those legacy systems like COBOL that isn't going to fade away anytime soon. IBM actually has taken the lead with it in recent years but modernizing the U2 products (Universe and Unidata).

For the people that don't know Code Camp is a day of talks on whatever speakers want to talk about. It's free and a rather fun time. In the past there have been talks ranging from starting out in ruby, writing custom modules for IIS 7 to hacking a hardware platform to tell you the whether. It is run by Magenic and hosted at New Horizons in Edina (I heard that the next spring one may be moving elsewhere).

Comments [0] #       |  kick it on DotNetKicks.com