Tuesday, September 29, 2009

Building a site – What am I doing wrong? (3)

Submit this story to DotNetKicks

Part 3 - Slimming down my classes

So I did a brief overview of my structure in the previous post, and you might remember to significant classes/interfaces - IEntity and DacBase.

I told you I'd throw patterns out the window and see where it led me... Well, to be honest, it leads me back to, yes you guessed it, patterns! :)

In my previous article, I had put CRUD-methods directly on my data-models like this:



To be honest with you (and you probably already see the problem) - This would be a major hassle everytime I need a new model, because I have to implement all the CRUD and relation work everytime. The beauty of it is that all the CRUD-methods are totally generic, and don't give a crap about what object they are hosted in, as long as they have something to do with IEntity.
So I thought, let's move them out from the models and into the DacBase, that all my models needs to inherit from.

So what I end up with is.... *drumroll*




As you can see, I have no methods conserning CRUD (Well, actually the User-model still has some data-fetching in the last constructor, but that'll go away too).

Ultimately I'd like for my model to only have one empty constructor that calls the base-constructor, and of course my fields.

But this is actually pulling me towards the Single Responsibility Pattern - And that's just because it felt more practical and easy for me. Not because I decided to follow that pattern at all.

I just want to add that all my integration-tests ran perfectly after I changed my code (and the reason for my tests is not that I want to do TDD, but simply because it's so much easier than to pull up a browser in debug-mode and click all around my site :))

Conclusion Part 3
As you can see, I've already started following one of the many patterns out there, so let's see how many patterns I end up following simply by ignoring them :)

Stay tuned.


Oh! And after posting my previous post got hit by a series of spambots auto-kicking it and ultimately getting me banned from DNK :( Working on getting that fixed.

Submit this story to DotNetKicks

0 comments: