The Inquisitive Coder - Davy Brion’s Blog

Thinking outside of the typical .NET box

Is SQL going the way of Disco?

Posted by Davy Brion on July 22nd, 2008

How many developers do you know who can write solid SQL queries? Practically every developer knows SQL, but there aren’t too many who know how to use it well. Now don’t get me wrong, i am by no means a SQL guru… but i like to think i can hold my own when it comes to writing ‘good’ queries. With the advent of querying techniques that shield you from directly using SQL (like the LINQ syntax, or NHibernate’s Criteria) the number of developers with solid SQL knowledge will probably only get smaller and smaller in the coming years. I wonder if in 5 years time, developers with good SQL skills will be as rare as politicians that don’t take money, athletes that don’t use performance enhancing drugs, doctors who care about your health instead of their wallet, gangsta rappers who actually used to be ‘gangsta’, single women that don’t… well, i’m sure you catch my drift.

Pretty soon, most .NET developers will be using nothing but LINQ statements to query their databases. Obviously, a part of the problem will shift from lousy SQL statements to lousy LINQ statements. That’s pretty much unavoidable, given the ratio of ‘bad’ developers out there. But the productivity gains from using LINQ (or Criteria) over directly using SQL will probably become more and more important as time goes on. Not to mention the fact that LINQ is ’sexy’ and will be pushed heavily by Microsoft.

Would that actually be a bad thing though? I kinda think the situation is similar to the shift we’ve seen over the years in programming languages. We used to write code in assembly language. Well, ‘we’ didn’t, but the poor souls who were in this industry before us did. Then people moved to higher level languages (like C) for the increased productivity. Fast-forward a couple of years and people moved to development platforms where the actual code would be compiled to machine code ‘just-in-time’, at runtime even! Oh, the horrors!

In each and every one of those shifts, there were developers who said “i don’t care, my hand-written code is definitely gonna outperform that”. And at the beginning of those shifts, this was probably true for a lot of developers. But as time went on, the newer technologies pretty much always ‘won’ because of the productivity gains. Even if they were slower in direct comparisons. Then again, those technologies evolve as well and they usually get faster along the way. The runtime performance of current Java byte code is a lot better than it was in the first 3 years that Java was around. I’m not sure if the .NET runtime has actually gotten faster in the past couple of years, but then again, it probably picked up on a lot of lessons learned in the Java world before .NET was released.

One thing that has happened consistently is that compilers have always gotten better. Well, they’ve always gotten better at optimizing the code they generate. This leads to situations where the same code actually runs better when it’s compiled with newer versions of a compiler (well, if it still compiles…). Obviously, this isn’t true for all code, but it usually is true for certain statements, or certain frequently-used bad-performing techniques (string concatenation for instance).

I wouldn’t be surprised to see the same thing happening with LINQ providers (or other SQL querying abstractions for that matter). The SQL that is generated by them is pretty good already. And it’s never gonna get worse… they will always become ’smarter’ and they will generate better SQL statements in future versions. When that happens, we all benefit from that.

Sure, there will always be die hard SQL fans who keep writing their own SQL queries and there will always be edge-cases where hand-written SQL queries will be better than those generated by higher level libraries. Especially if those SQL queries were written by Disco Stu. I mean SQL Ben (laugh Ben, it’s just a joke ;)). But i do believe that will only be true in edge-cases.

Posted in Software Development | 3 Comments »

Mocking Dilemma, Solved!

Posted by Davy Brion on July 21st, 2008

A coworker of mine, Tom Ceulemans, has provided a very nice solution to the mocking dilemma i posted earlier. I like this solution so much, i think it deserves its own post :)

The original question was basically: how do you do access a protected member in a test? I didn’t want to make it public because it is a base class, and i was hoping to avoid making it protected internal and then exposing the internals to my tests assembly.

Tom’s solution is very nice, and IMO, very clean as well. The trick is to create a derived abstract class from the class you want to test and then write the TestFixture as an inner class of the newly created class.

The test shown in the previous post would now look like this:

    public abstract class MyAbstractClassTestWrapper : MyAbstractClass

    {

        [TestFixture]

        public class MyAbstractClassTests

        {

            [Test]

            public void CallsProtectedAbstractMethod()

            {

                var mocks = new MockRepository();

                var myObject = mocks.DynamicMock<MyAbstractClassTestWrapper>();

 

                myObject.Expect(m => m.DoSomethingSpecific()); // <= no more compile error

                myObject.Replay();

 

                myObject.DoSomethingInteresting();

                myObject.VerifyAllExpectations();

            }

        }

    }

I like it :)

Posted in Test Driven Development | 11 Comments »

The Known Type Provider

Posted by Davy Brion on July 21st, 2008

Manually dealing with KnownTypes in WCF is a bit of a pain. Well, at least if you have more than a few derived types you want your WCF services to serialize/deserialize. Luckily for us, there is a way to do this automatically. Using the ServiceKnownType attribute you can specify a class and a static method of that class which will provide the Known Types. An example of this can be found on my ever-recurring Process service method:

        [OperationContract]

        [ServiceKnownType("GetKnownTypes", typeof(KnownTypeProvider))]

        Response[] Process(params Request[] requests);

This works, but i don’t want to write a class every time i want to specify some known types of a base type in a service method. So i modified that KnownTypeProvider class so that we can now do the following:

            KnownTypeProvider.RegisterDerivedTypesOf<Request>(mySharedAssembly);

            KnownTypeProvider.RegisterDerivedTypesOf<Response>(mySharedAssembly);

This registers each derived type of Request and Response in the mySharedAssembly reference (of type Assembly) with the KnownTypeProvider. You can register as many known types for as many base types as you want. Obviously, you have to do this before you start hosting your service. You also need to do this client-side, before you start using your service proxies.

You’re probably thinking “wait, won’t that return all registered known types for every service call where you use this stuff?”. That wouldn’t be good, so i took care of that. The ServiceKnownType attribute of WCF requires your class to implement a method which receives a parameter of type ICustomAttributeProvider and which returns a list of types. The ICustomAttributeProvider parameter is actually a MethodInfo instance at runtime. This makes it possible to inspect the current service method’s parameters and return type. That information makes it possible to only return the KnownTypes that are relevant to the current service call. All of this sounds expensive with regards to performance, so obviously this is all cached… so you only take the performance hit on the very first request of each service method where you use this.

The KnownTypeProvider makes it extremely easy to register your KnownTypes in a generic way, can be reused across multiple service methods and it only returns the relevant KnownTypes for each service method. And it does so with a minimum of performance overhead.

You can find the code in my public svn repository as a part of my utility library (the tests can be found here). At this moment it is one of the only classes in the library, because i still have to add some other stuff before i’ll put a ‘proper release’ online :)

kick it on DotNetKicks.com

Posted in WCF | 1 Comment »

Yet another utility library

Posted by Davy Brion on July 20th, 2008

Some of the stuff i’ve posted here lately seems to be somewhat popular and i’d like to make those things available to anyone who wants to use them. Scraping code together from blog posts is not only an annoying job, it’s also not entirely legal due to no explicit license being mentioned. So because of this, i’ve decided to just make this stuff available in a utility library with the intent of making everything easy to (re)use ‘out-of-the-box’. This library will be BSD-licensed so you’ll have a couple of options if you want to use it or parts of it…

The two biggest things i’d like to make very easy to use are of course the whole WCF batching stuff, and probably some base classes for my testable ASP.NET webforms. There will be a couple of smaller, yet interesting parts in there as well though… My KnownTypeProvider class from the WCF batching article also received some interest so i’m going to add that in a more reusable way as well, without it having anything to do with the whole batching issue.

I’m still assembling the various parts and making some modifications/improvements along the way, so i’ll probably release the code somewhere in the coming week, depending on how much time i’ll have to work on it.

Posted in Software Development | No Comments »

Mocking dilemma

Posted by Davy Brion on July 20th, 2008

Suppose you have the following abstract class:

    public abstract class MyAbstractClass

    {

        public void DoSomethingInteresting()

        {

            // some stuff would go here…

            DoSomethingSpecific();

            // more stuff would go here…

        }

 

        protected abstract void DoSomethingSpecific();

    }

What do you do if you want to write tests that set expectations on the protected abstract method?

Ideally, i’d be able to do this:

        [Test]

        public void CallsProtectedAbstractMethod()

        {

            var mocks = new MockRepository();

            var myObject = mocks.DynamicMock<MyAbstractClass>();

 

            myObject.Expect(m => m.DoSomethingSpecific()); // <= compile error

 

            myObject.DoSomethingInteresting();

            myObject.VerifyAllExpectations();

        }

But as the comment points out, this causes a compiler error because the DoSomethingSpecific method is inaccessible to anything except derived classes.

So what is the best way to deal with this? For this example, i could manually create a derived class, set a flag in its DoSomethingSpecific implementation and then assert on the value of that flag. But that approach doesn’t really scale well to complexer classes. If i want to use a mocking framework for this, i have two options (that i can think of right now): i either make the abstract method public so the above code would compile, or i can make it protected internal and then allow my internal members to be visible to my Test project.

I dislike both approaches. I wholeheartedly agree with the “Public vs Published” stance, but making a method that really should only be accessible from derived classes public merely for testing purposes just doesn’t sit well at all with me. Changing the abstract method’s visibility from protected to protected internal seems to be the lesser of two evils, although i’ve always seriously disliked the internal keyword and especially extending the visibility of it through the InternalsVisibleTo keyword. This approach just screams “HACK!” to me, but in this case i can’t really think of anything better.

If anyone call tell me what the best way would be to deal with this specific scenario, please do share :)

On a sidenote: i am happy about the fact that i do seem to periodically reflect on the merit of my many ‘rules’ such as “thou shalt not expose thy privates to an even larger group than the internal circle to whom they’ve already been shown”. I’ve already mentioned that i’ve had it with rules that serve only theoretical purposes without any practical merit… there’s no reason why my own rules should be excluded from this :)

Posted in Test Driven Development | 20 Comments »

Using TeamCity’s NUnit support, while keeping the output around

Posted by Davy Brion on July 19th, 2008

I’ve been playing around with TeamCity a lot lately. It’s really amazing how easy to use and powerful it is. Definitely my favorite CI server for the time being… i’m even running it at home now for my personal projects.

There was one issue at work that was somewhat hard to fix though. TeamCity has fantastic integrated support for NUnit, but unfortunately it doesn’t write NUnit’s output to XML files like nunit-console.exe does. After a bit of browsing, i found a post on the TeamCity forums that discussed a workaround on how to get the output. Unfortunately the workaround is a bit cumbersome as it requires you to you create an XML file which contains the arguments that TeamCity would pass to its NUnitLauncher task.

I believe in ’script reuse’ as much as i believe in ‘code reuse’, so every project’s build script merely imports a generic build script and then it just overwrites some variables that the generic script uses and then it kicks off the usual build process. Since all of our projects will now be built by TeamCity Build Agents, and we definitely need to have NUnit’s results xml file i wanted to automate this whole thing as much as possible.

So instead of having to create (and thus, maintain) a cumbersome xml file for each project, i wrote an MSBuild task that would generate the xml file containing the arguments on the fly during the build, and would then pass those xml arguments to TeamCity’s NUnitLauncher. This way we get to keep all of TeamCity’s NUnit integration goodness, while still keeping the NUnit result files around as well.

So now in my MSBuild file, i can just do this:

    <CreateItem Include=**\Bin\Debug\*Tests*.dll >

      <Output TaskParameter=Include ItemName=TestAssemblies />

    </CreateItem>

 

    <BuildTeamCityNUnitArguments HaltOnError=true HaltOnFirstTestFailure=true

                                HaltOnFailureAtEnd=true TestAssemblies=@(TestAssemblies)

                                NUnitResultsOutputFolder=TestResults

                                PathOfNUnitArgumentsXmlFile=nunitarguments.xml />

 

 

    <Exec Command=$(teamcity_dotnet_nunitlauncher) @@ nunitarguments.xml />

As you can see, there is nothing project-specific in there so this just integrates nicely into each build that we need. The only limitation to this approach is that TeamCity’s NUnitLauncher will write one NUnit result file for each assembly containing tests. Apparently there is no way in the current version of TeamCity to get it to combine those results. We use an extra tool to analyze the NUnit output after the tests have run, and unfortunately it requires all of the results to be in one file. I looked around for a way to merge the output of several NUnit result files, but i didn’t find something that was already available. So i wrote another MSBuild task that would merge the output into one xml file:

    <CreateItem Include=TestResults\*.xml >

      <Output TaskParameter=Include ItemName=NUnitOutputXmlFiles/>

    </CreateItem>

 

    <NUnitMergeOutput NUnitOutputXmlFiles=@(NUnitOutputXmlFiles)

                      PathOfMergedXmlFile=TestResults.xml />

Now we finally have all of TeamCity’s goodness while we still get to run our post-test analysis on the NUnit result file that contains the testresults of all the test assemblies :)

You can find the code of the MSBuild tasks here. They are BSD-licensed so feel free to use them if you need them.

Posted in Continuous Integration, MSBuild | 1 Comment »

Many projects don’t lead to a good solution

Posted by Davy Brion on July 17th, 2008

Chad Myers discusses an extremely common anti-pattern in the .NET world that usually annoys the shit out of me: too many projects in a solution. I’ve seen it way too many times as well. A lot of people really want to split everything up into a bunch of assemblies while there’s usually no good reason whatsoever. Chad brings up a few issues about this approach, but there are more reasons not to like this.

For starters, i always feel like i’m getting lost in the Solution Explorer when i have to work with these kinds of solutions. Granted, i am a Mac user so that might have something to do with it. I gotta use Vista at work all day and after 3 weeks i still feel hopelessly lost in it. Anyways, easy navigation is very important to me. Especially for my productivity because every time i feel i’m wasting too much time looking for a specific file or a specific namespace of classes, it hurts my rhythm. And that rhythm is something you really don’t want to get disturbed by stupid little issues when you’re working on some interesting stuff. Granted, if the Solution Explorer was a bit nicer to use (Track Active Item sucks half the time, disabling it makes the other half suck) this probably wouldn’t be so big of an issue, but still, it’s important to me.

Secondly, many projects slows compilation time which hurts my TDD feedback loop. If you have a lot of projects in your solution your compilation time increases. If you’re doing TDD, you’re constantly adding/editing code and you’re constantly compiling to run a couple of tests. The difference in compilation time between 3 projects or 10 really adds up, even for one day. It leads to frustration, which leads to me taking more breaks from coding. Again, this breaks my rhythm of coding.

And finally, i’m getting tired of developers using purely theoretical arguments to defend practices/approaches that have no practical merit whatsoever (on a side note: i call this the Intellectual Masturbation Syndrome). How often have you heard people say “oh but i want to be able to reuse these bunch of classes somewhere else later on without having references to all this other stuff as well”. I’ve heard it far too many times, and i’ve almost NEVER seen anyone actually reuse one of those assemblies somewhere else. If you do need it somewhere else later on, split it up at that point. You actually have a valid reason to do so in that situation. But no earlier than that. Or the typical “i think every layer should be in it’s own assembly”. Yeah that sounds good and all, but i’ve never seen a real benefit to doing that. If you’re worried about your developers using classes from layers in places where they shouldn’t be using those classes, you probably have bigger things to worry about. How about teaching them why they shouldn’t do that instead of just preventing them from doing it? If you don’t trust your developers, they will always disappoint you. Trust them and more importantly, teach them.

So here’s my recommendation: ONE project per physical deployment. That’s right. A web application that doesn’t use a remote service? One assembly. A fat client windows app? One assembly. A smart client windows app? One assembly for the client, one for each application-server deployment and one more assembly for types that are shared between the client and the server. So if you’re hosting 10 services on one application server, just go for one assembly server-side. Add another project for your tests, and you should normally end up with between 2 and 5 projects in your solution, depending on how many physical deployments you have.

Btw, i’m currently using two projects for tests… i have a .Tests project, and also a .SlowTests project. Tests that use the file system or the database or anything else like that go in SlowTests. Everything else goes in Tests.

Posted in Software Development | 8 Comments »

My Guide To Effective Test Driven Development

Posted by Davy Brion on July 14th, 2008

When it comes to Test Driven Development (TDD), there are 4 kinds of developers:

  1. The kind that doesn’t do it, and doesn’t care about it
  2. The kind that doesn’t do it, but is intrigued by it and would like to learn more
  3. The kind that does it, but wonders why it’s not as easy as everyone claims it is
  4. The kind that does it effectively

I don’t spend time on the first category, and i consider myself to be of the last category so this post is targeted at the 2nd and 3rd categories. Hopefully, some of the stuff in this post will help you on your way to using TDD effectively. It’s basically just a list of stuff to keep in mind while doing TDD. So without further ado, i present you with my guidelines to effective TDD:

Keep your tests small and focused

This is probably the thing that most people get wrong, which leads to all kinds of problems. Developing software is all about managing complexity. How can you effectively do that? By trying to keep things simple. No matter how smart or gifted you are, the more complex you make things, the easier it is for you to make mistakes. You do need a high-level picture of how things should work in your application or system, but the actual implementation is something that needs to point itself out while you’re working on it. Always start small and simple. Think of one small feature, write a test for that, and then write the code to make that test work. While you do this, make sure you don’t get sidetracked by other features that suddenly pop into your head. Make a note of them, and when you’re done with the test and the code you were working on, proceed with writing a test for one of the things you thought of earlier. But the key is to keep adding small features (after having written small tests for them of course). Some of you are now thinking “but what if i need to add a big feature?”. Well, then you need to break that ‘big feature’ down into smaller parts.

After a short while, you will have accumulated a bunch of small things that work. Because you implemented them in a focused way, and because you were forced to think about how you’d use those features in your test before actually implementing them, you normally should’ve ended up with a bunch of little parts that are easy to use. And your tests already prove that they work. That means you can start using these parts (they can be classes or just individual methods) to get other things (new features) working. Whenever you use a part that you already finished earlier (including its tests of course, since you wrote them before you wrote the finished code of the part) you don’t need to worry about the correctness of that part in the tests that you’re writing for whatever new feature you’re working on. This is something that a lot of people get wrong. I can not stress this enough: the correctness of dependent code should be covered by its own tests!

I believe the approach outlined above really is the key to effective TDD. Obviously, there are more guidelines, but this is the one that will have the biggest impact on your experiences with TDD.

Use dependency injection

Dependency injection (DI) is a really simple technique that offers more advantages than simply making TDD easier to do. But the focus of this post is on TDD, so i won’t get into the other advantages. You can find an introduction to this technique here.

Keep your tests fast

A slow test is like body oder: the problem is only gonna get worse until it’s taken care of properly. If you have a bunch of smelly people in one room, you really don’t want to be there. It’s the same thing with a bunch of slow tests… nobody wants to be around those. That means that most people won’t run the tests frequently, which is a shame. So you really need to keep those tests fast if you want people to run them often. The best way to achieve this is to mock/stub/fake your dependencies in your tests. If you’ve used DI properly, this is really easy to do. Every dependency that can slow down your test, or make it unreliable because of factors that are external to the test, should be mocked/stubbed/faked. Some of you might be wondering: “but how can we be sure that the code really works when it’s using the real dependency?”. Well, the real dependency should be covered by its own suite of tests that thoroughly tests the behavior of that code. If the correctness of the dependency is properly covered by its own tests, why on earth would you subject yourself to the downsides of using the real dependency in tests that have to cover other parts of code? Using mocks/stubs/fakes allows you to keep your tests focused on that piece of code they actually should cover and it helps in keeping your tests fast. What’s not to like?

Minimize access to the database in your tests

A lot of people use their database in a lot of their tests. This usually leads to slow tests, and excessive test set up to satisfy all relational constraints which in turn leads to fragile tests. None of this is beneficial to your productivity. But of course, you do have to test the code that uses your database. The key is to write tests for each specific database activity (a query or any other kind of statement). Write tests that verify that a query returns the correct values. Write tests that verify that a query does not return incorrect values. Be sure to cover everything you want to cover to make you feel secure about that query. And stop right there… every other part of your code that needs access to this specific query should be talking to an object (a dependency) instead of performing this query itself. Use mocks/stubs/fakes instead of the real dependency in the tests of that other code. By replacing your real data access code with mocks/stubs/fakes in your tests, you can make your tests very fast, and you can usually simplify the set up of the tests as well.

Don’t over specify your tests

If you’re using mocks, be sure not to write tests that are too tightly coupled to the implementation of the code you’re testing. If you frequently have to modify your tests because you changed the implementation of the class without it affecting the observable behavior of the class, then you probably have over specified tests. Mocks can be a very effective tool to increase both your productivity and the quality of your tests, but when used wrong it might lead to a lot of unnecessary extra work. This is quite an extensive topic in its own right, so i can’t do it any justice within the context of this post, but it is something to keep in mind even if you don’t have experience with mocking yet.

Don’t be afraid to throw tests away

If you’re doing some heavy refactoring, you might break a few tests. Depending on how extensive the refactoring is, you might break a lot of tests. A lot of people feel that they have to keep those tests around… but that’s not always the case. Take a close look at the tests… Do they still make sense after the refactoring? Perhaps you need to refactor those tests as well instead of going through hoops to get all of them working again. Sometimes you may need to modify a few tests to get them working again… But sometimes, those tests simply aren’t valid anymore. If they are, throw them out instead of wasting time on them. If the assertions of the tests are still important, but it’s too much work to modify the tests, it may actually be less work to simply rewrite them against the refactored code. There is no one right answer here, but you may need to learn how to look at your tests from more angles then you may currently be used to doing.

Conclusions

This is by no means a definitive list of things that you need to keep in mind while doing TDD… But i do think these guidelines may be the most important ones. If you follow these guidlines, you should end up with fast tests that are easy to understand, not a pain in the ass to maintain, and most importantly: verify the correctness of the code under test.

Once you have that, the code and design of your system is probably pretty flexible and it will be easier to adapt as time goes on. And this is probably the single biggest advantage of doing effective TDD.

It might take you a while to learn how to do all of this effectively. But that’s ok… there’s nothing wrong with that at all. Baby steps are better than no steps at all, right? Just keep in mind that if doing TDD hurts, you’re not doing it right… yet. Figure out where the pain comes from, try to fix the cause and try again. It’s a repetitive process and you wont get it right immediately. None of us did.

Posted in Test Driven Development | 2 Comments »

Introducing… Stuff!

Posted by Davy Brion on July 13th, 2008

I’ve added a new page to the site, called Stuff. As the late, great George Carlin would say: it’s just “a place for my stuff”. I’m gonna use it to keep various little tools or libraries or projects that i wrote (or still have to write) and that i want to share with people in case they might find it useful.

Now don’t get your hopes up, i only have one tiny little MSBuild task in there for now, with probably a few more to come in the future as i’m now using MSBuild exclusively for building projects. But it won’t just be MSBuild stuff… it can pretty much be anything, depending on what kind of things i feel i need to write in the future.

It’ll all be open source of course, and you can find a link to the SVN repository on the page as well.

Posted in About The Blog | No Comments »

Can someone tell me why i shouldn’t drop WCF?

Posted by Davy Brion on July 12th, 2008

I’ve written a few posts about WCF lately… mostly about how to batch WCF service calls. In case you didn’t know, i consider batching support a MUST-HAVE feature. Since then, my WCF services all extend the following interface:

    [ServiceContract]

    public interface IService : IDisposable

    {

        /// <summary>

        /// Processes each request within the same service call

        /// </summary>

        /// <param name=”requests”>each request</param>

        /// <returns>the corresponding responses</returns>

        [OperationContract]

        [ServiceKnownType("GetKnownTypes", typeof(KnownTypeProvider))]

        Response[] Process(params Request[] requests);

    }

I wanted to keep providing specific service methods for each kind of request, but each service implementation would delegate the handling of the requests to the Process method, which in turn would delegate the handling of the request to a specific request handler type which was associated with the request type. So basically, all of these specific service contracts which extend this service don’t really offer me anything extra.

So now i’m wondering why i’m even bothering with the specific services anymore… it only leads to more work: more service interfaces and more proxies and in the end, everything is routed to the Process method. So what’s the point of these specific services anymore if you don’t have batching support out of the box? If i have to go with a generic Process method to get batching support (again, which i consider a MUST-HAVE feature), why am i even bothering with Services at all? Essentially, i’m using a service bus with WCF. Why shouldn’t i just move to NServiceBus which is actally perfectly suited for this kind of work?

For the record, i’m not trying to flame or anything… i really want to get some answers to the following question: why shouldn’t i drop WCF and the whole service layer thing in favor of a real service bus and Messaging in general?

kick it on DotNetKicks.com

Posted in WCF | 10 Comments »