Monthly Archives: April 2008

A Big Decision

     This past Tuesday, I resigned from my job of three and a half years, and my last day is May 2nd.  This wasn’t caused by any real dissatisfaction with my job.  Though not perfect, I enjoyed my work and liked my co-workers.  My wife and I had decided shortly after our daughter was born that we wanted to be closer to family.  A five hour drive may not seem like a long way to some of you, but it meant that our parents only got to see Molly once every couple of months, which is huge when you’re talking about a seven-month-old. 

     In a lot of ways, it feels like a graduation.  It began recently enough that I can still remember my first couple of weeks vividly, but long enough ago that it feels extremely odd to think about not going there and seeing the same people every day.

     I wonder if it might also be like a graduation in that I feel like where I’m going is more like the “real” world.  Even though my old shop makes use of the .NET platform, it really doesn’t feel like a .NET shop.  It’s an IBM mainframe shop that dabbles in .NET.  The place I’m going has been using the .NET platform since its inception, and uses it as their primary development environment.  There a little bit of RoR going on (which I take as a good sign; they’re open to new and non-MS things), but mostly ASP.NET in C#.

     Also, rather than being an in-house IT provider, my new employer is a consulting firm.  This will be a bit of a change for me.  I’m used to dealing with one particular business (transportation), but now I’ll have exposure to all different kinds of industries.  My customers used to be just a flight of stairs away, and were almost always willing to sit down with you to discuss a feature that you were working on.  I have no idea now how accessible my new customers will be, or how much interaction I’ll have with them.

     Another thing that will be new is certifications.  I’m not sure which (if any) devs at my old employer have MS certifications.  Since they have their own training department, it’s assumed that everything necessary will be covered in training classes.  The new place doesn’t have it’s own training department, and besides, they want to give outside customers the warm fuzzy feeling that all the developers are “Microsoft Certified.”  I’ve been told that when I start, I’ll have a goal set for a certain number of certifications by the end of my first six months.  The company will pay for the books, plus my first attempt at the test.  Fail, and I’ll have to pay for the next test myself.  :-/

     Related to that, I’ll also have a training budget for books, conferences, etc., which ought to be cool.  Something like $3k a year, plus about three weeks per year of dedicated training time.  Not sure exactly how I’ll use it yet, but I’m sure I’ll figure out something.  😉

     I’ve made a lot of friends in Fort Smith, and I’m going to miss them all a lot.  I’m not sure how I’m going to be able to work without getting Travis Ebert’s weekend updates, and without being able to run over to David Mohundro’s cube when I have a design question.  Not living next door to Josh and Jennifer Pense (our best friends and carbon-copies) will feel so strange, and we will miss them terribly.

     On top of everything, our house was recently damaged by hail, so we have to get all of the siding and windows on the front of the house replaced and get a new roof before we can put it on the market.

     I apologize if I’ve rambled a bit.  My brain has been a bit addled since Tuesday, but I knew I had to get this post done soon, or I would never do it.  I’ll be able to post more descriptive updates regarding the new job once I start on the 7th.

     It’s a huge decision, and kind of scary, but I know God will guide our family in the direction He wants us to go.  “And we know that in all things God works for the good of those who love him, who have been called according to his purpose.”

My First Real ASP.NET Site

I’m fortunate enough to be one of the first developers at my current employer to create a site using ASP.NET.  Currently, almost all our internet and intranet applications are built with classic ASP.  Yeah, I know.  Early adopters we ain’t.  As cool as it is that I’m getting to blaze this trail, it also means that there’s little guidance to be had, and no internal examples to look at.  Even the guys I usually go to for advice at work (Kerry Jenkins and David Mohundro) haven’t used ASP.NET that much.

Right now, I’m still struggling to come up with an architecture that I think will be clean, but will also be understandable by people that come along behind me.  I know this problem has been solved a bazillion times before, but I’ve found it a bit hard to find some examples that are close enough to real-world complexity.  I took a look at Code Camp Server, but it uses NHibernate, which I don’t think is an option for us.  Using open-source software to build applications is viewed with more than a little apprehension by management.  Their reasoning is that if something goes wrong with the software, no one has a responsibility to offer us support.  I think that hinders us in a lot of ways, but that’s a topic for another post. 😉

So, lacking any good examples, I came up with something myself.  To illustrate, I’ll use an example scenario:  creating a new user account.  I’m going to simplify it and just talk about the architecturally relevant parts.  (I’m sure nobody cares what my CSS looks like.) 

The solution has four main projects:

  • Web (Controls and aspx’s)
  • BusinessLogicLayer (so there’s as little code in the aspx.vb’s as possible)
  • DataAccessLayer (DB2 Database stuff)
  • ObjectModel (DTOs and stuff)

So, say I have a Register.aspx that looks like this:


  
       
        Please choose a user name and password:

        User Name:
       

        Password:
       

        Confirm Password:
       
       
                    onclick=”submitButton_Click” />
       
   

Standard stuff.  One thing that gave me a bit of trouble was how to get error messages back to the UI.  I toyed with the idea of having something named Context that would contain both the object the UI needed, plus messages about any errors that happened while performing the request.  I eventually rejected that in favor of having my pages implement an interface like this:

    public interface IView
    {
        string ErrorMessage { set; }
    }

So my business logic services can take an IView in their constructors, then set the ErrorMessage property when it encounters an error.  So I end up with something like this:

public partial class _Default : System.Web.UI.Page, IView
    {
        WebUserService _userSvc;

        protected void Page_Load(object sender, EventArgs e)
        {
            _userSvc = new WebUserService(this);
        }

        protected void submitButton_Click(object sender, EventArgs e)
        {
            WebUser user = _userSvc.CreateNewWebUser(userNameTextBox.Text, passwordTextBox.Text);

            if (user != null)
            {
                FormsAuthentication.SetAuthCookie(user.UserName, false);
                Response.Redirect(“Home.aspx”);
            }
        }

        #region IView Members

        public string ErrorMessage
        {
            set
            {
                serverValidator.ErrorMessage = value;
                serverValidator.IsValid = false;
            }
        }

        #endregion

}

public class WebUserService
    {
        private IView _view;

        public WebUserService(IView view)
        {
            _view = view;
        }

        public WebUser CreateNewWebUser(string userName, string password)
        {
            WebUserRepository rep = new WebUserRepository();
            WebUser user = null;

            if (rep.GetWebUserByUserName(userName) == null)
            {
                user = rep.CreateNewWebUser(userName, password);
            }
            else
            {
                _view.ErrorMessage = “That username is not available.  Please choose a different one.”;
            }

            return user;
        }
    }

public class WebUser
    {
        public string UserName { get; set; }
    }

Another thing I had a bit of trouble with was deciding where to check for things like duplicates in the database.  I thought that if I did most of that in the data access layer, I’d have the same problem communicating error messages back to the business logic layer that I did to the UI.  So, I decided to try to make the data access layer methods as granular as possible so I could do all that kind of validation in the business logic layer.  It’s more chatty than I’d like, but I think it will work for me, since we really don’t have any logic in the database.  (We don’t use stored procs or anything, because higher-ups don’t want any logic in the DB). 

I think this setup ought to work for most cases, but I don’t really have enough experience to know if I’m on the right track here.  If someone with more ASP.NET experience than me has any suggestions, I’m wide open.  I’ll post more of my experiences as I go along.

More Adventures with Witty

As you may have noticed, I haven’t been keeping up with my desired posting rate lately.  That’s mostly because I’ve been spending most of my outside-of-work “geek time” working on Witty, an open source Twitter client written in C# using WPF.  After my positive experience building in proxy support, I decided to implement another feature that I particularly wanted to see, namely “toast” popups.  (You know, those little mini windows that show up at the bottom right of your screen when you get a message in some IM clients.)

This enhancement was already posted as an issue on Witty’s issue board, so I knew I wasn’t off base thinking it was missing.  The person who originally asked for the enhancement suggested that we send messages using Snarl, an application aimed at recreating Growl on the Mac, to minimize the additional code needed to achieve the toast functionality.  So I visited the site, looked at the API, and set about trying to send Snarl messages from a C# program.

Even though the site listed C# as one of the supported environments, it turned out that the only semi-useful library available was unmanaged C++.  “No problem,” I thought.  All Snarl requires that you do is send a Windows message to the Snarl application, so I figured that I could translate the needed Win32 calls by looking at pinvoke.net (a wonderful resource for Win32 interop programming, btw).

I was half right, I suppose.  The SendMessage call was easy enough to duplicate, but the kicker was the data structure I needed to pass to the Snarl application.  As I would soon discover, even though the type names in C++ and C# are very similar, they mean different things behind the scenes. 

For example, a “long” that was part of the data structure in C++ is actually only 32 bits in size, whereas a “long” in C# is 64 bits.  Those were easy enough to fix, since C#’s “int” type is the appropriate length.  The character arrays were a bit trickier.  First of all, a “char” in C++ is one byte, while in C# it is 2 bytes.  You can get around that by changing the char arrays to byte arrays.  But the thing is, arrays in .NET are a totally different animal than they are in C++.  In order to use fixed-length arrays like Snarl wanted I had to use the “fixed” keyword, like this:

public fixed byte text[1024];

The problem with that is that in order to get it to compile, you have to mark the struct using it with the keyword “unsafe.”  Yuck, that doesn’t sound good, does it?  And it’s not.  When you do things like that, you open yourself up to all kinds of mess that you thought you left behind when you started writing .NET code.  Not fun.

But, hey, it got messages to from Witty to Snarl, so I submitted a patch.  The guys were happy to see progress made, but the unsafe marker made everybody nervous.  Plus, it meant relying on an outside application for notifications.  So, I got to work doing what I probably should have in the first place:  creating integrated popups in WPF.

Meanwhile, Michael Letterle got wind of the Snarl integration that we were trying to do, and fixed those notifications faster that should be humanly possible.  The trick was a couple of attributes I’d never heard of before:  [StructLayout] and [MarshalAs].  The structure ended up looking like this:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    struct SNARLSTRUCT
    {
        public int cmd;
        public int id;
        public int timeout;
        public int lngData2;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = SnarlInterface.SNARL_STRING_LENGTH)]
        public char[] title;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = SnarlInterface.SNARL_STRING_LENGTH)]
        public char[] text;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = SnarlInterface.SNARL_STRING_LENGTH)]
        public char[] icon;
    };

Normally, the CLR will store a structure’s component parts wherever it feels like, so the parts of the struct could be separated.  In a managed world, this doesn’t bother us, we can always get back to the data if we need it.  But if we need to pass this struct to unmanaged code, we have to tell the CLR specifically to keep all the parts in one place, which we can do by specifying the [StructLayout] attribute.  I’m a little fuzzier about what the [MarshalAs] attribute does, but it’s pretty obvious that it’s telling the CLR how this managed data needs to be translated when passed to unmanaged code, and it lets us specify a length for the arrays.

Back in WPF land, I got to play around with a bunch of neat stuff.  Even just copying the look and feel of the login window, I started to get a feel for XAML, at least in some of its simper forms.  I thought that the way it defines animations was particularly interesting.  To do a fade-out for the popups, I used something like this:


     
       
       
       
     

   

I just think it’s cool that you can declaratively define an animation sequence.  It just seems a lot more self-contained and comprehendible when it’s laid out like this, as opposed to a chunk of imperative code.  It’s hard to define why it feels better, but it just does.  I think that’s what they call getting the zen of a particular technology, and I think I may have gotten a whiff of the zen of WPF here.  I’m hopeful that we’ll be able to start using WPF in my workplace within the next couple of years.  We’ve still got some Win2k machines out in the field, so they will have to be phased out before that can happen.  That, and they’ll have to spring for VS 2008 for the developers ;-).

I’ve had a lot of fun working on this, but after this feature is declared finished, I think I ought to work on a bug fix next.  One of the things I’ve heard about OS projects is that most people just want to work on new features, and it’s hard to get people to fix bugs.  I want to be more responsible than that, so I think I’ll take one (or a few) for the team and do something that may not be quite as fun next time.

Keep checking for new releases, Witty is getting better all the time!