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...