Skip to main content

Prerequisites

In this lab, we will focus on developing a web server using .NET and the C# programming language. Even if you have no experience in C#, it is similar to Java and can be easily learned with basic knowledge of object-oriented programming. We recommend you go through this course for a brief introduction to programming in C#.

.NET is a framework initially developed by Microsoft for the Windows operating system, but it is now compatible with multiple operating systems. It is one of the most used frameworks for developing enterprise web applications, with another popular technology in this field being Java Spring. The benefits of using .NET include its cross-platform nature, ease of learning, extensive community support, and detailed documentation. The primary programming language for development within the .NET framework is C#, a multi-paradigm language that supports object-oriented, functional, and reflective programming.

There are certain particularities in writing code in C#. If you encounter difficulties in understanding the lab structure, besides our course, you can consult the documentation provided by Microsoft and their electronic resources for web application development available here. Although this book discusses the implementation of microservices architectures, which are quite advanced concepts, the main design patterns and best practices described there also apply to developing monolithic backend applications.

Tools

Before starting the actual development, you need to prepare the following:

IDE and packages for .NET 8

  • For Windows, we suggest using Visual Studio 2022 Community, available here. Alternatively, you can access the Professional or Enterprise version through MyUPB.
  • For Linux/MacOSX or as an alternative, you can use the DotUltimate platform with Rider as the IDE. You can request an academic license for the entire suite from JetBrains using your institutional email address. You might need to install the .NET 8 SDK from here.
  • Bonus: For a better development experience with the IDE, you can use Visual Studio with ReSharper from JetBrains, but ensure you have at least 16GB of RAM.

PostgreSQL Database

  • To use the PostgreSQL database, one option is to install PostgreSQL on your local machine, although it is not recommended.
  • A better alternative is to install Docker and Docker Compose. Run the docker-compose command with the YAML file found in the lab's GitLab repository:

Database Client

  • For PostgreSQL, you can use PGAdmin.
  • If you prefer a universal client, you can opt for DBeaver.
docker-compose -f .\docker-compose.yml -p mobylab-app-db up -d

📄️ Basic Concepts in Web Application Development

In this lab, we will split the web application into two distinct components: frontend and backend. The frontend represents the visible part of the application for the user and runs in their browser. With the frontend as an independent entity, it will be developed as a SPA (Single-Page Application). Unlike an MPA (Multi-Page Application), an SPA consists of a single page that dynamically changes its content based on user actions and data provided by the backend.

📄️ Handling HTTP Requests

First, we need to clarify how the backend application in .NET works. When the WebApplication type application starts, it opens a port and waits for HTTP requests. The request is parsed and transformed into an HTTP context. This context is sent to an execution pipeline that will call the appropriate routines for that request and return the response back through the pipeline. Each step executed in the pipeline is called middleware, and some of these can be defined by developers. The last middleware executed in request handling calls developer-defined classes of type controller. In the controller, you specify which endpoints/routes from the server's API correspond to which methods in that class. A controller class is a special class whose public methods are called when the routes corresponding to the method are accessed. These classes inherit from ControllerBase. For the framework to identify controllers and routes, the class and methods are decorated with attributes, classes that extend the Attribute class. For example, [ApiController] decorates a controller class, specifying to the framework that this class should be used as a controller; [Route("api/[controller]")] on the class and [HttpGet("my-route")] on the method in the controller specify that when the route "/api/<controller_class_name>/my-route" is accessed with an HTTP GET request, that method is called.