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
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.
📄️ Project Setup
To assist you in implementing the lab project, you can use the .NET project skeleton available on GitLab. However, to start, we will create a backend project in the preferred IDE, selecting a template for .NET Core Web API, as shown below.
📄️ Database Interaction
Besides the application logic, there must be data persistence on which the actual logic is performed. In this regard, most applications use databases. To simplify the interaction between programs and the database, ORMs (Object-Relational Mapping) have been implemented. These are frameworks that map database tables and data types to objects, called entities, and types declared in the application code.
📄️ Dependency Injection
One of the main concepts underlying modern application development is Dependency Injection (DI). For a detailed description and information on its implementation in C#, see here.
📄️ 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.
📄️ Authentication and Authorization
In this section, we will discuss authentication and authorization. For user authentication, we will use a simple authentication scheme with email and password, which will return a token using the JWT (JSON Web Token) standard. The JWT is a JSON signed in Base64 format. Generate the JWT from the laboratory project swagger on the login route with user "admin@default.com" and password "default" to see how it looks.
📄️ C# Particularities
Here we will discuss some particularities of programming in C# compared to other web frameworks; more extensive information can be found in this course. One important feature in C# is extension methods. These are compiler tricks that add new methods to existing classes and can be called as if they were non-static methods of these extended classes. Extension methods are useful for extending functionality for existing code without modifying it.