Share icon illustration abstract graphics

When your web application start to grow, in terms of functionality, sooner or later you will need to make a decision about splitting it into smaller applications.

Sometimes, the operation is really simple. But sometime, like we had, it’s really very difficult to make the decision of where to make the cut.

Dividing the controllers and models is easy, as these are normal classes that you can refactor into separate library and reference them from all the web applications.

But what about the views?

These are not “normal” classes, and there is no intuitive way to reference them. The only way that we found is to compile them into dlls using RazorGenerator extension. We simply put our common views in a separate library, complied them, and referenced them from the web applications. But slowly we started to discover few disadvantages:

  1. Every time we made changes in the common views, we needed to build them and the web applications. It was a long process comparing to normal views, which don’t need compile at all (They are complied separately on demand)
  2. Some visual studio extensions (such as resharper) couldn’t handle these views well, and started to mark errors everywhere.
  3. We didn’t like the requirement to add additional view engine. Especially as we had another one of our own.

So we needed find another solution. And we found a very simple one. Implementing it requires the following steps:

  1. Create a new web application that will host all the common controllers, models and views.
  2. Compile this common application, and reference it from your web applications. Now you have the common controllers and models in the applications.
  3. From your development IIS, go to your web application site, and under the views folder, create a virtual directory (let’s say that you name it “common”) that points to the views folder of the common application.
  4. Change all the calls to the common views to use the full path (i.e instead of Html.Partial(“SomeCommonView”), change it to Html.Partial(“~/Views/Common/SomeCommonView.cshtml”). Also you will need to change the views results of the common actions to point to the full path.

Your done. Repeat the process to all of your applications and you have a common views. Any changes in a common view do not require any compilation and the effects are immediately reflected in all applications.

What about the production environment?

Creating a virtual directory in production environment might be more difficult to accomplish. But luckily, there is a built in solution inside the visual studio, by customizing the publish process. With a little of work, you can customize the publish process and copy the common views folder to the web application views folder.

We are using this solution widely now, and it’s very convenient and easy. We even expanded its usage, and now we are also sharing static resource (scripts, styles and images) in the same way.

Share →