DB Normalisation and other DB points of view

Over the last couple of months I have heard the commments “its a very normalised database” or various implications that database X is “too” normalised. i often found this odd.
Why?

  1. By looking at a DB schema how do you know how well indexed the DB is?
  2. How do you know how many rows are in it?
  3. How do you know about it performance

I have always been of the opinion that you

  1. Normalise first, create a beautiful piece of art that is a nicely and appropriately normailised database.
  2. Add indexes.
  3. Write your sored procs.
  4. Check the performance on test data.
  5. Denormalise if nessecary, however if there were issues I would go over everything with a fine tooth comb first to ensure keys, indexes, constraints, triggers are all in place where appropriate, working as intended and not adding unessecary overhead. Then I would start denormalising.

Yes i am a fan of stored procs, especially for anything that actaully is DB intensive. As Gumble also points out (frequently) it adheres to a basic concept we use daily, encapsulation. Normal Objects and Layers/Tiers cant see into the next object/layer/tier, why should this be broken at the data access layer. Using SP’s also (IMO) aids in security and data control (well it can, anyone can butcher code) ;). The OO coders should not have to know the underlying data structure, they know what they want, they just need a means to get it. The Dba may be adding a whole bunch of stuff that the OO boys dont need to about, inactive flags, triggers, loggin, other extra columns, how the data is retireved etc etc… also this mean performance is left in the DB world and can be easily tested with out any manged code interfering.
One thing i do prefer is isolation of tests, i dont want to not be able to test individual units. A stored proc is a unit, a BusObj –> Repository –> DA–> db –> DA –> Repository –> BusObj is not a very succinct unit. If i want to test that vertical i still can however i want to be able to break it down too.

Then there is the point of ORM’s; I’m all for them, anything that gets rid of boring DA code, sweet! But sometime you have some complex resultsets and DB call that do not suit your run of the mill ORM’s. I do believe for basic database and when fast turnover of code is of higher importances, then something like NHibernate (perhaps with AR) can be handy… but i think it needs to be reviewed on a case by case basis, unlike some of my peers who believe NHibernate is the silver for all solutions…

no doubt more will come…

Framework Design Guidelines

have just started reading Framework Design Guidelines i must say that it’s a must read for anyone who writes code that other developer may have to read or use. Framewroks designers, class library designers, plug ins, widgets etc… basically anyone who codes in .Net.
Its a VERY fast read, not a lot of overly conspetual stuff and very well written. Hopefully most of it is stuff you already do, fortunately i have worked with and been exposed to some very good coders and thankfully i have picked up their good practices so alot of this is confirmation of ideas.
Read it, you will either pick up lots of good practices or confirm you are doing things right. Either way its a book you can read in a weekend with out too much of a problem.

Sandcastle – Documentation

We are coming to the end of the project here at work so needless to say all the documentation has to be handed over, including the API for the code base.
As we using .Net 3.0 Ndoc cant be used so its replacement sandcastle was our obvious option. Sandcastle being still a CTP is pretty clumsy to use, so here is some tips to get you going.

firstly check out the Gui or batch tools out there, some of them are pretty good. Check the codeplex stuff and SandcastleGUI.
Unfortunately this (specifically SandcastleGUI) wouldnt work for me as it tries to find DLLs that may be referenced even if not used (this may point to redundant ref in our code). as time was not on my side i decide to just run with the default console implementation.

…Anyway some tips.
Put all your Dll’s in one directory. Even if this means copying them all into a holder directory for the means of the documentation exercise. This includes anything that the dll’s may reference. Normally this is the case anyway, however with distributed systems, and gacs, and what not, it isnt always the case.

Assuming you install sandcastle in “C:\Program Files\Sandcastle” the below script will create your help file.

“C:\Program Files\Sandcastle\ProductionTools\MRefBuilder.exe” alpha.dll beta.dll gamma.dll /out:reflection.org

“C:\Program Files\Sandcastle\ProductionTools\XslTransform.exe” /xsl:”C:\Program Files\Sandcastle\ProductionTransforms\ApplyVSDocModel.xsl” reflection.org /xsl:”C:\Program Files\Sandcastle\ProductionTransforms\AddFriendlyFilenames.xsl” /out:reflection.xml

“C:\Program Files\Sandcastle\ProductionTools\XslTransform.exe” /xsl:”C:\Program Files\Sandcastle\ProductionTransforms\ReflectionToManifest.xsl” reflection.xml /out:manifest.xml

call “C:\Program Files\Sandcastle\Presentation\vs2005\copyOutput.bat”

“C:\Program Files\Sandcastle\ProductionTools\BuildAssembler.exe” /config:”C:\Program Files\Sandcastle\Presentation\vs2005\Configuration\sandcastle.config” manifest.xml

“C:\Program Files\Sandcastle\ProductionTools\XslTransform.exe” /xsl:”C:\Program Files\Sandcastle\ProductionTransforms\ReflectionToChmProject.xsl” reflection.xml /out:Output\test.hhp

“C:\Program Files\Sandcastle\ProductionTools\XslTransform.exe” /xsl:”C:\Program Files\Sandcastle\ProductionTransforms\createvstoc.xsl” reflection.xml /out:toc.xml

“C:\Program Files\Sandcastle\ProductionTools\XslTransform.exe” /xsl:”C:\Program Files\Sandcastle\ProductionTransforms\TocToChmContents.xsl” toc.xml /out:Output\test.hhc

“C:\Program Files\Sandcastle\ProductionTools\XslTransform.exe” /xsl:”C:\Program Files\Sandcastle\ProductionTransforms\ReflectionToChmIndex.xsl” reflection.xml /out:Output\test.hhk

“C:\Program Files\HTML Help Workshop\hhc.exe” output\test.hhp
pause

The only change you would then need for this script is to put the names of the DLL’s you actually want to document (ie replace alpha.dll beta.dll gamma.dll with your dll(s))… you may want to do this before running the batch file 😉

Copy and paste the script below into a text file make the dll name change on the first line sand save it as a batch file eg “documentation.bat”. Save it in the same folder as your dll’s and run it (click it).

This will give you an ultra generic help file, ala visual studio 2005 and all the rubbish .net 2.0 help files when it first came out.
I personally played with the xsl file to add in images etc to customise it a bit. i’m sure there are better ways to do it but i’m not spending any more time on some thing that i doubt anyone will ever read.
If you want them all spruced up, you probably want to check out the tools mentioned above.

EDIT: Sorry I fogot to mention that I dont name all of my XML Comment files “comments.xml”!
I usually name them the same as my dll’s with an xml extension eg alpha.dll has the comment file alpha.xml. This means I had to make a change in the sandcastle config file (“C:\Program Files\Sandcastle\Presentation\vs2005\configuration\sandcastle.config” in my case) line 63 from

to

to allow for all of the xml files to be a part of the help (which is what you really want now isn’t it!).
Just make sure there are only comment XML files (ie no other data xml files) in the directory or i guess it could go pear shaped. Alternatively perhaps name them *.dll.xml and use that as the data file filter.
EDIT 2:
To change the lines at the top & bottom of the main pages
[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]
&

Send comments about this topic to Microsoft.

Edit the shared_content.xml file found below.
C:\Program Files\Sandcastle\Presentation\*\Content\shared_content.xml

The eternal fight

It seems like there is always the eternal struggle between dev’s and the “non Dev’s”. You know what i am talking about.
BA’s who give innaccurate or incredibly loose requirements yet want you to build a something exactly how they or the customer want it…
PM’s who want you to do something that will take 5 weeks to do it in 1 and after you pull uber over time to finish it they then think they were right, it did only take 1 week… and it your fault that it hasnt been tested properly or fully completed…
Sales people who promise the earth and have no idea what nightmares they are putting the team into…. etc etc

And poor old us. The developer gets to shoulder the brunt of it… well so we think. A couple of things happened today that got me thinking, if the average Dev could properly comunicate then alot of these issues could be reduced. Firstly if there is a technical person who will be directly involved in the dev at the time of scope to check everything is on the right track… that may help (in the last 12 months I have been gob smacked how often this doesn’t happen). Hell, if the techincal person is presentable and articulate, let him/her come to the scope meetings or be involved in the sales process. Gee that sounds good! In my mind this is a double win, if handled properly. Then the major dev issues come up early, and if they dont, then it was missed by the dev anyway who can no longer point the finger.
Also picking your battles is always something we should be aware of. If you bitch and moan about everything, when i comes to a major issue the PM (or whoever) will just roll their eye’s as if to say ‘not this again’. Help the non devs to understand the business reason behind your arguement (delivery times & budgets are always good), after all you are suposed to be on the same side!
Its not all doom and gloom. I have been lucky enough to work with great teams such as Neotek, where although conversations got heated, they never got personal and it was always with the intent of making the code, product of processes better. Also the team i work with at Change are a very smart bunch for very different backgrounds where i would like to think we asa collective are learning alot from the others in the team (well I am!).
In short, just because you are a developer doesn’t mean you have to be a nerd. Getting people on side with you early on is a big help when things get tight at the tail end of projects (or even just iterations!).

sorry just a bunch of random thoughts, but i thought i would throw them out. 🙂

Rhino Mocks

CDS showed me a cool new tool today called Rhino Mocks. I have never used mocks before so was impressed nonetheless. Its helping make unit test actual unit test and not block of code that hit DB’s or file systems or this, that and the next thing (check out Micheal Feathers post re unit test rules here).
One of the coolest thing is it is written by an up and coming software geek who is only 24 and has a healthy list of accomplishments already. Check out the work of Oren Eini (AKA Ayende Rahien) here

"Catch all the exceptions, ALL OF THEM!!!"

“Catch all the exceptions, ALL OF THEM!!!” were the words that ran thru my head as a saw the dreaded
..catch(Exception)
{}…

WTF??!?!

a)Why would you ever catch all exceptions? You should know what exceptions are coming back at you catch specific ones and deal with them appropriately. The only* time I could ever see this being usefull is for logging purposes…
b)Dont hide exceptions… deal with them. Catch exceptions and doing nothing is pretty rank… at least comment the hell out of why you are doing it.
c)Get FXcop or some tool that tells you when there is rubbish code in your solution!

*OK there is probably a few areas, but not as many as some would think…

MVC and Building User controls (Specifically .Net ASCX files).

MVC and Building User control's (Specifically .Net ASCX files).

User Controls are a common way to develop reusable UI components that use a common layout or have common generic functionality. Unfortunately I so often see these controls being developed, in my mind, incorrectly. User controls should generally be dumb. They should know very little of the out side world (except of course of the namespaces they inherit from and maybe utility classes that perform common public static encapsulated method calls). Think text boxes, drop down lists etc. These don't know about your business objects! You passed in and retrieve properties from them. They may do some basic processing however this is generic. They should certainly never call or run business logic or know about services. So how do we use User Controls to run common logic? On the user control there should be publicly visible properties. This is usually a given. There can also be methods and events. Often I call a PopulateControl(Type type) (which is a public void Method) that takes in a control specific DTO (or collection of DTO's). Usually this merely passes each of the properties in the DTO to the properties in the Control. Right, so data is in the control, how do we do stuff with it? The page load may have some generic code, but avoid adding smarts that are too specific. How about passing data back to the parent? Either retrieve the data from a MyDto read only property the does the exact opposite of the populate control returning a DTO or create a custom event eg Save and pass a delegate into the control. Yes this mean code stays on the page. So?? If it commonly called create a class that does all the leg work for you that all page can call. This then follows the MVC pattern and separates the visual aspect from the Data aspects

Question: why do you use PopulateControl(Type type) and RetrieveDTO() and not properties? Answer: I am of the opinion that a property should never be able to throw an exception or call code that can throw an exception. If you are confident that this can not happen then, sure, use the property approach. However, if there is functionality that may cause an exception, then use the method approach. I wouldn't expect people to strictly follow this, this is just my preference.

Question: When to use Delegates? Answer: When it is obvious that this control does something, like its is a control that has customer details for adding or editing then a save delete and cancel buttons may be appropriate. Control of what is done on the User Control (usually the page) with the event (ie EditCustomer_OnSave()) means that page can pass in the correct logic or call to the manager of logic as a delegate. Obviously this means exposing something to apply the delegate to, such as an event!

I am more than happy for feed back on this. I want to do better code, you may be able to help!

Breadcrumbs Sitemaps and Querystring Part 2

Ended up not using Mr Chens example as we had reservations on how whell this would be used with the SiteMapResolve event. The current implementation is a complete hack requiring 2 (yes 2 providers) one to check the node and the other to build up a temp sitemap with query strings appended and a user control sitting in master pages working with the two. This means the code is very delicate and only take one person to change the default provider to break the whole thing. I think i have written more comments that code on this and cant say im too proud of the work, but mainly disappointed in MS hack around site map and thir providers… more perhaps to come once this goes though full test environment.

lessons have been learned