With the release of Visual Studio 2008, Microsoft is working on including an add-on for C# ASP .NET web application developers to include the Model view Controller (MVC) design pattern within their software architecture. While one of the add-on’s core purposes is to enhance URL functionality in a web application by enabling a model view controller pattern and design structure, the MVC design pattern itself, can be a powerful tool in your C# ASP .NET web applications.
The Model View Controller (MVC) design pattern lets us pair loosley-coupled Models and Views together. In this manner, we can keep view logic completely separate from model logic. We can then interchange views or models with different classes, effectively changing an ASP .NET web application’s functionality with minimal side-effects.
Since this description may be confusing to developers who are new to the model view controller design pattern, let’s discuss in more detail what the individual parts of the MVC pattern entail.
The Model in the MVC design pattern represents the core functionality of a class, such as the data processing. The Model has all functions required to process a piece of data, convert data, work with data, move data, etc. The Model performs processing on data behind the scenes and is generally a back-end logical process.
The View in the MVC design pattern represents the display functionality of the web application. The View controls how to display data, drawing controls, writing text, getting input from the user, outputting results, etc. Since a C# ASP .NET web application usually involves showing something to the user, retrieving input, processing the input, and displaying the results, you can see how the View directly interacts with the Model to perform the applications functionality.
The Controller can be considered the middle-man between the Model and View. Since the MVC design pattern lets us completely separate the Model from the View, an intermediary is needed in order for the two to communicate with each other. This is the job of the Controller. The Controller actually holds an interface of the Model and View, so that it can call the required functions of each.
The Controller takes a View and a Model in its contructor in order to setup the linking between the Model and View. The Controller’s flexibility is particularly important in that it allows us to pass it any combination of Model and View in the contructor. This actually allows us to link any type of Model with any type of View. If you think about this, this means we could create any variant of user interfaces and still use the same data processing logic (the Model). Or we could create any number of different ways to process a piece of data and still use the same user interface (the View).
Now that you have an idea of what each part on the model view controller design pattern entails, we can begin setting up the framework code to use this pattern.
This design pattern actually begins with 2 interfaces to represent the Model and View. However, since the View needs to be able to talk to the Model and the Model needs to be able to talk to the View, we need 2 more adapter interfaces to allow them to communicate. In most scenerios, the View will communicate with the Model (and not the reverse), although we still allow the functionality for the other direction as well.
public interface IModel
In the above code, we start with an interface to define the Model and the View class. The view simply has one main function V(), which takes no parameters. We assume it displays a user interface for the user, possibly validates inputs entered by the user, and may even throw exceptions on invalid input. The Model has a main function M(), which takes an object as a parameter and returns an object. This allows us flexibility in the type of data we can pass to our model.
Next, we require two delegates to handle the connections for the Model and View factory methods. We simply define the two delegates as follows:
public delegate IModel ModelFactory();
Now that we’ve defined the interfaces, we can start on our concrete implementations of the MVC design pattern, starting with the Model. In this example, we’ll create a Model that simply encloses a string with dollar signs.
public class Model : IModel
Notice in the above code we assign the adapter, which gives the Model a means to access the View, if neccessary. Our main logic is located in the M() function.
We can now create an implementation of the View (our user interface). In this example, our user interface will simply be the console screen. Our view will ask the user for their name, retrieve the input from the user, send the input to the Model for processing, and display the result.
public class View : IView
Notice in the above code we setup the adapater, which gives the View access to the Model. In the main function V(), the view handles all user interface logic and then calls the Model to process data. With the Model and View defined, we only need one last part to link the magic together.
The Controller is the intermediary in the MVC design pattern and is required for setting up the actual linking between the concrete Model and View. The Controller keeps our linking loosley-coupled, so that we can easily replace any View with any other Model and vice-versa. Since the Controller handles both the Model and View, it will implement the interfaces of the adapters of both classes.
public class Controller : IModelAdapter, IViewAdapter
The constructor of the Controller class takes the two delegates we defined earlier, which represent a conrete Model and View. The Controller then sets up the adapter (which happens to be itself, since the Controller is the intermediary). When the View needs to call the Model via the adapter, it ends up calling the Controller, which in turn fires off the Model’s appropriate method. If you consider the details behind the logic, the Controller is quite a powerful class.
We can now utilize the model view controller design pattern in an example C# .NET application. We’ve defined a Model which encloses a name with dollar signs and we’ve defined a View which asks the user for their name via the console screen. We can now use the Controller to link them together. Note, we’ll need two factory methods to create an instance of the Model and View classes.
static void Main(string args)
Output from running the example:
What's your name? John Doe
The basic example above shows that the model view controller pattern actually works. However, if all we needed was to enclose a name in dollar signs, we could perform this function in far less code. The real power of the MVC design pattern comes out when we need to create several different types of user interfaces or several different types of data processing. This is where code could become more complicated, but the MVC design pattern keeps it simple. To show this, let’s define another View. We’ll still use the console screen for our user interface, but suppose we wanted to change the language to Spanish? Instead of re-writing our View and replacing the English with Spanish, we can create a new concrete View as follows:
public class SpanishView : IView
To use the new View, we simply link it in the model view controller in our main program as follows:
static void Main(string args)
Output from running the example:
¿Cuál es su nombre? John Doe
Notice in our main program that all we had to do was replace MyViewFactory with MySpanishViewFactory. The Controller took care of linking together the new Spanish View with the same Model as before. We can do the same thing with a different Model. Likewise, you can link any combination of Model and View together.
Let’s create another Model which doubles a name.
public class ModelDoubler : IModel
Now we can link together many combinations of the Views and Models as follows:
static void Main(string args)
You can see how the power behind the model view controller design pattern in a C# ASP .NET web application can provide us with a lot of functionality for loosley-coupled connections. Imagine changing the View user interface from console text to graphics or the web browser. Imagine changing the Model data processing from strings to XML or a database.
The Model View Controller design pattern in C# ASP .NET web applications allows us to link together user interface Views and data processing Models and a loosley-coupled fashion. We can achieve greater code re-use and separation of logical boundaries by implementing individual classes in this fashion.
While ASP .NET provides its own type of a View in the form of ASPX pages and its own type of a Model in the form .CS files, understanding the core functionality of the MVC design pattern can provide advantages in your software architecture and web application results.
This article was written by Kory Becker, software developer and architect, skilled in a range of technologies, including web application development, machine learning, artificial intelligence, and data science.Sponsor Me