Microsoft.Data.dll and LightSwitch

Microsoft has made some announcements over the last week or so.  The first was Microsoft.Data.dll.  I think that Oren adequately wraps up the feelings that I have towards it.

The second was the announcement of Visual Studio LightSwitch.  I have some strong feelings for this as well.  Microsoft is positioning this as a tool that allows non-professional developers create line of business (LoB) applications.  They suggest that it will allow these non-developers to create applications that can be handed off to IT for maintenance and further enhancement.  Since LightSwitch isn’t really Visual Studio, the IT group will have to upgrade the application codebase.  Does this sound like anything to you’ve ever heard of before?

While Microsoft won’t come out and say it, LightSwitch is positioned to fill the space that MS Access has for more than a decade.  During that decade plus IT departments and programmers world wide have grown to loathe MS Access application created by business.  Invariably those MS Access systems start live as small intra-department apps that service one to a few users.  Over time their feature set grows along with their user base.  At some point these LoB applications hit an invisible wall.  Sometimes it’s that the system has devolved into a mess of macros and VBA.  Other times they have collapsed under the pressures of concurrent users.  Regardless, the developers that take over these applications are met with a brownfield mess.  On top of that, the application has likely grown into a brownfield application that is critical to the business.  We professional developers end up picking up the pieces and, often, re-writing the application from scratch, under huge timeline pressure, so that it can meet the requirements and specifications that it has grown to need.

So back to LightSwitch.  Why is Microsoft pitching what this product is good at to us professional developers?  They say it’s not for us, but instead for non-professional developers.  Market it to them then.  Don’t waste our time with this marketing campaign.  Instead Microsoft, sell us on the part we’re going to have to deal with; the migration and fixing once these “LightSwitch” applications when the business inevitably comes running to us to do this.

To the professional developers that read this blog (most of you I’m guessing), prepare to move your hatred and loathing from MS Access to LightSwitch.

Making the most of Brownfield Application Development – Winnipeg Edition

On Friday July 23rd I’ll be in Winnipeg giving a one day seminar on the nuances of Brownfield Application Development and how to get the most out of it.  More about the day can be found here.  I recently did the seminar at the PrairieDevCon and it was a blast.  The day is filled chocka-block with content and ideas that pertain directly to Brownfield codebases and will work in Greenfield situations.

Registration can be found here and until July 2nd the session is available at a discount.  Hope to see you there!

Visual Studio Project files and coupling

The way that we’re told to use Visual Studio is that we create a solution file and add into it one or more project files.  Each project file then gets filled with different development artefacts.  When you build inside of Visual Studio each project represents a compiled distributable file (exe, dll, etc).  Many people carry this practice over into their build scripts.  You might be one of them.  I’m here to tell you why you’re wrong to be doing this.

Let’s say you’re starting a project.  You open Visual Studio, select File | New Project and get things rolling.  In a few minutes you have a Solution that contains a few Projects.  Maybe you have one for the UI, one for the business logic and one for the data access layer.  All is good.  A few months later, after adding many artefacts to the different projects, something triggers the need to split the artefacts into one assembly from one DLL into two DLLs. 

You set off to make this happen.  Obviously you need to add a new Project to your Solution, modify some references, and shift some files from one Project into another.  Say you’re stuck using an exclusive locking source control system (like VSS…shudder).  You *must* have exclusive access to all the files necessary including:

  • the sln so you can add the new project
  • at least one existing cs/vb/fsproj which you’ll be removing existing code artefacts from
  • any cs/vb/fs files that will be moved
  • any cs/vb/fs files that reference the ones moving (using statements will need updating when you change the namespacing on the files being moved)
  • possibly some resx files that need to be moved
  • possibly config files that need to be changed or moved
  • any automated tests that make use of the moving cs/vb/fs files

It’s a pretty damn big list of files that you will need to exclusively lock during this process.  Chances are you will need to push all of your co-workers out of the development environment so that you can gain access to all of those files.  Essentially you are, at this point, halting the development process so that you can do nothing more than split one DLL into two.  That in quite inefficient in the short term and it’s completely unsustainable in the long term.

I can hear you now, “Well I use <git/mercurial/svn/etc> so we won’t have those issues”.  Really?  Think it through for a second.  Go ahead, I’ll wait.

With the volume of changes that I listed above, you’ll likely want to be working in some kind of isolation, whether that is local or central.  So yes, you can protect yourself from blocking the ongoing development of your co-workers by properly using those version control systems.  But remember, you do have to integrate your changes with their work at some point.  How are you going to do that?  You’ve moved and modified a significant number of files.  You will have to merge your changes into a branch (or the trunk) locally or otherwise.  Trust me, this will be a merge conflict nightmare.  And it won’t be a pain just for you.  What about the co-worker that has local changes outstanding when you commit your merged modification?  They’re going to end up with a massive piece of merge work on their plate as well.  So instead of being blocked while you do the work, you’re actually creating a block for them immediately after you have completed your work.  Again, the easiest way to achieve the changes would be to prevent any other developers from working in the code while modifications are occurring.  Doesn’t that sound an awful lot like exclusive locking?

Now, I know you’re thinking “Pfft..that doesn’t happen often”.  This is where you’re wrong.  When you started that application development cycle (remember File | New Project?) you likely didn’t have all of the information necessary to determine what your deployables requirements were.  Since you didn’t have all of that information, chances were good, right from the outset, that you were going to be doing the wrong thing.  With that being the case, it means that chances were good that you were going to have to make changes like the one described above.  To me that indicates that you are, by deciding to tie your Visual Studio Projects to your deployables, accepting that you will undertake this overhead.

People, possibly you, accept this overhead on every software project they participate in.  This is where you’re wrong.  There is a way to avoid all of this, but people shrug it off as “not mainstream” and “colouring outside the lines”.  The thing is it works, so ignore it at your own peril.

There is a lot of talk in some development circles about decoupling code.  It’s generally accepted that tightly coupled code is harder to modify, extend and maintain.  When you say that a Visual Studio Project is the equivalent of a deployable, you have tightly coupled your deployment and development structures.  Like code, and as the example above shows, it makes it hard to modify, extend and maintain your deployment.  So why not decouple the Visual Studio Project structure from the deployables requirements?

It’s not that hard to do.  You’ll need to write a build script that doesn’t reference the cs/vb/fsproj files at all.  The .NET Framework kindly provides configurable compiler access for us.  The different language command line compilers (vbc.exe/csc.exe/fsc.exe) allow you to pass in code files, references, resources, etc.  By using this capability, you can build any number of assemblies that you want simply by passing a listing of artefacts into the compiler.  To make it even easier, most build scripting tools provide built in capability to do this.  NAnt and MSBuild both provide (for C#) <csc> tasks that can accept wild carded lists of code files.  This means you can end up with something like this coming out of a solution-project structure that has only one project in it:

<csc output="MyApp.DAL.dll" target="library" debug="${debug}">
  <sources>
    <include name="MyApp.Core/DAL/**/*.cs"/>
  </sources>
  <references>
    <include name="log4net.dll"/>
  </references>
</csc>

<csc output="MyApp.Core.dll" target="library" debug="${debug}">
  <sources>
    <include name="MyApp.Core/Business/**/*.cs"/>
  </sources>
  <references>
    <include name="log4net.dll"/>
    <include name="MyApp.DAL.dll"/>
  </references>
</csc>

<csc output="MyApp.UI.exe" target="winexe" debug="${debug}">
  <sources>
    <include name="MyApp.Core/**/*.cs"/>
    <exclude name="MyApp.Core/DAL/*.cs"/>
    <exclude name="MyApp.Core/Business/*.cs"/>
  </sources>
  <references>
    <include name="log4net.dll"/>
    <include name="MyApp.DAL.dll"/>
    <include name="MyApp.Core.dll"/>
  </references>
</csc>

Likewise, we could consolidate code from multiple projects (really file paths is what the build script sees them as) into one deployable.

<csc output="MyApp.UI.exe" target="winexe" debug="${debug}">
  <sources>
    <include name="MyApp.DAL/**/*.cs"/>
    <include name="MyApp.Business/**/*.cs"/>
    <exclude name="MyApp.UI/**/*.cs"/>
  </sources>
  <references>
    <include name="log4net.dll"/>  
  </references>
</csc>

Now, when it comes time to change to meet new deployables needs, you just need to modify your build script.  Modify the inputs for the different compiler calls and/or add new compilations simply by editing one file.  While you’re doing this the rest of your co-workers can continue doing what they need to provide value to the business.  When it comes time for you to commit the changes to how things are getting compiled, you only have to worry about merging one file. Because the build script is far less volatile than the code files in your solution-project structure, that merge should be relatively painless.

Another way to look at this is that we are now able to configure and use Visual Studio and the solution-project structure in a way that is optimal for developers to write and edit code.  And, in turn, we configure and use the build script in a way that allows developers to be efficient and effective at compiling and deploying code.  This is the decoupling that we really should have in our process and ecosystem to allow us to react quickly to change, whether it comes from the business or our own design decisions.

Rotating text using Graphics.DrawString

Recently I needed to create a custom WinForms label-like control that allowed for the text to be displayed in a rotated fashion.  Our needs were only for four rotation locations; 0 degrees (the default label position), 90, 180 and 270 degrees.  There were other complicating factors, but for this post we’ll only concentrate on this component of the control.

To rotate text using the Graphics.DrawString method you only have to do a couple of things.  First you have to use the Graphics.TranslateTransform method, then the Graphics.RotateTransform method, and followed by the Graphics.DrawString.  Here’s what it looks like.

using (var brush = new SolidBrush(ForeColor))
{
    var stringFormat = new StringFormat
                       {
                           Alignment = StringAlignment.Near,
                           LineAlignment = StringAlignment.Near
                       };
    e.Graphics.TranslateTransform(transformCoordinate.X, transformCoordinate.Y);
    e.Graphics.RotateTransform(rotationDegrees);
    e.Graphics.DrawString(Text, Font, brush, DisplayRectangle, stringFormat);
}

What you see are the three steps that I outlined above.  Let’s start at the bottom and work our way up.  The code exists inside of a UserControl’s overridden OnPaint event.  The DrawString method makes use of some of the properties on the control, like Text and Font.  It also uses the DisplayRectangle property to set the boundaries for the drawing to be the same size as the control.  This is one of the keys to making the rotations work.  The other key is to provide the DrawString with the StringFormat settings.  By setting them to be StringAlignment.Near for both the Alignment and LineAlignment, you are declaring that the text’s location should be based in the top left of the DisplayRectangle’s area.

Graphics.RotateTransform is how you set the rotation value.  In the case of our control, we would be putting in a value from the list of 0, 90, 180, and 270.  As you might expect the rotations are clockwise with 0 starting with the text in the ‘normal’ location.

Graphics.TranslateTransform is where the last piece of magic occurs.  It is here that you set where the top right corner of the text drawing area will be located in the DisplayRectangle’s area.  Here are some images that will help clarify the situation.

0degrees

When you need the text to appear the same as “Text Area” does in the above image (rotated 0 degrees), you need to set the TranslateTransform X and Y parameters to be those that are designated by the “X” in the image.  In this case, it’s X=0 and Y = 0.

90degrees

The picture above shows you what you should be displayed when you are rotating the text “Text Area” 90 degrees.  Again, you need to set the TranslateTransform, but this time the values are slightly different.  The Y parameter is still 0, but the X parameter equals the height of the text.  You can get this value by using the following line of code:

var textSize = TextRenderer.MeasureText(Text, Font);
textSize.Height;

180degrees

To render the text upside down we set the rotation to 180 degrees and then, again, determine the location of the TranslateTransform X and Y coordinates.  Like we did for the last rotation, we will need to retrieve the text size to set these values.  For this situation Y will be the text height and X will be the text width.

270degrees

The final step is to make the rotation work for 270 degrees.  Like all the others, we need to set the X and Y coordinates for the TranslateTransform method call.  Here the Y value will be the text width and the X value will be 0.

This is simply the first step of many to making a control that will allow rotation of the text and locating it in one of 9 locations in a 3x3 grid representation of the control’s DisplayRectangle.  More on that in another blog post though.

PrairieDevCon 2010 wrapup

Friday past brought the end to the first incarnation of the PrairieDevCon in Regina.  The conference had a great buzz of people, interest, conversations and learning about it.  It really was a blast to be at it.  Thanks to everyone who attended in whatever capacity since it was you that made this event so much fun and productive to be at.

Here are the materials from the sessions that I presented.  There isn’t anything for the panel discussion since it was all off the cuff.  If you weren’t there you didn’t get to add or absorb….sorry.

Intro To Aspect Oriented Programming: Slides, Code
ORM Fundamentals: Slides

Thanks again everyone and I hope to get invited back to do this all again next year.

Where do you start building skills from?

In the past I’ve had to take development teams and build their skills.  It was part of what I was hired to do.  “Build an app, and at the same time make our developers better.”  I’m back at it again and today I had a chat with someone online about where do you need to start.

First you need to know what your goals are.  Usually I find that management is, by asking me to make their developers “better”, looking to increase quality, decrease development time and increase maintainability.  All of these are pretty vague and there’s certainly no one day course for each one, let alone all of them.  So where do you start then?

One of the first lessons I learned while at basic officer training was that before getting my section/platoon/company working on a task I needed to know what their skills (special or otherwise) were.  The lesson was all about resource management.  I’m starting a new project complete with a new (to me) development team and once again I’m being asked to make them “better".  I could go into a meeting room right now and tell them all how they should be doing TDD, BDD, DDD, SOLID, etc.  Some (most I hope) of you will agree that these are practices that can make you a better developer.  It would be far more prudent of me to walk into that room and ask instead of state though.  I should take the lessons of my Drill Sergeant and initially put effort (and not much will be needed) into evaluating what skills (special or otherwise) the team has.  That knowledge is going to set the foundation for how I will approach making these developers “better”.

One of the questions raised in the conversation I was having today was “When we talk about things that we can throw at developers to learn, something like DDD is (sic) beneficial. By the time someone reads the ‘blue book’ they should know quite a bit.  Where would you place it (sic) relative to SOLID or the other good practices?”  This raised the question of what knowledge in what order when dealing with under trained developers.

For me the whole idea revolves around one thing building on another.  Yes you could dive straight into DDD, but wouldn’t it be easier if you understood something about SOLID, or OO Fundamentals?  So what is my preferred order then?  Depending on what the developers skills are I may start in different places, but usually the order would be something like this.

  1. Tooling.  Understanding and being effective inside Visual Studio and other tools that are used every day.
  2. OO Fundamentals.  Abstraction, Encapsulation, Polymorphism and Inheritance.
  3. DRY. Simple code reuse techniques.
  4. SRP. Single Responsibility is the foundation (in my mind) for all that follows.
  5. OLID.  The rest of SOLID.
  6. Coupling.  Why developers should care and how they can deal with it effectively.
  7. Application Layers.  How to use logical layering to build more flexible applications
  8. TDD.  With the foundation for good design acquired, developers are ready to learn how to apply those design skills.
  9. DDD.  How to think about business and translate it into code.
  10. Frameworks.  With the foundations built through this list, I feel developers are ready to understand how to use tools such as nHibernate, StructureMap, log4net and others.

I made the mistake that most developers have; jumping straight into frameworks.  While it didn’t set my career back, I did need to take a step back and put concerted effort into building my way back up to frameworks again.  The best part?  With all of the fundamental and foundational knowledge, learning almost any framework is quite simple.

You can’t expect to blast into a room full of developers and expect them to follow this (or any list on this topic) to achieve overnight success.  It’s going to take time.  Time, effort and commitment.  At least the transition from one learning topic to the other will be logical and smooth.

Winnipeg Code Camp and DevTeach Toronto

It’s been a hectic couple of weeks speaking at Winnipeg Code Camp, the Winnipeg .NET User Group and DevTeach Toronto.  All the events were a blast to do.  To those who attended, thanks for the great questions and conversations.

Session material is available as follows:

Introduction to Dependency Inversion and Inversion of Control Containers – Slide deck, Dimecasts
ORM Fundamentals – Slide deck
Software Craftsmanship – Slide deck

NotAtPDC and ICE Edmonton session material

Thanks to those who attended the session that I did on “A (Failed?) Project From the Perspective of a Team Lead”.  As you know from being there, the slide deck by itself is not all that useful.  Instead of putting your memory to the test I’ve written up an accompanying white paper that can be downloaded here (pdf format).  If you also look back at my series on failure you can see some other examples of what and where I’ve failed on projects and how they could have been, or were, resolved.

Failure: Establishing Flow

Like my past posts in this series, we’re going to talk about failures and I’ve had this problem at a number of places that I’ve been employed or contracted.  I just can’t maintain flow.  Developers all know what flow is, whether this is what you call it or not.  You know that feeling when you’re “in the zone” or “on fire”.  That’s flow.  It’s a period of focused hyper-productivity.  We enjoy it.  I long to experience the adrenaline like rush that it provides to me.  The things is, achieving flow is the exception, not the rule, for our daily lives.  Here are some examples from my experiences.

At one place I worked I shared an office with another programmer.  It was located part way down a quiet dead end hallway.  Just a little farther down that hallway was an office belonging to a manager in the company.  She had a fairly regular stream of people coming and going through the day.  The office that I was in was a little bit weird in it’s layout.  While there was a hallway right outside of it, the office also had doors connecting it to those on either side of it.  For some reason my co-workers got in the habit of not using the hallway to walk to this manager’s office.  Instead they would travel through my office and use the joining door.  When the manager was busy people would linger in my office waiting for her to free up.  Essentially my office became a waiting room for this manager’s visitors.  As you can imagine it was quite disruptive.  No amount of loud music in headphones could completely block the distractions of movement that occurred in my peripheral vision all day long.

In another working situation, I was managing a fairly large development team.  As a result, I was required to be constantly moving about helping to solve problems, clear roadblocks and to communicate (read: meetings) with management above me.  All of those tasks were part of the job.  Over time my managers began to request my attention on shorter and shorter notice.  This resulted in my day constantly being broken up by spur-of-the-moment meeting requests and drop in discussions.  Regularly these would interrupt work that I had to get done.  Instead of being able to focus on that work for any appreciable amount of time, I found my self constantly having to re-integrate the work task into the front of my conscious mind.  As most of us know, this kind of context switching takes an enormous amount of effort and an appreciable amount of time.  Tasks that would normally have required 30 contiguous minutes of effort were now taking two hours (when the smaller pieces were added together).

In both of those cases I was far less productive than I could have been.  I responded differently in both situations as well.  In the highly trafficked office I expended an enormous amount of effort trying to block out the distractions.  When that wasn’t providing any results I attempted to break my co-worker’s habits by locking the connecting doors between the offices.  The, of course, revolted against my attempts to change their habits and freedom of movement was quickly restored for them.  In the end the only times I was able to regularly achieve a state of flow in that job was when I was working in the office after 7pm.

When I was fighting with the cost of context switching in the second example, I did for a short time attempt to counter act the problem by block booking “meetings” in my calendar.  For a short while people respected those meeting blocks which gave me the desired results.  Again the old habits and practices came back once people clued into what I was doing.  Once that happened seeing a “meeting” entry in my calendar simply invoked the person to walk by my cubicle to see if I was there or not.  If I was….well…you get the picture.  Like the other example, the only times that I could make any progress were in non-traditional working hours.  For the duration of that work engagement I never once successfully achieved flow.

So how is this a failure (since this is a series on failures)?  I failed to be able to produce at the levels that my employers should have received from me.  Yes, a significant portion of the blame can rest with my co-workers and bosses for not allowing or providing an environment where flow was possible.  On the flip side, as a professional I should have requested that I be allowed to create those conditions.  I’m not saying that I should have marched into management’s office and stated “I want to be left alone to work”.  That just won’t fly.  Any development role requires interaction with others.  You can’t just squirrel away and expect to have full day after full day of flow capable time.  It’s unrealistic.  There are a couple of things I should have done though.

First I should have requested to have an office that minimized any unnecessary distractions.  I should have made it clear that having my office be a hallway and/or waiting room was unacceptable considering the alternative (the hallway) that was available. 

I should have made it clear to people that I had ‘office hours’ where I would be available and ‘productive hours’ where I would not.  As long as the ratio of the two is well balanced (this will vary depending on your role and the requirements that the job places on you), I don’t see this as being a difficult request to fulfill.

Finally, I should have made it clear to all of my bosses that the environment was not allowing me to achieve what I was being asked to do.  I should have explained to them, and quantified if requested, how much it was costing them by keeping me from achieving flow.

In any event both of those experiences are from a number of years ago.  It wasn’t until last spring that I was lucky enough to hear Neal Ford mention the book Flow by Mihaly Csikszentmihalyi in one of his talks.  I spent a week reading and really absorbing the information in the book and now understand that the state of flow doesn’t just happen.  You can construct it and make it repeatable.  Not allowing myself to do that at future jobs will be a failure completely born by me now.

Failure: Napkins and a completed product are not good enough

Continuing on my journey through failures and the lessons that I’ve learned, we’re going to make a stop at a project that I did when I worked at a very small (but successful) development shop.

I started at this shop and one of the first tasks that they assigned me was building a reporting engine for a POS style’d application that they were re-developing.  This sounded fine by me as I’d just rolled out of a company where I’d done the exact same thing.  I gathered what little info I had on the new and old applications plus the reporting that was being replaced.  People dropped print out copies of existing reports on my desk.  I was getting my bearings with regards to the outputs that may be needed, but I still was completely lost when it came to what the user experience should be.  It wasn’t too long into this project that I realized this (I hadn’t written any code yet in fact) and decided that I needed a sit down with whoever was going to act as the client.

After finding out who I should talk to I set up a meeting so that I could gather some requirement stuff.  I sat down and laid on the table that I wasn’t sure what the expected experience should be when we finished this thing.  The response was “It should be configurable.”  I asked how.  I was told “We want to be able to change things like logos and stuff when we put this out to different clients.”  Okay…this person was obviously thinking of the final reports.  I redirected the conversation back to the user experience leading up to the generation of the reports.  The advice I was given at this time was “We need the same selection criteria as we currently have.”  Nothing more (nothing less mind you).  When I asked about layout, modality (this was a WinForms app) and other things that every user would see and have to deal with I was met with blankness.  After an hour of probing and questioning in different ways I left the meeting holding onto one detail…the criteria must be the same as before.

I left the meeting believing that the user experience was firmly in my hands and that anything I did (within reason and practicality) would be acceptable.  Oh my was I wrong.  I worked for about a month and a half on the product and had some working reports that I could show so I asked for a desk review of what I’d done.  None of the user experience was acceptable.  It didn’t mimic the green screen style of the system we were replacing.  In the eyes of the reviewers, I had horrifically failed.

For some reason, when trying to initially gather requirements I chose to ignore my past experience, which had taught me that just because a client didn’t state something, it doesn’t mean that they didn’t want it.  I should have known to dig harder to pull those requirements out.  That was a large part of my failure.  One simple assumption quickly transitioned into a mistake which grew into failure.

How could I have fixed it?  Well, the obvious answer is that I should have asked more questions and taken more time before getting started.  Sure, that will work, but I like to get things delivered quickly so having a large up front requirements gathering task, just isn’t my style.  The not-so-obvious answer is that I should have started, but delivered after the first week instead of waiting one and half months.  I should have mocked up the user experience in a way that conveyed my intentions and direction and shown that too the parties concerned.  I could have done it by drawing up some forms and linking them together to show navigation and mock content.  I could have take the decidedly lower tech solution and just drawn up some story boards.  Either way, after one week I would have known if I was on the right track or not.  If not, I would have had a great opportunity to use the existing mockup as a conversation starter to elicit more requirements and then I could have started over. 

Having to start over after one week is hardly a failure in my mind.  In the case of lacking guidance it would have acted as a necessary step in gathering requirements.  Better yet, I only would have burned one weeks effort instead of one and a half months.  That is a more responsible use of the company’s money and a much better way to get exactly what the business needs.  Fail fast and succeed.