Wednesday, August 16, 2006

Rolling Back After Database Tests - DataRollbackAttribute

If you do a lot of integration testing against a database and find you want to clean up at the end of each test then I highly recommend you look at the DataRollbackAttribute which is discussed in this blog entry.

I particularly like the way the attribute is used, very elegant and works very nicely. You can also modify the attribute to use TransactionScope, NHibernate transactions or whatever you need.

The only slight problem with it is that some of my tests were causing odd DTC errors when I ran them after applying this attribute, however I'm hoping that it will work properly when we move using NHibernate transactions (simply because we'll have a much simpler transaction management scheme than we do now).

Having said that at some stage I need to look at Ndbunit which also seems like it might be useful.

Share This - Digg It Save to del.icio.us Stumble It! Kick It DZone

NHibernate + Generics = Small Codebase

My company are going to use NHibernate for a few projects we're doing. I was heavily pushing for its usage because I believe it allows OOD, DDD and TDD (all the D's). However I've been amazed to find just how easy it is to use.

Essentially myself and another developer have been putting together a team project that the existing domain classes will move into. The most time consuming part is creating the mapping files for our existing classes, but if there isn't a tool out there to do that then we can write a stupid one that does the simple stuff.

Anyway its when you put generics and NHibernate together that I think things really get good.

For example to write an integration test that tests that persistence is running well you just need to do this:

  1. Create an instance of the class your testing.
  2. Use the NHibernate session to save it to the database and then flush the session.
  3. Evict it from the session. This needs to be done because NHibernate uses an identity map so if we don't evict then it will just return the same object (from memory) when we ask for it in the next step.
  4. Get the object, by ID, from the session. Since we evicted it in the last step this will cause NHibernate to reload it from the database.
  5. Compare the two objects (I wrote a reflection based helper class that does this automatically when passed two objects).

You might ask what the point of this test is, well it verifies that my database and HBM files are correct and that everything is working end to end.

I also think its worth writing these tests because its so easy, other than the first line of code you can reuse everything from one test to another by combining NHibernate with generics. To me this is wonderful and a big advantage of moving to NHibernate.

Share This - Digg It Save to del.icio.us Stumble It! Kick It DZone

Monday, August 14, 2006

NDepend - Analysing Your Dependencies

A while back I started reading Robert Martin's excellent Agile Software Development, Principles, Patterns, and Practices book.

The books very strong on object orientation and a large section is devoted to how to manage dependencies between different parts of your application. Whats great is he gives some metrics you can use to analyse the dependencies in your application, which is a great thing to do.

Anyway the good news is theres a tool for .NET that applies Robert Martins metrics to an arbitrary bunch of assemblies then does a good job of displaying them, namely NDepend.

Unfortunately there are a few problems. Firstly, as many sites point out, it runs over the IL so the cyclomatic complexity figures may be a bit untrustworthy (this is admitted at the Website). No biggy.

Other problems are more difficult to deal with, for example the fact that enums are treated as concrete classes during the analyses so skew the results...which can be a big problem and in fact kind of put the breaks on my evaluations of the tool.

This is probably a case where the tool needs to be changed to use more knowledge about the language its analysing.

Anyway I suggest you have a look at the tool and book, in fact might be worth getting the C# version of the book which is out now (reminds me to go buy it!).

Share This - Digg It Save to del.icio.us Stumble It! Kick It DZone

Saturday, August 12, 2006

CSLA

We've been looking at this framework at my work for a while as we believed NHibernate + CSLA could allow us to get the advantage of an approach like .netTiers but with a few advantages:

  1. No need to generate large amounts of code.
  2. We would only use the features we need.
  3. Seperate, to a large extent, the design of the domain and database.
Anyway although CSLA seems like a good framework if your not going to use the mobile objects approach, and we aren't, then it adds a lot of complexity for very little gain. In particular for a Web application I'm not sure how realistic CSLA is. In our case we didn't like the delegate based rule code, we don't need multiple level undo, we don't need dirty tracking....

Having said that the binding code and a few other bits could be useful.

One bit I did find very odd in my reading about CSLA was the fact that call your persistence code from your domain objects was justified for encapsulation reasons and to avoid using reflection as it can hurt performance.

The reflection argument seems pretty redundanct as CSLA itself uses reflection quite widely and the cost of reflection isn't likely to be noticable compared to things like database or network calls.

Likewise the encapsulation argument seemed a bit questionable, there are certainly good arguments for not putting your data access code in your domain/business objects and indeed for not accessing the data access code from your domain objects.

Anyway so far we've used CSLA pretty sparingly and even the bits we have used, like the validation framework, are slowly being engineered out.

Share This - Digg It Save to del.icio.us Stumble It! Kick It DZone

Microsofts Guidance Explorer

I've been using the Guidance Explorer from time to time and I have to say its quite good.

I find its quite an easy way to access the patterns and practices content, though one problem I do have is that some of the guidance isn't necessarily of the quality I'd expect.

In particular I found some of the exception related advice highly questionable, however I would hope this will improve over time.

Share This - Digg It Save to del.icio.us Stumble It! Kick It DZone

ASP.NET Provider API's - Just how good are they?

A while back we looked at using the provider pattern in our ASP.NET application and I was a little unimpressed with the API so I reported it.

To me lack of consistency seems to be an unfortunate feature of the class libraries Microsoft provide and I hope at some point they try to address it.

Share This - Digg It Save to del.icio.us Stumble It! Kick It DZone

TypeMock - Helps Improve Your Designs?

If anyone ever reads this blog, and if they've used TypeMock, then I'd be interested in what they thought of it. For those of you who haven't used it you should take a look if your doing TDD as it allows you to use mocking without redesigning your code.

For me the question is whether designing your code for testability is actually a good idea, if so using TypeMock too much is a bad idea.

Personally I'm much in favor of TDD but I think I disagree with most of the books/Websites because I don't think that decoupling your code to allow mocking necessarily results in particularly good designs.

Say I want to test a class that relies on a concrete Foo class . One way to get a mock Foo in for testing would be to create an IFoo interface, implemented by Foo and a new MockFoo class. To get the MockFoo into the class being tested I'd use something like IOC or a factory. An alternative would be to use NMock to create a dynamic mock for Foo, again its up to me to get the mocked object into the class being tested.

In the past I've tried doing this sort of decoupling for testability but my view is now that it adds unnecessary complexity. I also found that the interfaces/designs I was producing didn't necessarily decouple the software in ways that were later useful.

Thats where I think TypeMock is strong, I can use TDD to design the interfaces of my classes for normal usage and use TypeMock to mock out depedencies of the class being tested. Then once my class is in real usage I can look to decouple when and if its needed.

Share This - Digg It Save to del.icio.us Stumble It! Kick It DZone

DLinq - How it fits into TDD/DDD

For a while theres been a discussion at my company about what way to improve the development of a few applications. The applications are all quite data-driven but some of them will also need to have relatively complex business log in them.

I've only just joined and my joining coincided with the the company starting to use agile development (hurrah), it also coincided with a move to reducing the amount of code written. In particular to avoid writing the boring CRUD code to access the database.

Although its a small team we were split into people who think that, for the less data centric applications, we should use NHibernate/TDD/DDD/OOD and people who want to refactor the database and then use it as the starting point for the object model (using netTiers/CodeSmith to generate everything with a set of "OO" classes on top).

My view was that the NHibernate approach was very strong, it allows good seperation and the creation of a proper domain model. However its been an interesting discussion and I'll be interested to see how Linq fits into it all, though it seems to me it will support the data-centric design approach AND the more behavior (object-oriented) approach.

Time to start playing with DLinq methinks.

Share This - Digg It Save to del.icio.us Stumble It! Kick It DZone

The Edinburgh Fringe

Living in Edinburgh the month of August involves muttering under your breath about how busy everywhere is and taking advantage of all the comedy shows that are on (muttering under your breath whilst sitting in the audience ofcourse).

Anyway I went to see Richard Herrings show ménage à un last week and its highly recommended. I'm a big fan anyway but this show is superb, balancing perfectly between trying to offend the audience and keeping them going along with him. Hurrah for Herring.

Share This - Digg It Save to del.icio.us Stumble It! Kick It DZone