Archive for the ‘Programming’ Category

SharpShop in Progress…

Monday, March 17th, 2008

Like PythonShop (which could still use some touching up) the current task is to build the same functionality in .net. As I’m the .Net person (and *I’m* not at PyCon this weekend) I started up the project groundwork. I’m trying to do it with NHibernate, log4net, mysql (yeah, thought it might be interesting to hit something non-MS) full TDD methodology, maybe Windsor if I can figure out a good place to use it that isn’t over my head at my current knowledge level, and maybe MonoRail. With my current project at work, the project was over a year old by the time I got on board, so a fair amount of infrastructure was already set up. As a result, I’ve never really done this sort of thing from the beginning… I’d done .Net at my previous job, but it was your standard MS-style with Datasources, no ORM, so entirely different. Anyway, I’ve been doing a lot of reading trying to figure out how to set things up well. The amount of clearly written information online for project architecture seems pretty thin, and what’s there varies a lot in recommendations without explaining why, and in the quality. Maybe architecture is usually something learned by mentoring, I’m not sure. But it makes for tough going for learning.

At the moment, I’m structuring the project based on a tutorial at … this is just a set of handy-dandy links for individual lessons by Ben Scheirman hosted at It seems to strike a good balance between separating things out and ease of understanding. I tried looking at Ayende’s tools this weekend to get some similar guidance on architecture, but though I’m sure it’s probably more extensible, etc, all of that comes at a cost of being more abstract and harder to understand, particularly when you’re working through it alone.

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….

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…

The PythonShop saga continues

Monday, January 28th, 2008

We made a lot of progress working on our python shop this weekend. Our working patern ended up being a pair programming scenario, with him at the keyboard (as he was the python person, this was the most efficient) and me finding logic problems, and working on the overall program design. Based on some of what I’d seen discussed recently on the Alt.Net list regarding validation, we ended up doing a close-to-the domain style validation. The classic TurboGears way of data validation is to attach it to the page methods inside the controller. In contrast, the Alt.Net discussion centered around how domain logic validation (for instance, object x should have an object y to allow a certain set of actions, and this integer should not allow things outside of a certain range, and certain fields should be required) should be usable with the business objects themselves. If this is really domain logic, that is something that gets changed by the client, should be in one place and should be reusable no matter how these objects are addressed, bet it in other forms in the application, future tests, or even command-line manipulations. In a more advanced scenario, we could even have different object validators that we inject as appropriate… since this project is a toy 5-table project, we considered that, and decided that was out of scope. But to bring that object validation logic into the domain, we ended up working an optional validator method into the business objects.. We still have validation in the controller to deal with the form, but it purely tests for formatting of input… for instance, numbers should be numbers, to make sure that any data that makes its way into the object at all at least is the right type so the validator there won’t throw up trying to compare a text string to a decimal or something. The only negative of this is that it ends up being a two-pass validation, (type formatting then business validation) but it seemed like an acceptable tradeoff.

Python, the other white meat

Tuesday, January 22nd, 2008

So, a colleague and I started on a little project. We’ve decided on our own time to take a toy project and jointly try it out in as many languages as are interesting to us. It’ll be a great learning experience, and it’ll also be some nice code that can be shown off without nondisclosure-type strings attached. We’re an interesting team because he’s heavily a Python person, whereas I’m mostly .Net. We’ve decided upon a shopping cart as our model project.

Our first session was to determine a general design/scope for all of these little shopping cart projects. An hour or so later, and a paper full of boxes and arrows, and we had a rough first hack at the sort of model that Amazon must have. Upon looking at it the next day, we decided that we’d gone though some temporary insanity. For the type of toy project we want to build, things like coupon codes, order fulfillment and tax were way out of scope! Really, a simple 5 table design was plenty for experimentation, and more than that would be too exhausting to realistically get through multiple projects.

Our first project is in Python, which you might think would be boring for my python-specializing colleague, but we’re doing it using TurboGears and SqlAlchemy, which are both new to him. I’m working my way through learning Python basics and finding some interesting differences from C#. The passing in of self everywhere as a parameter is odd, especially in that you only have to declare it, but not really pass it! I also found the importing of not just namespaces, but of actual classes (sort of as a shortcut) to be fairly weird. the dynamic number of parameters to functions was interesting as well… in C# you’d have to handle that as a passed-in array, list, or dictionary, and those things really wouldn’t be full-fledged parameters. At this point, he handled the initial TurboGears setup, and I’m looking through the default login stuff. Some of the initial setup was a little odd-I mistakenly tried setting things up in Windows before throwing that all out and giving it another go as a full cygwin setup, which worked a lot better! Hopefully, by the end, he’ll have a good grasp on TurboGears, and I’ll have a decent beginner’s grasp of TurboGears and Python! The SQLAlchemy stuff looks to have searching capabilities that are similar to some of what nHibernate has with its Criteria tools. (which, admittedly I haven’t looked into much-I’ve pretty much used HQL thus far)

Demeter’s Law

Friday, January 18th, 2008

So, I’m still working my way through The Pragmatic Programmer (yeah, I’ve been busy) and just went through the chapter on Demeter’s Law. I have to say, I’m a little skeptical. The general idea is that you only use objects and methods of objects that your current method(or class) “owns” directly, not things that other objects own. So for example, if your object takes a “Customer” object containing a “Person” object, you should not call Customer.Person.FirstName, because you don’t own the “Person” object. Instead, you should use a wrapper getter in Customer that gets FirstName from Person (CustomerFirstName, say). And if Customer was inside of Order, and you wanted the Customer’s first name from Order, the proper thing to do would be to wrapper the CutomerFirstName up, maybe with BuyerFirstName.

SO, instead of Order.Customer.FirstName

you’d have Order.CustomerFirstName

And THAT you can safely call. What’s the reasoning for this? well, this allows all kinds of changes to Person and FirstName, and once the class is changed in Customer, nobody else needs to know anything happened. since everything else is a wrapper, everything else will just work. Problem? well, Order, instead of being a stripped-down class just dealing with order-related stuff ends up possibly being an enormous class full of Customer and Person wrapper stuff, and depending on what other objects Order has, it could be even worse. Pragmatic suggests dealing with this by using a code generator… Frankly, though I see how coding this way would be safer, the idea of having huge amounts of wrapper crap kinda feels like a smell. Maybe this is worth it, but I’m not quite sold yet on it being worth the clutter. I’m going to have to think about this one a bit more.