Dependency Injection of ALT.NET

It’s common to hear developers promote layering as a means to provide extensibility. The most common example, and one I used in Chapter 2 when we looked at interfaces is the ability to switch out your data access layer in order to connect to a different database. If your projects are anything like mine, you know upfront what database you’re going to use and you know you aren’t going to have to change it. Sure, you could build that flexibility upfront - just in case - but what about keeping things simple and You Aren’t Going To Need IT (YAGNI)?
I used to write about the importance of domain layers in order to have re-use across multiple presentation layers: website, windows applications and web services. Ironically, I’ve rarely had to write multiple front-ends for a given domain layer. I still think layering is important, but my reasoning has changed. I now see layering as a natural by-product of highly cohesive code with at least some thought put into the coupling. That is, if you build things right, it should automatically come out layered.

The real reason we’re spending a whole chapter on decoupling (which layering is a high-level implementation of) is because it’s a key ingredient in writing testable code. It wasn’t until I started unit testing that I realized how tangled and fragile my code was. I quickly became frustrated because method X relied on a function in class Y which needed a database up
and running. In order to avoid the headaches, I went through, we’ll first cover coupling and then look at unit testing. (A point about YAGNI. While many developers consider it a hard rule, I rather think of it as a general guideline. There are good reasons why you want to ignore YAGNI, the most obvious is your own experience. If you know that something will be hard to implement later, it might be a good idea to build it now, or at least, put hooks in place. This is something I frequently do with caching, building an ICacheProvider and a NullCacheProvider implementation that does nothing, except provide the necessary hooks for a real implementation later on. That said, of the numerous guidelines out there, YAGNI, DRY and Sustainable Pace are easily the three I consider the most important.)
Previous
Next Post »