Highway.Data

The fastest and smoothest way to great architecture

View project onGitHub

Highway.Data.EntityFramework – Getting Started with IoC

So you have now gotten comfortable with Highway.Data.EntityFramework and decided that you want another sharp poke in the eye with a stick of Awesome!? Great! We have just the awesome stick, IoC!

If you are not familiar with the concept… allow me to explain in true internet fashion.

http://en.wikipedia.org/wiki/Inversion_of_control

http://msdn.microsoft.com/en-us/library/aa973811.aspx – The funny one to me is the MSDN article uses Castle.Windsor not Microsoft’s Unity…… irony.

 

So lets get going on IoC. To make this example slightly harder to follow and more realistic we are going to jump into our first application, but with some architectural refactoring done.

It looks something like this…. well exactly like this.

image

 

The first thing I did was pull the domain objects into their own project, then I moved all data access specific code into it’s own project. This took all of 3 minutes with Resharper … have I mentioned how awesome Resharper is? No, well it is one step shy of my wife’s home made Cherry pie!!! NCrunch is better than her home made Cherry pie!! But I digress…

We want to start in the MVC project down at the bottom there. We are going to wire IoC into MVC so that we get the full effect of this. Then we will leverage our new found powers to make Home –> Index even better!!

First we need to bring in our IoC of choice. Highway.Data supports any IoC that adheres to Common Service Locator. We have also gone through and written several adapters to make your life easier.

So pick… ( if you pick something other than Castle.Windsor your mapping syntax will be different than the examples – You have been warned ).

 

image

 

I am picking Castle, and I get this output from Package Manager.

 

image

 

This get my IoC and the Highway.Data.EntityFramework bootstrap in place. Now all we have to do is wire this into the global.asax.

 

image

Notice the highlighted part? It is highlighted because I HATE when there is a ton of stuff plugged right into the App_Start method. Please, extract it to a method. I BEG YOU!

Now back to our regularly scheduled tutorial….

In the WireUpIoC method we will……wire up the IoC. Like so:

image

Notice here we wire up Highway.Data’s Repository, Context, some default logging, and context configuration. We do however load up the mappings specific to our implementation. *That is important*

We then just need a controller factory for MVC, and luckily I have one.

image

And just like that we are wired up. We just have to use it now, like so:

image

We simply inject the repository via constructor and then use it. The IoC supplies a new version each time, and we are off to the races!!!

“Hop on the Highway and see where it takes you”

Go Back to Getting Started with Highway.Data.EntityFramework

Continue with Highway.Data and Advanced Query Scanarios