Developing an application in both Web Forms and MVC
I recently had an opportunity to co-write an new application for Sitecore's Marketplace. With this app we decided that we wanted to support both Web Forms and MVC environments. Sitecore started to support MVC a couple years back and in result we have seen a split among clients on which technology to use. In short each has its strengths and weaknesses; neither are going anywhere. So to support all our client base with our new application we wanted to provide a solution that could handle both frameworks. There really isn't anything magic about the concept. We used two separate project files and a common class project to support them. Then as a developer you just use one or the other. But overall there was some interesting surprises and a few "if only I had thought of that before" moments. Here are some tips:
- Write as much as you can in your helper class. In short, if you write in the helper class project and both MVC and WF project call it, then you only have to write it once.
- WF advanced controls like <asp:DataPager are not necessarily your friend. This one refers back to the first. These more advanced controls can be a time saver, but when you have to look at how your going to do it in MVC and have to go down a completely different path, then things get ugly. It can be a lot more practical to just build a pagination inside your helper class project that both sides can reference, even if its as simple as just a string output.
- Develop small user controls and MVC views. If you are a Sitecore developer you should be doing this already. There are all sorts of advantages to breaking renderings down into small movable controls. But in the case with an application that supports MVC and WF, if your user controls and views are kept small you will have a much easier time keeping the two in sync when its time for bug fixes and upgrades.
- Consider developing under MVC first. I have found that when it comes down to developing between a WF user control and a MVC view, the views are just much more straight forward. The views are the least complex and gives you that base where you would be pulling from the helper class over using more advanced means. Also the hidden advantage of views not requiring a recompile can greatly speed up your troubleshooting time.
- Accept that no translation between MVC and WF will be perfect. At most the similarity between user control and view might be 90%. Each frame work in some cases call things differently. Specifically if you are working with Sitecore. Getting the data source of an item takes two separate approaches. Furthermore don't forget about the most obvious fact that a user control works with code behind. Where a for loop exists on a MVC view, you might have a for loop on the back end being data sourced on the front end to an <asp:ListView. Overall just accept the fact that things will be done differently and in result you might even slightly different results.
While developing this latest app I certainly hit a few pitfalls where rewrites were required. Going forward however I hope to take this experience and build a better plan. I hope this list helps!