Wednesday, June 23, 2010

Refactoring ASP MVC 2 to use Dependecy Injection (part1)

In my previous post named ASP.NET MVC 2.0 - Master Detail Views I wanted to show how to retreive data from several tables move this into objects and then present this in a view in a master-detail format.

My comment specifically with regards to the controller was "Since this controller was created to illustrate as simple as possible how to build a master-detail view in ASP.NET MVC,
I have not focused on proper architecture using Depedency Injection, Repository Patterns etc... (read more here and here)."

I would now like to go through the steps needed to refactor the solution to take advantage of Dependency Injection. The scenario we are facing is that my fictual boss has requested that we move the implementation so it is running on mono on top of a CentOS/Redhat linux distribution on Amazon Elastic Compute Cloud. We will have to switch the database to MySQL.

Now since I have chosen to NOT use NHibernate or another similar database agnostic ORM (which I could have) I will instead do this manually. The ultimate goal is that we can swap the database from Microsoft SQL Server to MySQL server but we want to allow the client (my fickle fictive boss) to go back to Microsoft SQL Server as his temper changes.

As a matter of fact I would also like to allow implementations of another view front end in Silverlight 4.0 or WPF.

I will in this post not actually implement the MySQL DAL or the Silverlight GUI but make sure that my applications allows it.

If we have a look at line 04 - line 37 we have intimate knowledge of the database in this code. Basically since we are directly using the generated LINQ classes we might as well hardcode SQL Server in the controller.

Now lets first of all try to move the LINQ classes to a new assembly.

So let us move the LINQ classes to a new project and change the name to reflect that this generated set of classes is for MSSQL.