Archive for the Programming Related Category

Typemock’s ASP.NET Bundle released for the masses (and there was much rejoicing!)

Unit Testing ASP.NET? ASP.NET unit testing has never been this easy.

Typemock is launching a new product for ASP.NET developers – the ASP.NET Bundle - and for the launch will be giving out FREE licenses to bloggers and their readers.

The ASP.NET Bundle is the ultimate ASP.NET unit testing solution, and offers both Typemock Isolator, a unit test tool and Ivonna, the Isolator add-on for ASP.NET unit testing, for a bargain price.
Typemock Isolator is a leading .NET unit testing tool (C# and VB.NET) for many ‘hard to test’ technologies such as SharePoint, ASP.NET, MVC, WCF, WPF, Silverlight and more. Note that for unit testing Silverlightthere is an open source Isolator add-on called SilverUnit.

The first 60 bloggers who will blog this text in their blog and tell us about it, will get a Free Isolator ASP.NET Bundle license (Typemock Isolator + Ivonna). If you post this in an ASP.NET dedicated blog, you’ll get a license automatically (even if more than 60 submit) during the first week of this announcement.
Also 8 bloggers will get an additional 2 licenses (each) to give away to their readers / friends.
Go ahead, click the following link for more information on how to get your free license.

Thoughts on Unit Tests for Stored Procedures

So very recently I was approached by a team member to write some unit tests around a stored procedure that was being modified.I have never done any unit tests against for a Stored Proc so I was intrigued. I wasn’t really sure where I was going when I started but I opted to write a small console app in C# to proof it out.

For the sake of discussion, let’s say the stored procedure returns back a record-set with a single record containing five columns which describe time-zone information for a given US zip code and takes a single parameter, the zip code. I really couldn’t ask for a better test subject for a first pass, not too big, not too complicated.

My first inclination was to approach it like unit testing any other unit of code. So I figured I would need some variety of data store for my expected values and stored proc inputs. At this point it occurred to me, “Hey, I have a database full of inputs and expected results right here”, so I crafted a quick query to get my expected info. From that, I call the stored proc for each zip and verified the results. Worked like a champ. However, I was unaware the sp code refactoring had already been done on the database I was working against. It was around this point that I started getting a sinking feeling that something wasn’t right with the test. I attributed it to not being sure what to expect as this was a new testing scenario for me (typically the database developers are in charge of their development and testing of their code, while I can write SQL with the best of ‘em, a DBA I am not) and moved on.

Well ok, I have the behavior of the new code, the Actual results in testing terms. Now I can simply aim my program at the old database with the old code-base and get the expected results and modify my app to compare them. And then I discovered that the underlying data structure for the old code was different. The old way, the SP selects the five columns I need by joining three tables and then limiting on the passed in zip code. The new way, a nightly job is scheduled to populate a new “Time Zone” table and the SP simply selects all columns from the new table limiting by zip code.

So finally, it hits me… My test as it stands really is only testing that I get a result for every zip code I ask for and that it selected the same row that I got my expected result from. And maybe, that’s exactly what it should be testing, it does add some value… And yet it feels empty.

What I ended up doing was to retrieve the master list of zip codes from the old way’s table and ran both the new and old SP for each zip code and compared the result. This test felt better, the underlying data structures driving the stored proc could change and the test would be isolated and continue to work as long as the SP’s calling signature stayed the same.

The rub is, I could not have written my new test app without the new SP being developed. So what would I have been testing in it’s first iteration had I wrote tests for it at the time? I would have written my original test that would ultimately grab the expected data from the same database that the stored proc would.

What I should have done was adapt my initial test to work the same way with the old data and verified the stored proc returned the right data in my five columns. Then I should add a a test to verify the data in the new table matches the data from the old data structure. (This test typically would be the responsibility of the db job developers). Then I should modify my test to get it’s expected data from the new (and tested) structures and continue to verify the SP results to the data.

When unit testing code, you tend to have to “mock” out the data calls to control your tests, so from having to avoid all the database calls, it’s a “context-switch” to be requiring the database for everything. But the constant in both is testing the discrete parts from the bottom up. In retrospect, I could have done the test completely with SQL and suddenly the MS “Database Unit Test” makes a lot more sense.


Regards!

 

Typemock, my new best friend!

In particular, Typemock Isolator.

 Typemock fan

DISCLAIMER: I am in no way affiliated or receiving any compensation from the makers of Typemock. Simply a developer in the trenches loving this tool in his tool box. From thier website: http://www.typemock.com/index.php

  Isolate any .NET dependencies to make unit testing easyWhat does Isolator do?Typemock Isolator gives .NET developers the power to easily perform unit testing by making unit tests easy to write and automate.Isolator improves the bug-fix-time factor, and increases your code coverage.

 

I’m not really going to review it, but it is an excellent product for use in writing unit tests. Plays 100% with NUnit. This is simply a no brainer, a tool you must have as a professional developer working with .NET.

If you are familiar with unit testing, then you have run into the "Mock” object. It looks and works just like the real thing, but it’s not, it’s a fake that does your bidding so you can control the outcomes of the tests to hit the proper scenarios for verification. And for small loosely coupled objects, this works with the added overhead of making the mock objects. But invariably you will end up with a data-bound object or a class sub-classed 3 levels deep and controlling the testing inputs gets harder and harder to control.

There are many development patterns out there that help to mitigate this “cost” such as factories with delegated constructors to allow snapping in of the data-provider, but these quickly become very complex beasts, and the codebase is just as large as the production code. Yes it works, yes it’s thorough, but is there a better way?

Yes! Why just today, I was creating some tests around a subsystem that essentially picks what will be shown to the user based on various rules. We are re-using the subsystem with another application and since the codebase is well set up and shared between like applications, with proper testing in place, we will be able to re-factor the code to be used in both spots with much higher confidence.

For one scenario, a random number between 0 and 99 is picked values less then 50 see A and the values greater then 50 see B. Well to right a test for this, you really need a consistent value returned from the random number generator. In my case, the random number was being returned from a private static little wrapper function around Random.GetNext() which I wil call GettRandomInt for namesake.

Typemock Isolater allowed me to intercept the call to GettRandomInt and force a return value of whatever I need for that test to be able to fully test my scenario. In a single line of code. That’s right 1 DAMN LINE OF CODE, I almost cried. Mind you I got lucky in that it was a static function so I never needed an instance, but at worse, 2 lines of code. Not too bad. And it was a private member as well. It has always been a source of frustration to only be able to test public functions, or to break ideal object model integrity for testing purposes alone. I mean of course quality is the clear choice here, but it isn’t free.

I was just very impressed with the product through a single day’s use. If you have ever “mocked autoquote” (and you know who you are (-: ) I guarantee, that’s right I said it, GUARANTEE, your satisfaction! I will be buying the personal edition for myself to use on my own projects, and for $89.00 that’s quite affordable.

They also have SharePoint isolator which I have not used, but have worked with SharePoint. And I can only imagine how this would be an essential tool.

Anyway, thank you Rob Witt for showing me the light. Amazing!

Regards!

A foray into Flash

I recently had the opportunity to do a small enhancement to a web site. In particular a simple file upload “page” so that their logged in users can upload a pdf file to them. The site is a asp.net based site, so we could have used the asp file upload component, but it has no feedback while it’s waiting. So given the market share of Flash Vs. say JavaScript, Java, etc. We went with flash.

It was really actually a great project in that small and focused enough to be very manageable and I’d been looking for an excuse to play with it, but I’m not a graphics guy so I haven’t really had the opportunity to ever use it.

So, what I ended up doing was grabbing a file upload widget from FlahsDen ( http://flashden.net/item/upload-gadget/12202 ) It’s a fine widget and you get the source code. This particular component is made with the intent that it be embedded within a Flash based application. It’s quite configurable and had it up and working within a few hours of playing with it. Pretty good documentation to boot!

I embedded it within a very simple Flash data entry fields. The flow is, you fill out the form and the upload widget becomes usable and you upload the file. The page that accepts the incoming file and it’s accompanying form data then inserts the image as a binary blob in the DB and the values are inserted into the database as well.

When all is said and done it looks much like this (click for larger image):

flash-finished

The most difficult portion was writing the logic to control the date pickers, leap years and all. Actionscript 3 is essentially a strong type-able JavaScript. The syntax is slightly different but easy to get used to. It’s a little “sloppy” by which I mean undisciplined coding will lead to spaghetti code on the inside.

Once I got over the learning curve of Adobe Flash CS4, coding was nice. I used a programmatic event driven approach and it worked out really well. I was expecting a much more difficult time when I went to try sending the form data and the file upload which the purchased widget does via a FileReference. But it worked just like I wanted it to. Unfortunately the FileReference.Upload call throws away any http headers you may want to provide. I didn’t need that but ya never know.

Another interesting thing I found was that the combo box controls don’t initialize their selected index. I don’t know why I expected a fat 0 (zero) but it’s –1. I understand the ability to notice the default state’s value Vs.. a selected one, but for some reason that stuck with me.

In retrospect, the only thing I would have done differently was to make the form portion, straight asp.net dynamic html and disabled the flash uploader when the form wasn’t valid. Form values on the page are sent for the ride so I would never had to broker the user provided fields into the uploader to ride on it’s FileReference request.

At any rate, I’m left feeling intrigued and somewhat excited. I would like to work on some animation coding and perhaps pursue some more “real” custom flash based applications for clients.

I really wanted to hate it, and *hangs head in shame* kinda <3 it   :-o

–Regards!

Whoa! Here’s some bad code

This post has no basis in advice or analysis, I just was documenting an existing SharePoint site I sadly must maintain at work. If you’ve read my posts in the past, you’ll remember that I am not a big SharePoint fan. Well, this isn’t really SharePoint related (I just wanted to kick an app while it was down with that last comment) aside from a demonstration of how things can go horribly awry when one tries to mix development methodologies for SharePoint…

The application in question is a corporate use Room Reservation system. Oddly, the company uses Lotus Notes which has an ugly (though quite functional) room reservation system built right in. In my opinion, this never should have been created. The company is trying to get away from Notes but until they migrate the 1500 - 2000 custom lotus notes database applications to SharePoint, Notes is not going anywhere. It should have been one of the last items migrated and it should have just been migrated to Microsoft Exchange as it also has a good meeting scheduler.

At any rate, the reason for the post. Check out this code snippet from a C# 2.0 Web Service which runs under SharePoint. To be honest, until I saw this, I didn’t even know C# supported "goto" *slaps head*

And sadly that’s one of the more clear-cut code chunks up in there. This my friends is why we have Coding Best Practices and Code Reviews… DON’T LET THIS HAPPEN TO YOU (and you better pray I don’t ever catch you writing something like this, you will be flogged!)

            double num = -41.0;
            double num2 = 39.0;
            string str22 = str10;
            if (str22 != null)
            {
                if (!(str22 == "week"))
                {
                    if (str22 == "day")
                    {
                        str12 = "<Where><DateRangesOverlap><FieldRef Name=\"EventDate\" /><FieldRef Name=\"EndDate\" /><FieldRef Name=\"RecurrenceID\" /><Value Type=\"DateTime\"><Today /></Value></DateRangesOverlap></Where>";
                        goto Label_01A5;
                    }
                }
                else
                {
                    str12 = "<Where><DateRangesOverlap><FieldRef Name=\"EventDate\" /><FieldRef Name=\"EndDate\" /><FieldRef Name=\"RecurrenceID\" /><Value Type=\"DateTime\"><Week /></Value></DateRangesOverlap></Where>";
                    num = -8.0;
                    num2 = 15.0;
                }
            }
            goto Label_01BB;
        Label_01A5:
            num = -1.0;
            num2 = 1.0;
        Label_01BB:
            time2 = time.AddDays(num);
            DateTime time3 = time.AddDays(num2);
            string roomid = "0";

Some musings on Sharepoint

First let me say, if you’re looking for some tutorials or samples related to Sharepoint, this post isn’t for you ;-) but feel free to read on.

At my new employer’s, I have to do a fair amount of Sharepoint development. Well that phrase right there is just ridiculously large in scope. Allow me to elaborate.

I like to say, Sharepoint is to Web Sites as Microsoft Access is to Databases. Great great tools when used for their intended purposes. Very bad when attempting to stretch them to do things they just weren’t intended to do. Yeah, you can make a front end to your database with Access, but if you need scalable deployable solutions for an enterprise or mass user base… Well, that just isn’t how it’s done. When Sharepoint is used as a collaboration site, it is awesome. Easy to set up and perform basic customizations. It can be styled very nicely by graphic artists and web design folks but when you try to stretch it with configuration settings alone, you end up with hard to maintain ugly solutions.

There are two flavors of Sharepoint. One is Windows Sharepoint Services (WSS) and the other is the Microsoft Office Sharepoint Services (MOSS). WSS is a free "Sharepoint Server" (the SS in WSS and MOSS) [ download: http://www.microsoft.com/downloads/details.aspx?FamilyId=D51730B5-48FC-4CA2-B454-8DC2CAF93951&displaylang=en (requires Windows Server 2003 (you can grab a Sharepoint Virtual Machine for the Microsoft Virtual Server as well), search the download site for Sharepoint VHD] MOSS is built on top of WSS and contains many additional webparts (I’ll explain a little later) as well as the additional services and administration applications to create and manage web farms and multiple site collections. The webparts are the value-add. One such webpart is the Business Data Catalog which auto-magically allow various data sources (web services, databases, etc) to be used as a Sharepoint list while Sharepoint and the BDC do the heavy lifting of getting, caching, updating. etc.

Sharepoint has a few intrinsic "types" that one can work with on a SP site: Lists, Libraries, and Webparts.

A list is simply that, a list of "things". You can best think of these lists as database tables, though they are not stored as such. Each one comes stock with a few default columns such as created by, created date, last modified, that sort of thing. You then can add as many additional columns as you require to more properly define the "thing" in the list. Such as a list of customers, or regions, or support staff members, etc. These are all stored in a content database behind the scenes. If you modify this content database outside of using the Sharepoint provided tools and applications, you will void your support contract on that installation (which considering the MOSS retail server for enterprises in the tens of thousands of dollars in licensing, not something you want to void your warranty with!) These lists automatically have generated Create, Edit, and View pages which can be customized to further enhance working with them. And Sharepoint manages all this for you.

A Library is one of the default "applications" it provides. At it’s heart it is a list, but it has special coding to in the support pages for the list that allow it work with special types of lists. Sharepoint comes with a few built in libraries, such as a Document Library and a Picture Library. As I mentioned these lists are persisted in a database and since images and documents (such as word files, or pdfs) are binary files, you need to store them, retrieve them, and view them in special manners, depending on what type it is. The libraries isolate that and do it for you and provide you with thumbnails in the picture library views and open up word to edit word documents, and excel to edit spreadsheets. All very integrated with office (hence the MO in MOSS, though WSS has these libraries as well.)

A Webpart is essentially a WebControl (in asp.net terms). They are custom web controls that implement various Sharepoint API interfaces. Calendar Control? That’s a webpart.Ghant Chart? That’s a webpart. Anything you can code as a web control can be a webpart. Sharepoint comes with a lot of different ones and they are all pretty useful for their purpose.

The sticky wicket is that you can do a helluva lot without needing to write a webpart. If you’re doing Sharepoint development and not coding webparts, you are using MS Sharepoint Designer, a must have and of course, it is not free. But it does have a fine configurator for the DataView webpart. In a nutshell, the dataview interacts with some external source of data (web service, database, xml file on the files-ystem) and renders it to a list with a customizable XSLT. With something like that you can completely render the data with html markup to the page any damn way you want. You can even pass querystring parameters into it, pretty flexible and you can accomplish a lot of simple-ish apps this way)

I strongly suggest that anytime you discard all the other Sharepoint webparts and libraries to use dataviews and custom xslt, you INSTEAD develop your own custom webpart!! It will be far more maintainable in the end. And if it’s a really complex application that needs to interact with a lot of systems around the enterprise, make a standard asp.net application and host it in a iframe webpart. That is what Microsoft intended. However, most IT managers just want everything in Sharepoint and they want it in there now because Jim in accounting made his group’s Sharepoint site by himself in 2 days. Of course it’s just a few custom lists and no theming or workflows. And that’s great! But they don’t understand the nuances in the differences of the applications they want, it just gets lumped under Sharepoint and that is a MISTAKE!

More later, cross your fingers boys and girls, the rabbit hole goes pretty deep on this one I suspect!

–Regards!

Coding without comments and the ‘Single Use’ Principle

So it’s all the rage to talk about how comments are bad to use, bad to rely on, and should only be used to describe why something is being not done, not how. And for the most part I agree. As soon as a comment is written it becomes stale. It’s hard enough to get the time to update the code, much less updating the comment. With today’s integrated development environments with name completion and the like, there is no excuse for using abbreviated variable or function names in your code. This is not to say that variable names should go on forever, they should be as long as they need to be to express their purpose and NO LONGER!

 ’Fair nuff, no real problems here. Now, the Single Use Principle states that some “thing” should have exactly One purpose.  This “thing” is either an object, class, function, some item of encapsulation.

Again, who can argue with that, makes sense.

Now, my annoyance :-) (c’mon you knew it was coming) So ok, we have these functions which end up being relatively small and easy to read as we’ve emplopyed single use and descriptive variable/function names this is a good thing. The downside is, you now have class files with many many many small functions and getting a big picture view of what is actually happening becomes impossible… Instead of spending all your time figuring out what code does, you end up diagramming the call mapping of your functions so you can find out what is really being called, in what order. All these small almost atomic functions become more time wasting to follow then before we employed our “Best Practices” …

 Very disappointing folks. Got a good solution? Neither do I, but I suspect it comes in the form of more advances to the development environment. I would love to see expandable function calls in code so a simple expand click would show you whats actually going on without having to diagram it all out. I know visual studio has a code declaration window which is great but not quite what the doctor ordered.

At any rate, the best practices are worth it, but let’s not pretend they aren’t causing another, and perhaps larger, maintenance issue.

Regards!

Project References with Visual Studio

So, you’re writing an application, and you break down the functional parts to more base layer services and utility assemblies, and now you want to create a new application and re-use some of your code.

My “go to” answer has always been separate assemblies copied to a global project bin and then adding references to the bins in whatever application needs them. For example:

Example of a directory layout “Application 1″ and “Application 2″ represent two applications that are re-using logic. The “Libraries”directory off the root represents the project directory housing “Library 1″ and “Library 2″ which are the assemblies being re-used, they build to their respective bin directories. The “bin” directory off the root is the global bin where the binaries from “Library 1\bin\” and “Library 2\bin\” are copied to for re-use by the applications.

In the above scenario, the Applications reference the libraries from “monkk.com\bin” and as new versions of Library 1 and Library 2 are released, they can replace the existing ones when required.

The problem with this type of solution is that often times your libraries end up building on each other to create more complex libraries and soon you find yourself in an endless series of updating the various libraries for any new feature necessitated by the applications and so can lead to updating Application 2 because Library 2 had a for Application 1… Not Ideal!

So, what to do, what to do?!?

Well a technique I’ve now come to employ for just such scenarios is project references. I know, simple how could I not have thought of this? Well I had, but for some reason I’ve been ignoring them because it just didn’t seem “right” because of how intimately coupled the assemblies and projects can become but truth be told, it’s so natural (after being forced to use it) I don’t know how I ever got along without it. I suppose the problem has been, when you use 3rd party assemblies, it’s just not an options, and if all your library assemblies are supposed to be stand-alone assemblies, shouldn’t referencing them both be the same?

Well no, it makes complete sense to modify various Libraries while working on Application 1, and likewise for 2. There are some areas for concern, such as not breaking Application 2 while modifying Application 1 and it’s Library 2 use. But with a good set of Unit tests for both applications and the libraries themselves, you can do this with less risk. And of course best practices for published assemblies come into play such as adding new function signatures instead of changing existing ones (of re-factoring the internals in the process.)

This obviously applies more to companies writing their own frameworks where the libraries stay internal. My point is this doesn’t really scale to mass consumer use due to support issues. In that case, as I’m sure you are, each binary is it’s own and the extra work of version management of the application used assemblies, but such is life and it’s what we’re paid for so, enjoy!


Regards!

Goodbye TDD, Hello TDD …

Well a third TDD post and with mixed emotions I am here. On the one side, I have found BDD which (in my opinion) is far better then TDD.

What the h#ll is BDD you ask? Behavioral Driven Design. So what does that mean? It is development that is driven by the required behavior of your application(s). For example, if you need to write a online slot machine game, and the app you write behaves as an online slot machine game, you have successfully developed an online slot machine game. Ok, no duh! So what? How is that better then TDD where you would have written tests that would pass when you successfully written an online slot machine game?

Oooh, so go ahead and re-read that last question. Notice how it suggests that your artifact of the entire development process is a set of tests (and the implied working application), whereas the BDD version leaves you with a working application (and the implied tests).

That is the difference my friends, they are essentially the same but their terminology differs to emphasize what truly is important in the development process. The creator of BDD states that BDD is just TDD done right. And that’s relevant to the effect that when using TDD it is too easy to fall into the trap of focusing on the tests. Not to imply testing isn’t important, but they are a required evil in that if we never made mistakes, we would never need to write a single line of test code. As that’s pretty much impossible (being human) testing and QA aims to mitigate the risk of the human factor.

So I still haven’t really explained what the deal with BDD is, so here goes (and I apologize, an expert on the topic I am not.) To cite http://behaviour-driven.org/GettingTheWordsRight:

Behaviour Driven Development grew out of a thought experiment based on Neuro Linguistic Programming techniques. The idea is that the words you use influence the way you think about something.As an example, when I was first getting to grips with TDD, I was pairing with an experienced agile coach, writing little test methods, then writing the code, and generally feeling good about life. Then I went ahead and wrote some code without a test. The coach, JR, asked me why I’d written the code. I answered: “we’ll need it in a minute”, to which JR replied “yes, we might”. By using the word “might”, he introduced the possibility that we might not. As it turned out, we didn’t. - Dan North

As far as using it, there are a few implementations ( http://en.wikipedia.org/wiki/Behavior_driven_development) for writing tests using BDD. The most popular is rSpec ( http://rspec.info/ ) for the Ruby platform. There are severl for Java, two (mostly dead) for MS .Net Framework, and vaious others (python, PHP, scala). There is a Google Video by the rSpec creator which is quite interesting: http://video.google.com/videoplay?docid=8135690990081075324&q=behavior%20driven%20development&total=27&start=0&num=10&so=0&type=search&plindex=1

I haven’t had the opportunity to do anything with BDD, nor does it look like I will with the state of BDD as it stands today. I believe what has happened is that TDD ate it and we just have to make due. The tests we write should be behaviorial based even if the syntax of the testing domain language leaves us asserting like we’re writing procedural code instead of the OO stuff us enterprise type developers eat all day :-/ Please rSpec guys, port your code to .net :-))

A few weeks into a TDD style project

So, as you may have read in my previous rant regarding Test Driven Development, I am one of two developers participating in a test of TDD. The project is relatively small in scope and is mostly in a stand-alone “engine” so really, I think it’s a pretty ideal test subject. If you were paying attention, your spidey sense started tingling when you read “…an is *mostly* in a stand-alone engine…” Yes, well, as one might guess the mostly is now causing us some grief. The subsystem I am working on interfaces with the main application so that it knows when it has work to do. In the final design of the subsystem, it is a stand-alone windows service consuming application events from the event queue. Now, not only is this a test TDD project, but also trying to incorporate some Agile/Scrum techniques. I think maybe this was a mistake. Agile makes more sense when you have a team of greater then two. So really I think we are suffering an issue stemming from Iteration grouping. Our 1st iteration is quite large and provides the framework for the rest. However, we are not able to take advantage of smaller implementation items that can be pushed to later interactions. Which causes us to call my sub-system from within the application that writes to the event queue, instead of just reading the event queue.

Now, here’s where it really gets ugly. Due to how we have our name-spacing and library hierarchies, I have to move my subsystem completely out of the main project library and add it to the library code it interfaces with. And later I will have to move it back. The sub-system isn’t huge, but that’s a pain. 

And why didn’t we see this coming? Because we are implementing from what the tests dictate we need, not what we will need for the entire project. Some may argue that the tests apparently were not complete and I agree. But I think it’s a little unreasonable to know that from the test perspective until it’s discovered by actual implementation. And these happen late in the game so a change is not only a change to the implementation but to the entire underlying tests which defeat the purpose of starting with the tests.

I guess one might further argue that TDD is not a replacement for detailed design. But I have to wonder, if the design is indeed so detailed as to reveal this issue before any coding (and I have seen designs that would fit the bill, as well as designed some of that ilk for prior employers) that one would not need to start with the tests as testing techniques would have been built into the requirements and design. Of course if you can , why not as it prevents people from getting time crunched and not implementing tests at all. But isn’t the point of TDD to speed up and allow for iterative design (as well as pushing testing to the forefront, which I praise it for) ?

Regards!