Archive for February, 2008

Managing Mangers

Thursday, February 21st, 2008

A spot-on post by SerialSeb…

An Exception Handling system

Saturday, February 16th, 2008

I was having a problem where thrown .Net errors were showing up on a web site, but didn’t always want the entirely hide the errors, since sometimes the system purposely threw errors with messages that were intended for the user. (“You selected an invalid object for this operation”-type messages) However, unexpected errors, such as SQL errors needed to be entirely hidden. Either way, I wanted the thrown message to be automatically logged via log4net, and a “friendly” error page displayed. Lastly, I wanted this system to be easily reusable on future websites.

To handle this situation, I set up my own Exception class that extended the system exception class, called ApplicationException. All errors that were purposely thrown with a message that SHOULD be seen my the end user throw an ApplicationException instead of a plain Exception. Errors I didn’t expect would come in as plain old Exceptions.

I also set up an HttpModule that caught the application_errors, and would check the type of the actual exception, logged via log4net whether it was a regular Exception or an ApplicationException., and then did a Server.Transfer to an error page. (pulled from the appSettings) It needs to do the Server.Transfer (grabbed from the context) instead of the web.config error setting, by the way, because otherwise the error page won’t be able to access the Exception object! Anyway, then, those two files get pulled into error handling dll.. the HttpModule gets pointed to in the project’s .config, and the logging portion is done. Potentially, the system might also send out an email at this point, letting the developers know.
All that’s then needed is the actual error page(named in the .config file and referred to from the HttpModule) That page also switches on the Exception type… if it’s an ApplicationException, display the generated message inside the exception. Otherwise, it just displays a generic “Sorry, there’s been an error” message. This logic could/should probably also go into the ErrorHandling library, but the actual page can be site specific so each site can have a pretty site-matching error page, with a message telling the user what to do and who to contact, as appropriate.

Provider Model and design patterns-I need to learn!

Thursday, February 14th, 2008

Colleague was telling me about it, says it’s good for mocking, etc.  Putting it here so I don’t forget!

We also talked about a problem I was having where I’m the only one who understands the domain of my project, and if I’m not sure how to architect something, I have nobody to discuss approaches.  She brought up design patterns, and how if if I can find a design pattern that is similar to my problem, I could use that as a starting point with someone who is unfamiliar with the domain, discuss ways in which the pattern does NOT fit, and go from there.  That way, I could still productively bounce ideas off people.  I’ve glanced at some patterns in the past, but other than the Strategy pattern (which actually got used in my project), I’m pretty fuzzy on them.  This is some new incentive to crack open the Heads Up book.  And hey, maybe even the GoF book, which I’ve heard is a must-read. (I get the idea it’s a bit better, but the Heads Up book is already in my possession and is an easy read)

Log4Net Success! And nHibernate upgrade goodness..

Thursday, February 14th, 2008

So in a fit of insanity, I spent all of yesterday evening, on my own personal time:

  • trying to get log4net set up,
  • trying an upgrade to the latest version to see if that would get it working
  • realizing that nHibernate was hooked to log4net and so I had to use nHibernate’s chosen version if I wanted to use the binary
  • said “What the hell” and decided to upgrade my project to nHibernate 1.2
  • FINALLY got log4net working
  • realized that log4net’s working might have been a configuration issue, and had nothing to do with versioning
  • realized nHibernate was seriously busted… whole project was busted
  • rolled back the version updates-hey logging still works!
  • then, went back and methodically tried to upgrade log4net/nHibernate (long as I was in the groove, might as well)

So, there I was, upgrading nHibernate/log4net. Once log4net was working, it seemed to work reasonably well. nHibernate was a fair bit more involved! the iTriggerable interface had a few extra members, which was making things not compile. Interestingly, they recommended extending from an empty version… the idea being that if they add any more crap, they’ll add it to the base class and I can be ignorant. Yay to targeted ignorance!

The more annoying, less obvious issues with the upgrade ended up being ended up being

  1. virtual is now required on all mapped objects. This was a big pain… I’d known about this, but thought it was only needed on the ethods nHibernate interacted with. nope, it’s everything! So I had a TON of places that needed it. Luckily (well, maybe) the code compiled without them, but gave a giant runtime error list missing EVERY method missing virtual, so I’m pretty sure none are missing now!
  2. This may come off as confusing, but bags that hold items that are subtyped need their discriminator in the where.  I don’t see why, this strikes me as duplication, and is fairly yucky.  Not yucky enough to not do the upgrade, but yucky.  The wost part isn’t even clear in the nHibernate documentation… it shows as an example something like  where=”discriminator=’CAT'” but that discriminator is really your actual discriminator column name, so in reality, it might be where=”animal_type=’CAT'” … and you’ve got this in the subclass definition already, so why here?
  3. This last one is probably the most subtle… if you do typeof(myInstanceOfCatClass) you DON’T get CatClass back… you get something like (this is from memory, the idea is correct even if the exact words are not) CProxy_Class_CATCLASS_Proxy .  I imagine this is due to nHibernate’s stronger emphasis on lazy loading, and relying on proxying (thus the virtual importance on everything)  …  however, this broke code, and I didn’t know to look for it.  To get CatClass back , you actually have to call NHibernateUtil.GetClass(myInstanceOfCatClass)

All of the old queries we were using are using Find, and are all giving “obsolete” warnings.  Ah well, see those enough while running Nant, and I’m definitely going to fix every one of those out of sheer annoyance.

Anyway, I’ve alwady written some HQL and gotten a typed collection back. Woohoo!  That was great.  And a colleague helping me on a task out tried making a typed collection in the business objects… and it worked, though she said it only worked with an ICollection, not an IList… weird, something to look into.  So, the key to getting a typed query with the new HQL stuff is that after the CreateQuery where you do List(), you instead do List<CatClass>().  That’s it!  (assuming CatClass is the correct type the query is actually trying to return, otherwise, I suspect you’d get a runtime error)


Wednesday, February 6th, 2008

I am determined to get Log4Net working on my project.  I made a mild effort recently, but wasn’t successful- and the whole failing silently thing is not so great when you’re trying to figure out WHY it’s not logging!  Anyway, a goal for tomorrow is to get that working. If that happens, I think it’ll be a party-worthy occasion because debugging things on live is a bear at the moment.  Actually even debugging things in the dev environment is a bit of a bear, but that’s a different issue….

Resharper rewrite

Wednesday, February 6th, 2008

OK, so the first ReSharper post was a written statement of resharper joy. However, it’s not the best format for getting me some $$ for ReSharper at work. So, let’s write up an informational document for the people who would make the decisions…

ReSharper is an add-in to Visual Studio that provides Visual Studio with a number of useful features. Overall, they help a developer code cleaner, faster and more efficiently.  ReSharper is produced by JetBrains, the company that also produces IntelliJ, a highly regarded IDE for Java.

Some Key Features

  • Find Usages – this is VERY different from a search. It finds anyplace a class, a class’s method, a property or a variable has been used… if another class has a method of the same name, that method won’t come up-only the ones you want. This is a huge time saver. It’s amazing the amount of time you want to know where something is called so you know if it’s “safe” to change, for instance
  • Related to “Find Usages” is Rename… rename a class at the site of an object declaration, and the class will get changed, the name of the class file, the constructor, all declarations of objects based on that class, and any private variables that seem to have a related name get changed. This is great because any code that’s been around for a while collects artifacts that are misnamed… among other reasons, because something ends up getting repurposed in unexpected ways, or a domain concept becomes better understood. These misnamings were previously a minefield that would inevitably happen. With rename, it’s very easy to give objects the proper name, so the code can become much more intuitive.
  • Highlight unused code. This isn’t really something that a user actively does, it’s one of the many things ReSharper does on its own that helps a developer code better. If a variable or “using” statement is not being used, it colors it gray. Then, unused things can be removed with a quick keystroke. Just another feature at keeping the code clean.
  • If you’re viewing an interface, there’s an option to go to implementors, and it provides a list. Yes, you could do a text search for the interface name, but if you ARE coding with interfaces, this saves time, and is a really intuitive way to navigate.
  • Code completion. If configured correctly, Visual Studio will do code completion, but it requires a code compile for methods to come up. Resharper parses code on-the-fly, so you immediately have classes and methods from other files available as you are working, even if things don’t compile. A little thing, but another timesaver-it saves on typing/guessing errors on your current work even when the code isn’t compiling.
  • It provides better inline method documentation. Again, you could get better documentation on the web, but having it right at your cursor as you type is instant (unlike a web search), and doesn’t require a context switch.
  • It autoparses the ASP.NET aspx files, and makes the content accessible via Intellisense in the codebehind file. Again, you could just spend time flipping back and forth, copying and pasting, but typing in the codebehind and having it pick up the elements in the aspx file helps reduce error (you can intellisense the name out so you KNOW it’s correct) and then you also have the elements’ methods immediately available via Intellisense, so you won’t have to manually type NavigateUrl, savinng type (and avoid possibly mistyping as NavigateURL)
  • Many refactorings are supported, such as “Extract Method” and “Extract Interface”. Refactoring is a way to impove the readability and maintainability of code though safe reogranizations and restructurings. This is something all good developers do as a matter of course, but this tool can help a refactoring happen in a few seconds instead of minutes or even hours. Making refactoring easy is going to make it happen more, which (when done appropriately) is very healthy for a codebase.

Items of note on ReSharper:

  • ReSharper works best on a properly organized Visual Studio project, with classes organized by namespace in folders (this is the recommended way anyway) … but to get best use out of ReSharper on a project that’s legacy and incorrectly set up, you will want to straighten things up.
  • It’s SLOW to open a project. … this is because ReSharper does a pass through of everything for parsing. Granted, things are fine once it does open, but if constantly opening and closing things in VS is your mode of working, You might want to either rethink ReSharper, or your mode of work.
  • It’s a memory hog. This is probably (I’m guessing here) somewhat proportional to the number of classes, since it parses, and holds the definitions in memory so it can do its magic. Anyway, JetBrains says 512 is a bare minimum, and recommends at least 1 gb. I have to say that with my machine set up normally, an ftp program open, and a web browser, I’m going WAY over my 1gb of memory, and am swapping like crazy. I’m guessing here, but looking at my real-world memory usage, I’d say not less than 2gb, and maybe more to be comfortable.
  • The current version is 3.1, but 4.0 for VS 2008 is coming out soon. Anyone who buys 3.1 now gets a free 4.0 upgrade license when it comes out.
  • As this is a plugin for Visual Studio, The developer must have at least Visual Studio Standard (the lowest non-free version, and the cheapest version taking plugins) to use ReSharper.



  • Single-developer cost $149 for C# only, $199 for C# and support. They are non-transferrable. The license allows for installation of the software on different computers (at work and/or at home), provided that multiple instances of the software will not be used at the same time.
  • Commercical license is $249 for C# only, $349 for C# and VB support. Commercial licenses are based on a concurrent user model… as many installations as you want, but number of copies legal to be used at the same time limited by the number of licenses purchased. Non-transferable to other companies, but transferable within the organization.

Rhino Mocks

Wednesday, February 6th, 2008

It sounds like Rhino mocks is the way to do mocking… I just watched Ayende’s intro to Rhino Mocks (Episode 1) and mocking seems like the way to go… and I have some hope that I can use it on my current project! At a very high level, I get that mocking is the way to properly unit test.  I’m still working out the specifics of how one actually sets things up to be mockable.  It looks like a key is to interface things like crazy.  Luckily, I’ve got resharper (well, for the next 20 days or so until I’ve got to beg or buy a license) , so throwing interfaces on some key classes to start is doable.  I’d really like to get some real unit tests written.  At the moment I’ve got some integration tests for a complex portion of my project-which were a godsend, I don’t think we ever would have made it to launch without a way to test that didn’t involve people spending 2 hours creating each scenario in the UI.   And those were a lifesaver, but they were complicated and a bit finicky since they involved a db and a bunch of webservice calls, and because of those same things, were also slow as heck.

ReSharper = manna from heaven

Friday, February 1st, 2008

This week, after having heard/read many, many times from a variety of developers that ReSharper from JetBrains was essential for any C# developer, I (finally)gave it a go. And after having only worked with it for about three days, I have to say that Resharper just might be the best thing I’ve ever downloaded. Sure, iTunes was fairly cool, and Google earth was nice and all, but the first time I loaded ReSharper up and got things set to use it (more on that later), and it told me what classes/methods were being used, plus it could tell me WHERE (this is very, very different from a text find)… and then, when I used its built in rename function, and it auto-renamed the class, the file name, anywhere that class was used, AND even listed the places it showed up in strings and comments, so those could be gone through separately/manually if desired …. well, that was exhilarating. To think I was no longer bound by the tyranny of my (and others’) old legacy names! That’s one of the things that has always annoyed me about working on an older project. As you get more familiar with a domain you inevitably realize that your original names for things reflected poor understanding, and are in fact misleading… however, you stick with them because of the nightmare it would be to change. Well, no more! (If only you could bottle this kind of glee…)

ReSharper also tells me what is not being used, which is just as important. Of course it’s important to give stuff a quick test after ripping stuff apart, but ReSharper actually encourages better coding. If things are not being used (using statements, variables, etc) it shows them as gray, and you get into a habit of commenting out, or even better, deleting stuff in gray by habit. It’s a simple thing that really helps keep the level of useless cruft down. It also tells you if you can declare variables in a smaller score, keeping things tidy (sometimes I decided against, for the purposes of declaring “starter” values all at the top of a method where they won’t get lost, but sometimes I went along and moved it-which involved just a click)

It even has features like go to implementors of an interface, and if you added things to the interface, you can click to have those methods added, albeit stubbed out with a “Not Implemented” exception… go to interface, or go to inheritors… Just lots of little things that all add up to a more seamless experience.

The initial setup was a fair bit of work, though I really can’t blame that on ReSharper. At work, I mostly support one large site that is several years old, and was really a learning experience for my organization. By the time I came on to the project, it was several years old, and had been through several developers. As a result, it was a hybrid of different approaches, and didn’t quite fit a “usual” .Net setup. To get ReSharper working fully, I had to move a number of directories around, and get the site into a real VS “Project” and “Solution”.. this all took a fair bit of rejiggering and such, but it was definitely worth it.

It’s $200 for the full version that also includes VB support, but I’ve already decided I don’t want to write C# without ReSharper. I’m at a small, mostly shop, but I hope I can sell my need of this anyway. If not, well, $200 is not too much to have a better working experience…