Wednesday 12 December 2012

Bananas and Monkeys


Ok first off, I did not write this, but damn it has some truth. For acknowledgement here is the link I found it on. It goes something like this...


Start with a cage containing five monkeys.
Inside the cage, hang a banana on a string and place a set of stairs under it. Before long, a monkey will go to the stairs and start to climb towards the banana. As soon as he touches the stairs, spray all of the other monkeys with cold water.
After a while, another monkey makes an attempt with the same result - all the other monkeys are sprayed with cold water. Pretty soon, when another monkey tries to climb the stairs, the other monkeys will try to prevent it.
Now, put away the cold water. Remove one monkey from the cage and replace it with a new one. The new monkey sees the banana and wants to climb the stairs. To his surprise and horror, all of the other monkeys attack him.
After another attempt and attack, he knows that if he tries to climb the stairs, he will be assaulted.
Next, remove another of the original five monkeys and replace it with a new one. The newcomer goes to the stairs and is attacked. The previous newcomer takes part in the punishment with enthusiasm! Likewise, replace a third original monkey with a new one, then a fourth, then the fifth. Every time the newest monkey takes to the stairs, he is attacked.
Most of the monkeys that are beating him have no idea why they were not permitted to climb the stairs or why they are participating in the beating of the newest monkey.
After replacing all the original monkeys, none of the remaining monkeys have ever been sprayed with cold water. Nevertheless, no monkey ever again approaches the stairs to try for the banana. Why not? Because as far as they know that's the way it's always been done round here.
And that, my friends, is how company policies are made.


Interesting huh?

Tuesday 4 December 2012

NHibernate, Ninject 3, MVC 4

Recently I have started to use Ninject more and more, its my favourite of all the IOC frameworks I have used so far. Also I wanted to give MVC 4 a dabble just to keep myself in the loop with what is going on in the MVC world after only using MVC 2 in the past. So I thought I would try to wire up something nice between MVC4, Ninject 3 and NHibernate and came up with the following located on github here.

So in a previous post with NHibernate, Unit of Work, Repository and Structuremap, I carped on about a Unit of Work and a Repository.


With regards to the repository...

I think I have gone off the idea of using the Repository pattern with NHibernate after reading lots of posts on Ayende's site, various other sites and from previous experience I feel it is limiting NHibernate to make optimal calls to the database. I believe NHibernate's Session is the repository. Here are a couple of links:
Architecting in the pit of doom: The evils of the repository abstraction layer
Repository is the new singleton pattern


With regards to the Unit of Work...

Here I just set up a filter to wrap up the work done across a transaction. So now any of the work in the domain model/business logic that is done via a particular controller action should all be wrapped up in a transaction. I wired up the filter using the Ninject.MVC3 extension and the Kernel.BindFilter.


Github project...

Its been a while since I looked at this project and what I was doing, so I will need to refresh myself with it as well. I just created the default MVC 4 app and added a new controller, the ProductController. I just wanted to be able to show a list of Products that are stored in the database and also be able to add new products and render the change.

Libraries I used:
MVC 4
Ninject 3
Ninject.MVC 3 extension
NHibernate
Automapper

The project isn't finished yet, I would like to try to wire up NServiceBus and RavenDB in here too (but these are just distance thoughts for the moment), and really push all the business logic down into the domain model.

Some points on what I did.
  • Created the standard ASP.NET MVC 4 application. 
  • Added NHibernate and created a simple database relationship for stores, products and employees, I may have taken these relationships from somewhere else but I can't remember.
  • Added Ninject and its Ninject.MVC 3 extension, this works for MVC 4.
That's it for now... I will come back and update this some more when I get a chance...

Monday 26 November 2012

Programming is not just programming

Well its been a very long time since I posted something here, but today I had an idea of what I could write about...

I had a very good friend ask me about .net development, and that he was concerned about getting his .net skills up to scratch. My immediate thought was this guy is a super bright guy and I am sure he knows enough of .net to be able to do pretty much anything that was required of him and quickly as well. Which then led me onto thinking of this post and what I could write about.

I would say that a lot of .net developers out there know how to write code in .net and probably/possibly many other languages too, but possibly miss the idea of writing code that can be maintained and also feel confident about being maintained quickly (this does not mean that the super bright guy above does not know this :-)).

I would say of those that don't care and don't even realise what they are writing, their code is becoming legacy code as quickly as they type it. I for one was one of these guys (the didn't realise one), absolutely 100% omg! Am I admitting that! We all have to start somewhere (that's my excuse ;-) ) and from there its a lot of practice and realisation of what we are writing.

Now I am making a concentrated effort on everything I write to be much clearer, simpler, easier to understand and change, by keeping in mind as many of the things I have read about in the following books, links, blog posts frameworks etc.

I will try to keep this up to date every time I discover something new or useful. Lots of links and lots to digest I know, but it has taken me some time. Just take your time, practice a few things, try them out on some scratch projects see what you can come up with. You'll surprise yourself on how your code becomes much more awesome!

  • S.O.L.I.D - A must for every OO developer, probably any other link on that page too.
  • Tell Dont Ask - Another nice one that can lead you onto the other nice things like CQRS and Law of Demeter
  • Dependency Injection Stack Overflow link and James Shore Link from the same SO link - Lots of Inversion of Control (IOC) containers out there to help with this too. Ninject (my favourite), StructureMap, Unity, there are also more. I find this is really important and should be used on every project regardless of size of the project, what you will start to realise when you use this that your unit testing becomes easier to write.
  • Unit Testing - There are many unit test frameworks out there to pick from, NUnit (I have used a lot), XUnit (but heard a lot of good things about this and I think I will start to use this for some home projects ), MSTest (Microsoft's, included with Visual Studio). Just a quick side note I would like to write an article about is Unit Testing. I believe and found it so difficult to start writing unit tests, this is also a lot of practice and when you realise how it fits and what it does, BOOM! It will be a Eureka moment! 
  • Resharper, get it! It's worth it! It has some very nice tools with it, the solution wide analysis(dont need to waste time building solution to find errors), the stack trace viewer are a couple. I recommend learning a shortcut a day, and within no time you wont need your mouse to code. Pffff, the amount of times I have sat there with someone using the mouse to click on menus, scrolling around, switching windows, looking for code in a solution, blah blah, all that time wasted.... aaarrrrgggghhhhh! Drives me nuts! Mind you I can get some 5 second power naps, when they are searching for the next thing to click! lol ;-)
  • Read blogs articles, follow some recognised people, like some of the people from the books I have mentioned. Also take a look at some code you have found on github, contribute maybe too, you will learn so much like this.
  • Go to some talks, organised day events, local newsgroups in your area, etc. skillsmatter, DDD
Ok some other stuff, buzz words, people, software etc... I will try to write these more in depth at some point but it will be worth you guys having a sniff at what they are about also.


Continuous Integration

Setting up a build and release process just off a single check in! If you can get this right, this is one of the most powerful things in releasing software fast and reducing all that wasted time, matching up bugs with changesets, with releases, deploying the correct version for your testers, unit testing, integration testing etc. A couple more pieces of software provided from JetBrains, TeamCity and  YouTrack is worth taking a look at also, extremely useful for tracking all task, bugs etc. There are many other solutions out there too, but these are the first that came to mind for the moment.

Source Control

There are several source control systems out there, here are a few...
  • GIT - Extremely powerful and gives much more ease and freedom for your code. I have only started to use this recently, it takes a little time to get your head around if you are used to using a more centralised system, here are some links... GIT Succinctly, GitHub get an account, try it out, practice, stick at it. Well worth it!
  • TFS - Microsoft's version of source control, along with managing tasks and probably other things too. I use it most of my working time. But the more I use GIT the less I want to use this.
  • SVN - Not used in a long time and used sporadically.
  • SourceSafe - This has to get a mention! DO NOT USE IT!! REALLY! DON'T! Go take a read of any articles about it, see what people have to say about it, maybe you have had your own experiences with it...


Database Stuff

Apart from the usual same old boring databases are databases blurb, blah blah, same old, same old, SQL Server, MySQL, PostgreSQL, blah blah etc ooops forgot about Oracle.
But there are these new document databases turning up, which seem very very interesting, take a look, try them out, RavenDB and MongoDB. I haven't used them yet, but will be giving them a go soon I hope. They seem extremely powerful and fast. I went to a see Oren Eini and he gave an excellent talk on RavenDB. Interesting thoughts of why now we should be looking at storing and retrieving data differently.


Books

  • Working Effectively With Legacy Code - Michael Feathers, Don't be depressed with crap code, take a read of this and see how you can fix it! Very good read and interesting.
  • Clean Code, and The Clean Coder - Robert C. Martin (Uncle Bob), Both these books very nice.
  • Refactoring: Improving the Design of Existing Code. Excellent! Nice techniques, mixing this with Michael Feathers book above and the Clean Coding books of Uncle Bob you will have some quite powerful ways of being able to modify the most complex of code.
  • The Pragmatic Programmer: From Journeyman to Master - Andrew Hunt and David Thomas,  Superb, must read for all developers! Interesting too, see if you can recognise any of your situations you have been in match whats in this book, I wouldn't be surprised most of them match :-)
  • The Art of Unit Testing - Roy Osherove, Very nice, a definite good start with Unit Testing, but I still think you need to practice and see what it really is worth.
  •  C# In Depth - Jon Skeet, Superb! I have to take my time in this one, and probably have to read it a few times, but excellent!
  • Domain Drive Design: Tackling Complexity in the Heart of Software - Eric Evans, I have started reading it but not got very far yet, but heard it is very good.

Other Things

  • Rx - A must! Take a look! Try it out! See what you can do and learn from this. Very powerful!
  • Agile - I would say a very powerful, fast and useful process of writing software. I would never go back to Waterfall again if I could help it, unless I needed money for sleeping. I haven't read up much on the agile processes but been in the flow of using this style of development with several projects. 
  • NServiceBus - I haven't used yet but all I hear is good things about this piece of software check it out. I will be trying this out in the near future also.
  • Automapper - For transfering data from one object to another which has the same properties, Very neat, very useful, easy to use, set up.
  • Some people, Martin Fowler, Kent Beck, Robert C. Martin (Uncle Bob), Oren Eini, Udi Dahan, Jon Skeet, all of these have a massive community around them and well worth reading up some of their articles.