Enterprise Applications with C# and .NET: Develop robust, secure, and scalable applications using .NET and C# (English Edition)
()
About this ebook
This book will help you build enterprise-grade applications with C# and .NET. It covers a wide range of topics, including the basics of C# programming, advanced concepts such as object-oriented programming, and the use of the .NET platform for building robust and scalable applications. You will also explore the best practices and design patterns for building enterprise applications.
By the end of the book, you will be able to create a full enterprise application by applying all the given concepts across the entire book, including TDD, database access, security, authentication, modern architecture, and the new features of C#.
Read more from Alexandre F. Malavasi Cardoso
Implementing Design Patterns in C# 11 and .NET 7 - 2nd Edition: Learn how to design and develop robust and scalable applications using design patterns (English Edition) Rating: 0 out of 5 stars0 ratings
Related to Enterprise Applications with C# and .NET
Related ebooks
ASP.NET Core for Jobseekers: Build Career in Designing Cross-Platform Web Applications Using Razor and Entity Framework Core Rating: 0 out of 5 stars0 ratings50 C# Concepts Every Developer Should Know Rating: 0 out of 5 stars0 ratingsC# 7 and .NET Core: Modern Cross-Platform Development - Second Edition Rating: 5 out of 5 stars5/5Mastering C# 8.0: Master C# Skills with Hands-on Code Examples (English Edition) Rating: 0 out of 5 stars0 ratingsAngular Interview Questions and Answers: Including Angular 6,5,4 and 2 Rating: 0 out of 5 stars0 ratingsHands-On Network Programming with C# and .NET Core: Build robust network applications with C# and .NET Core Rating: 0 out of 5 stars0 ratingsFront-end Development with ASP.NET Core, Angular, and Bootstrap Rating: 0 out of 5 stars0 ratingsVisual Studio 2022 In-Depth: Explore the Fantastic Features of Visual Studio 2022 - 2nd Edition Rating: 0 out of 5 stars0 ratingsImplementing C# 11 and .NET 7.0: Learn how to build cross-platform apps with .NET Core (English Edition) Rating: 0 out of 5 stars0 ratingsPractical C++ Backend Programming: Crafting Databases, APIs, and Web Servers for High-Performance Backend Rating: 0 out of 5 stars0 ratingsProfessional Java EE Design Patterns Rating: 0 out of 5 stars0 ratingsTesting with JUnit Rating: 0 out of 5 stars0 ratingsHow To Build Microservices: Top 10 Hacks To Modeling, Integrating & Deploying Microservices Rating: 0 out of 5 stars0 ratingsNode.js High Performance Rating: 0 out of 5 stars0 ratingsNode.js Design Patterns - Second Edition Rating: 4 out of 5 stars4/5Building Microservices with .NET Core Rating: 1 out of 5 stars1/5Microservices Architecture Handbook: Non-Programmer's Guide for Building Microservices Rating: 4 out of 5 stars4/5Parallel Programming with C# and .NET Core: Developing Multithreaded Applications Using C# and .NET Core 3.1 from Scratch Rating: 0 out of 5 stars0 ratingsC# 7 and .NET Core Cookbook Rating: 0 out of 5 stars0 ratingsLearn Microservices - ASP.NET Core and Docker Rating: 0 out of 5 stars0 ratingsAzure for .NET Core Developers: Implementing Microsoft Azure Solutions Using .NET Core Framework Rating: 0 out of 5 stars0 ratings.NET 7 Design Patterns In-Depth: Enhance code efficiency and maintainability with .NET Design Patterns (English Edition) Rating: 0 out of 5 stars0 ratings
Computers For You
Creating Online Courses with ChatGPT | A Step-by-Step Guide with Prompt Templates Rating: 4 out of 5 stars4/5Mastering ChatGPT: 21 Prompts Templates for Effortless Writing Rating: 4 out of 5 stars4/5The ChatGPT Millionaire Handbook: Make Money Online With the Power of AI Technology Rating: 4 out of 5 stars4/5Procreate for Beginners: Introduction to Procreate for Drawing and Illustrating on the iPad Rating: 5 out of 5 stars5/5Elon Musk Rating: 4 out of 5 stars4/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Data Analytics for Beginners: Introduction to Data Analytics Rating: 4 out of 5 stars4/5Technical Writing For Dummies Rating: 0 out of 5 stars0 ratingsUX/UI Design Playbook Rating: 4 out of 5 stars4/5The Self-Taught Computer Scientist: The Beginner's Guide to Data Structures & Algorithms Rating: 0 out of 5 stars0 ratingsStorytelling with Data: Let's Practice! Rating: 4 out of 5 stars4/5CompTIA Security+ Get Certified Get Ahead: SY0-701 Study Guide Rating: 5 out of 5 stars5/5The Innovators: How a Group of Hackers, Geniuses, and Geeks Created the Digital Revolution Rating: 4 out of 5 stars4/5Deep Search: How to Explore the Internet More Effectively Rating: 5 out of 5 stars5/5Microsoft Azure For Dummies Rating: 0 out of 5 stars0 ratingsBecoming a Data Head: How to Think, Speak, and Understand Data Science, Statistics, and Machine Learning Rating: 5 out of 5 stars5/5Learning the Chess Openings Rating: 5 out of 5 stars5/5Computer Science I Essentials Rating: 5 out of 5 stars5/5Fundamentals of Programming: Using Python Rating: 5 out of 5 stars5/5The Musician's Ai Handbook: Enhance And Promote Your Music With Artificial Intelligence Rating: 5 out of 5 stars5/5A Quickstart Guide To Becoming A ChatGPT Millionaire: The ChatGPT Book For Beginners (Lazy Money Series®) Rating: 4 out of 5 stars4/5Get Into UX: A foolproof guide to getting your first user experience job Rating: 4 out of 5 stars4/5CompTIA IT Fundamentals (ITF+) Study Guide: Exam FC0-U61 Rating: 0 out of 5 stars0 ratings
Reviews for Enterprise Applications with C# and .NET
0 ratings0 reviews
Book preview
Enterprise Applications with C# and .NET - Alexandre F. Malavasi Cardoso
CHAPTER 1
Introduction to .NET
Introduction
With this chapter, we are starting our journey into the cross-platform and modern .NET. The .NET platform has changed since 2016 to provide powerful libraries that allow us to build applications for any operating system and even multiple devices, taking the benefits of the best software development practices.
In this chapter, you will learn how to set up your local environment to build .NET applications using Visual Studio or Visual Studio Code, and you will also have the opportunity to get familiar with basic concepts of multi-platform development for Linux, macOS, and other operating systems.
Structure
In this chapter, we will discuss the following topics:
Tools and environment setup
Installing Visual Studio
Installing Visual Studio Code
Introduction to Visual Studio
Introduction to Visual Studio Code
Understanding Multi-Platform concepts
Overview of project types on the .NET platform
Objectives
After studying this unit, you should be able to install and set up the Visual Studio IDE and Visual Studio Code, understand multi-platform concepts and discuss the available .NET project types.
Tools and environment setup
To get started with software development in .NET and C#, you must install the most recent version of Visual Studio, a complete Integrated Development Environment (IDE) for creating, compiling, and building your .NET projects. Visual Studio is available for Windows and macOS, both of which are available in the Community Edition for studying. The tool can be downloaded from the official Visual Studio Website.
Furthermore, Microsoft has provided Visual Studio Code, an alternative light version of the editor for .NET and C# applications, which is available not only for Windows and macOS but also for various Linux distributions. Considering this editor is an open-source extensible project, the technical community, IT professionals, and companies around the globe have created tons of free extensions for different languages apart from C# itself. Therefore, it is a suitable tool for cross-platform applications without compatibility concerns. The Visual Studio code can be downloaded from the official Website for free.
Installing Visual Studio
After downloading Visual Studio on the official Website, you must take the following steps for the installation:
Double click on the downloaded executable file. Ensure your user on the operating system has permission to install the software.
Choose the desired workloads to be installed and set up together with the Visual Studio. For the examples of this book, the following workloads must be installed:
Asp.Net and Web development
Azure development
.NET Desktop development
Universal Windows Platform development
.NET cross-platform development
Mobile development with .NET
The necessary workloads for the development of all the code samples along with this book are presented in Figure 1.1:
Figure 1.1: Visual Studio workloads
After choosing the necessary workloads, click on the install option.
Usually, Visual Studio is configured to use the same language as the operating system language. If you would like to set up a different one, you can do that in the Language Packs option, where other ones will be available. For the examples of this book, the Visual Studio was configured to the English language.
After finishing the installation, you can already create .NET projects.
Note: Once Visual Studio is installed, you don’t need to install the latest stable version of .NET SDK, as it is already part of the Visual Studio installation. By default, the Visual Studio updates follow the newest features introduced into .NET, such as library updates, minor and significant changes, and new project templates. Even though the library updates do not automatically apply to your existing projects, each project targets a specific .NET version, and the Visual Studio updates modify just the IDE and not the project configurations.
Installing Visual Studio Code
Visual Studio Code is a cross-platform alternative to Visual Studio IDE, and it is a good option if you want a lightweight editor for .NET projects. The companies and communities have provided extensions that allow us to work with many distinct languages, and it has become one of the most popular editors for software developers. Also, it is available for any operating system, such as Linux, macOS, and Windows. After downloading the executable from the official Website, you must take the following steps:
Double click on the downloaded executable file. Ensure your user in the operating system has permission to install the software.
Download the Visual Studio extension for C# and Azure, as shown in Figure 1.2:
Figure 1.2: Visual Studio Code extensions
You must have an Internet connection to search for the extensions on the Extensions tab. After typing the extension name, just choose the install option to complete the process.
Introduction to Visual Studio
Visual Studio is a powerful integrated development environment that allows you to create, build, debug, and deploy your .NET applications in one place, including access to external resources such as databases and Azure features. Also, it contains many project templates to get started with software development, including project types based on .NET. To access those templates, just click on the File option on the superior menu and choose the options New Project, as shown in Figure 1.3:
Figure 1.3: Visual Studio Code extensions
As seen in Figure 1.3, the most popular project templates for .NET, such as the Console App application, Asp.Net Core Web API Application, and Blazor App, are available after installing Visual Studio. Each project template has other sub-types to choose from. Those templates are time-saving and help to configure and create new projects using Visual Studio.
Tip: Open-source communities, companies, and individual developers share many free extra templates on the Visual Studio Marketplace website. Also, Visual Studio extensions for many purposes can be downloaded on the same Website to get the best experience in software development and get integrations with third-party tools. They are also available for Visual Studio and Azure DevOps.
After creating a simple project from the template list, you are redirected to the integrated environment for developing your code, having in a common place the text editor with code suggestions support and access to files and external resources, as shown in Figure 1.4:
Figure 1.4: Visual Studio features
You can access your solution's existing folders and files on the Solution Explorer sidebar. Also, it is possible to see all the linked projects in case you have multiple ones as a part of the software development. As seen in Figure 1.4, the Editor sidebar is where you develop your code. It can get tips on code syntax, indentation, code suggestions, previous codification error messages, and navigate into classes, functions, and methods. The editor is customizable, and many settings, such as background color, contrast, and font size, can be changed.
Finally, on the Server Explorer sidebar, it is possible to connect to external resources, databases, servers, cloud resources, and on-premise features. That way, you can keep all the work in a unique and shared place, which has great value in getting high productivity.
In the Toolbar, represented in Figure 1.5, there are options to run the application, save pending changes, modify the debug mode, comment code lines, open new files, and undo recently added code in the editor:
Figure 1.5: Visual Studio toolbar
In the next section, you will learn about Visual Studio Code, a multi-platform IDE option to develop .NET applications.
Introduction to Visual Studio Code
Visual Studio Code seems to be quite different from Visual Studio 2019, but both of them have the exact same purpose: create, debug, build, and deploy applications made using many languages and resources. At its core, it contains many extensions to simplify our routine as developers. The main difference between Visual Studio 2019 and Visual Studio Code is regarding cross-platform development, once Visual Studio Code is available not only in Windows and macOS operation systems, but it is also ready to use in Linux. Another relevant aspect is the performance of this editor compared to a fully integrated development environment. On the other hand, you must enable individual extensions and components according to what you need to develop your software. Figure 1.6 shows how the main screen of Visual Studio Code is configured:
Figure 1.6: Visual Studio Code
You can access all your project's existing folders and files on the Explorer sidebar. Also, it is possible to see all the linked projects in case you have multiple ones as part of the software development process. As seen in Figure 1.5, the Editor sidebar is the place where you develop your code and can get tips on code syntax, indentation, code suggestions, previous codification error messages, and navigate into classes, functions, and methods. The editor is customizable, and many settings, such as background color, contrast, and font size, can be changed. Considering you have installed the C# language extension, the editor is ready to highlight the most important parts of your underlying code, showing in distinct color classes, methods, primitive types, and static text such as String, Integer, or even the custom classes you will create. Different extensions were released as open-source projects for other programming languages, but for all the examples in this book, the C# extension is enough to get the desired results.
The Visual Studio Code is a command-line-based editor. Once you are familiar with the commands, you can benefit from quickly running your application and building and creating new files without using mouse clicks. It is essential to be productive and manage your development environment better. All the commands can be executed on the Terminal sidebar, and the output window is shown the results and the status of the commands. Because of the facility to use the editor combined with command lines by a terminal, Visual Studio became the most popular software editor in the world, and it has been well received even by developers who do not use .NET Core and C# for software development. Considering the editor is a cross-platform and open-source project, it has been broadly used by Linux users and for software development in various programming languages and platforms.
Furthermore, Visual Studio Code supports deployment and source control and is easily integrated with GitHub, Gitlab, and Azure DevOps.
Note: Visual Studio was the IDE used to create, run and build all the code examples of this book, but you can get the same result similarly using Visual Studio, using the command lines present in the appendices of this book.
The traditional and complete Visual Studio 2019 IDE version depends on the existent specification in files whose extension is .sln (solution file) and specific extensions for projects such as .csproj for C# library projects. Although Visual Studio Code allows you to run code by only opening simple folders and files, which is helpful in case you want just to run other languages apart from Microsoft technologies.
Visual Studio Code is more flexible regarding productivity and menu options. It contains many pre-defined commands to get files, search across files and contents, debug and deployment options, and manage and add new extensions, as shown in Figure 1.7:
Figure 1.7: Visual Studio Code - User Interface
In the next section, you will be presented with multi-platform concepts related to the ability to develop applications for multiple operating systems, devices, and cloud providers.
Understanding multi-platform concepts
With the beginning of cloud-based applications in the last decade, the market competition significantly changed from a battle for a software license to cloud system infrastructure services to support the high demand for scalability, modern applications, big data, globalization, and an open market. Therefore, the interest in profitability is no longer focused on operating systems but on reliable infrastructure, as many companies are changing their business model to provide solutions based on the Software as a Service (SaaS) concept.
Nonetheless, big IT companies are still releasing and creating new operation systems, the attention is entirely on open-source projects nowadays, and any modern application must be able to run on Linux, macOS, and Windows platforms. Furthermore, with the rise of the Internet of Things (IoT), the types of devices where applications can be run are much more diverse, making the software development process more complex. Not long ago, every Web developer's biggest concern was successfully running the same application in multiple different browsers properly, which still represents a considerable challenge. But, although web applications are accessed from multiple operating systems, usually, the application is hosted in a server whose specifications are under control, using a single operating system and platform. This scenario rapidly moved to another more complex one, based in a cross-platform environment to have more compatibility, decrease costs, and deliver applications faster to more relevant users across the globe, using a micro-service architecture.
Therefore, to meet these new requirements, Microsoft provided the .NET Core platform. This rich and powerful technology was made to run everywhere and in any operation system, rewriting all the libraries presented on the .NET Framework and getting better results in terms of performance. At the very beginning of the C# language and .NET Framework, they were made entirely to support only the Windows platform, which was an explicit limitation for adoption in the market because in specific scenarios, such as building Desktop applications, the projects of this type require to be written twice or even more times redundantly and, therefore, increase the costs of any project.
Building applications using .NET Core allows you to write a unique application that can be hosted on any platform in many types of devices and distinct cloud infrastructures and to consume it in plenty of Web APIs. It represented a revolutionary change for the .NET world and placed this technology in a prominent position in the market. Also, another important movement of .NET was the fact that it became an open-source project. So, individuals, companies, and academic institutions could contribute to the platform's evolution, and since it became true, the .NET community has been one of the most active communities in the world.
Overview of principal project types in .NET
To get started with .NET Core, we must be aware of the main type of projects it supports. Along with the examples in this book, you will have the opportunity to create various types of applications for multiple platforms (Web, Desktop, and mobile). This section only represents a brief overview of all the types used for building enterprise applications. By default, Visual Studio already contains specific project templates to facilitate the creation of .NET Core projects. Some of them are provided by Microsoft, and others by the technical community as open-source projects.
Once you start a new project using Visual Studio, the following options will be available, as shown in Figure 1.8:
Figure 1.8: Visual Studio Project options
As you can see, a wide variety of programming languages, platforms, and project types are available for .NET applications, including mobile app development, desktop applications, Web development, machine learning, and games.
To get started with the .NET project types, we will create a simple Console App, a standard project for desktop applications that allows us to run applications in the background and integrate them with simple user interaction. After opening Visual Studio and accessing the project type list (Figure 1.8), you must choose the C# language and Windows platform to create the project. It will filter the results to show only the suitable types. Additionally, you can type the word Console
in the search box. That way, you will get the Console application template types, as shown in Figure 1.9:
Figure 1.9: Console Application
If you are unfamiliar with C# language, it will be your first opportunity to get started with the language, structure, keywords, and statements. Once you confirm the project's name in the application creation dialog and make it to the following steps, the result is a simple Console Application containing only one class called Program.cs. To see the content of the class, just open the file by double clicking on it, and Visual Studio will show the class in the code editor section, as shown in Figure 1.10:
Figure 1.10: Console Application
Namespaces organize every C# application, referred to in Figure 1.10 in the first line. A regular enterprise application might have a lot of libraries, packages, and various used namespaces for different purposes. Their names and paths follow a logical sequence hierarchically, and they might belong natively to the C# language as the namespace System. Other ones could be long to a custom or third-party implementation.
Tip: Following and using good name conventions for namespaces, classes, methods, and variables is one of the most important best coding practices, and it is valid for any programming language. Always use meaningful names across the system, applying the Camel Case pattern for classes (for example, MyBpbBook) and Upper Case for constants (for example, MAX_ITEMS_ORDER = 3). Make your code easy to read by others and even by yourself.
Line 3 contains the custom namespace reference for our project (ConsoleAppBook), and it usually is the name of the project or the company organization name, depending on the architecture of the application and the technical decision made by the team. By default, the Console application has a main class called Program and a static method called Main, which will run once the application is started. The content of the Main class in the example is a Console.WriteLine, a method that will print the content I’m learning .NET Core
on the screen and the Console.Read will ask the user for a typed entry.
Once it is running by Visual Studio using the option Start Debugging
in the Debug superior menu option, the application starts, and it shows in the console the printed phrase specified in the method Main,
as shown in Figure 1.11:
Figure 1.11: Console Application debug
Another known type of project template in the .NET platform is the Windows Form project, which allows us to create rich desktop applications using visual components, easily integrated with native desktop components for Windows and other platforms. The first version of .NET Core (1.0 and 2.x) did not support Windows Form applications, but it was fully introduced in .NET Core 3.0 version with relevant improvements if we compare it to the legacy .NET Framework version. One of the most relevant improvements is related to self-contained executable files. The application is built with self-contained mode enabled, and the .NET Core will generate a single file with all the libraries and files the application uses. The executable will contain all the necessary project dependencies, such as .NET Core libraries and third-party implementations. Therefore, the machine where the application will be installed does not need to have any extra library or .NET Core installation. The executable contains everything necessary. It facilitates the deployment, installation, maintenance, and even software updates in any environment, and it works appropriately for Windows operating systems and even for Linux and macOS.
To get started with the creation of Windows Form applications, you must go to the creation template dialog already shown in this chapter, search for windows form, and the results will show the Windows Form project type as a suggestion, as demonstrated in Figure 1.12:
Figure 1.12: Windows Form
Visual Studio provides an extensive list of visual components that can be used in Windows Form applications. The available list is helpful because almost all enterprise applications share the same or at least similar visual components. Among them are specific ones for common form controls such as TextBox, Label, and ListView. It also contains many options for native operation systems features such as media player, embedded browser, file and folders manipulations, and much more. To access the complete available list, go to the left sidebar on Visual Studio and open the Toolbox tab.
You can drag and drop components directly into the form and change their properties visually or access the property tab, selecting a component and clicking on the Properties tab in the right sidebar, as shown in Figure 1.13:
Figure 1.13: Windows Form
To include a logical statement to your components, you must double click on the control to access the source code control associated with the component. Each visual control has custom events associated with the component type. Considering you can start your logical coding in a design mode, Windows Forms is one of the easiest ways to learn C# language because you can quickly see the results of your code working. Also, once the project is created in Visual Studio, the program is ready to run without extra configurations, increasing productivity and deployment. As a cross-platform solution, a Windows Forms application built in .NET Core is ready to be used and installed on any platform, such as Linux, macOS, and Windows operation system.
To get started with Windows Form application development, once you have created the application on Visual Studio, drag and drop a TextBox and Button controls to the form, and you can position those as you want. Selecting each control, just go to the Properties tab on the right sidebar and change the "Name property to
txtName for the TextBox and
myButton for the button control. After doing that and pressing the
Start" button on the superior toolbox, the Windows Form application will run, and the design is going to look like Figure 1.14:
Figure 1.14: Windows Form
As a concept and following a programmer's perspective, the Windows Forms project type was made simple and fast to create. Unlike Web development, you can create in visual mode all the screens of your system and code just the logical part of the events, methods, and buttons such as database call operations, communicating with external API’s or even making business operations to meet the user system requirement.
Once you have brief contact with two desktop project types in .NET Core (Console and Windows Forms), there are other project types that you must know regarding Web Development. The first is the Asp.Net Core Web API project type, which allows us to create Application Programming Interface (API) applications based on REST, meeting all the most modern requirements for Web Services, security, and performance.
Tip: REST is the contraction of Representational State Transfer, a standard architecture to build and define Web Services. The most significant advantage of this approach is the interoperability across many languages and platforms. Also, modern Web APIs are mainly based on JSON (JavaScript Object Notation), a broadly used pattern for data object transfer between systems. All modern languages provide methods to handle JSON objects properly. It became the most popular way to integrate systems.
To create a Web API project in .NET Core, you must search Asp.Net Core on the search box in the Visual Studio project creation dialog, give it a name, and it will be redirected to the list of available Web project types in .NET. Choose the API type and finish the final step, as shown in Figure 1.15.
Figure 1.15: Windows Form
Once the creation is complete, the Visual Studio generates a Web API with a basic structure based on Model View Controller architecture, which will be explained further in this book. Each API has multiple methods called endpoints as well. Each exposed method could contain security settings, allow HTTP verbs to be configured, and also logical programming to make the necessary operations according to the main target of each endpoint. To create a new endpoint, you must take these steps:
Create a new Controller by clicking the right mouse button on the Controller folder shown in the Solution Explorer right sidebar. There are a few Controller types, but it needs to be of the kind of API Controller Empty.
Give a name to the Controller, for instance, CustomerController,
and save it.
The Visual Studio will generate a Controller class without a method. To insert one, create a public method that returns a string type and an underlying string content for the test.
After creating a public method, it is necessary to specify the HTTP Verb allowed for this method instance, HttpGet, which specifies that this method should be called using the GET method by the application which will use this API. Usually, different HTTP Verbs are used for distinct purposes: for example, POST is used for inserting data, and GET retrieves data by the API.
Once all the steps are done, your code will look like Figure 1.16:
Figure 1.16: Web API code
For organizational purposes, all the controllers in a Web API project usually share the same namespace as WebAPIBook., as you can see in line 8. Also, each Controller must use Annotations above the class name to indicate to the .NET Core application "ApiController" that this Controller should be accessed as a