When developing a software application, choosing the right architectural pattern is crucial for maintainability, scalability, and testability. Let's explore the five popular architectural patterns: MVC, MVP, MVI, MVVM, and VIPER, with examples in C#. We will discuss what they are, how they differ, and how to determine the right one for your project.
1. MVC (Model-View-Controller)
What is MVC?
MVC is an architectural pattern that separates an application into three main components.
- Model: Manages data and business logic.
- View: Displays the data (UI).
- Controller: Handles the user input and updates the model and view accordingly.
// Model
public class UserModel
{
public string Username { get; set; }
public string Password { get; set; }
}
// View
public class UserView
{
public void DisplayUserDetails(UserModel user)
{
Console.WriteLine($"Username: {user.Username}");
}
}
// Controller
public class UserController
{
private readonly UserModel _user;
private readonly UserView _view;
public UserController(UserModel user, UserView view)
{
_user = user;
_view = view;
}
public void SetUserDetails(string username, string password)
{
_user.Username = username;
_user.Password = password;
}
public void UpdateView()
{
_view.DisplayUserDetails(_user);
}
}
// Usage
var user = new UserModel();
var view = new UserView();
var controller = new UserController(user, view);
controller.SetUserDetails("john_doe", "password123");
controller.UpdateView();
2. MVP (Model-View-Presenter)
What is MVP?
MVP is similar to MVC but with a distinct difference: the presenter handles all the logic to update the view. This makes the view passive, focusing solely on UI representation.
// Model
public class UserModel
{
public string Username { get; set; }
public string Password { get; set; }
}
// View Interface
public interface IUserView
{
void DisplayUserDetails(UserModel user);
}
// View
public class UserView : IUserView
{
public void DisplayUserDetails(UserModel user)
{
Console.WriteLine($"Username: {user.Username}");
}
}
// Presenter
public class UserPresenter
{
private readonly UserModel _user;
private readonly IUserView _view;
public UserPresenter(UserModel user, IUserView view)
{
_user = user;
_view = view;
}
public void SetUserDetails(string username, string password)
{
_user.Username = username;
_user.Password = password;
}
public void UpdateView()
{
_view.DisplayUserDetails(_user);
}
}
// Usage
var user = new UserModel();
var view = new UserView();
var presenter = new UserPresenter(user, view);
presenter.SetUserDetails("john_doe", "password123");
presenter.UpdateView();
3. MVI (Model-View-Intent)
What is MVI?
MVI is a reactive pattern where.
- Model: Represents the state.
- View: Renders the UI based on the state.
- Intent: Captures the user's intention (actions) and updates the model.
// Model
public class UserModel
{
public string Username { get; set; }
public string Password { get; set; }
}
// View Interface
public interface IUserView
{
void DisplayUserDetails(UserModel user);
void DisplayError(string message);
}
// Intent
public class UserIntent
{
public Action<string, string> OnUserDetailsSubmitted;
}
// View
public class UserView : IUserView
{
public void DisplayUserDetails(UserModel user)
{
Console.WriteLine($"Username: {user.Username}");
}
public void DisplayError(string message)
{
Console.WriteLine($"Error: {message}");
}
}
// Usage
var user = new UserModel();
var view = new UserView();
var intent = new UserIntent();
intent.OnUserDetailsSubmitted += (username, password) =>
{
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
{
view.DisplayError("Invalid input");
}
else
{
user.Username = username;
user.Password = password;
view.DisplayUserDetails(user);
}
};
intent.OnUserDetailsSubmitted("john_doe", "password123");

Join the conversation! Your thoughts help the community grow.