Over the past few months, I have been deep down the rabbit hole with .NET MVC web application projects. A pattern of development emerged out of these months of work, which I want to share with you today. I am not sure what to call it, but MVC Squared seems to fit. In reality, it is more of a .NET model-view-controller server side design with a view-controller, view-model client side design. One night I sat down to create a pseudo flow chart of my idea, so others working with me could critique it.
Lets take, for example, a products page. We may have hundreds or thousands of products in a database which we want to give to the user in an elegant and non-postback implementation. This is where the new pattern shows its strengths!
Now that we need to talk to our server with AJAX, our server code needs to implement the actions for the AJAX get and post requests. Since I am mainly a .NET developer, this is where some of you may exit. If so, then you probably already know how to implement those functions in your respective language. Just remember that to make your life easier make sure your server models match your client side view models. This will make data more consistent and easier to work with when going from server to client side.
For .NET Developers – My Implementation
For those that are .NET developers, here is a simple description of my implementation. In .NET MVC we already have our simple MVC setup. I started to create my post and get actions in my normal controller but then started to get confused with the large code files I was creating. My controllers were holding all the normal functional actions plus my AJAX actions. I decided to implement a new controller, with the same name but appending an “AJAX” after my controller name. Then some simple inheritance from my standard controller gave me a separation of logic and files to work with.
I feel this is just touching on the amazing things that can happen with a predefined structure of this nature. Now I find myself working on traditional sites and saying to myself, “If I had view model binding here and AJAX, this could be done a lot better and with nicer user interactions”. You may have noticed that I did not start to touch on unit testing, but if you are the unit testing type, then you can already see how this structure lends to make unit testing easier and not dependent on the DOM. I also attempted to keep this article a little more about theory and architecture instead of examples. For those that are more of the hands on learners, I will post back soon with some code examples.
Hopefully I have either taught you something to help your day, or intrigued you to criticize my idea. Either way, I would love to hear your opinions and comments!