MPA vs SPA
Technical Stack for Modern MPA
What This Book Includes
Is This Book for You
What Do You Need to Use This Book
How This Book is Organized
Using Code Examples
Welcome to Practical Multiple-Page Apps with ASP.NET Core and Angular Elements. This book will provide all the tools you need to develop ultra-modern multiple-page applications (MPAs) using ASP.NET Core Razor Pages, Angular elements, WebPack, RxJS, and mini-SPAs (Single-Page Applications). I hope that this book will be useful for web programmers, business developers, and students of all skill levels who are interested in web-application developments.
A web-based application is a software that can be accessed through the internet. The software and database are resided in a central server. It takes advantage of today’s technology to enhance the productivity and efficiency. Web or distributed applications are mostly used on client-server networks where the user’s computer accesses information from the server or cloud computing server. There are some obvious advantages of web applications over desktop applications. Web applications need to be installed only once, which can avoid the burden in deploying in each client machine and make software updates and maintenance much easier. They are platform independent, adaptable in mobile application, and can be accessed from anywhere in the world.
Here, I would like to point out the difference between the website and web application, even though they are not entirely exclusive and the difference is simply a matter of perspective. Websites are primarily informational and defined by their content. In this sense, https://www.cnn.com and http://www.espn.com are websites, not web applications. Web applications are primarily interactive, and allow users to perform actions. Google Gmail and Google Maps are examples of web applications. That is, a website consists of static content repository that is dealt out to all visitors, while a web application depends on interaction and requires user input and data processing.
MPA vs SPA
If you are thinking about creating your own web applications, you have probably heard that there are two main design patterns: multi-page application (MPA) and single-page application (SPA). MPA works in a more traditional way. It has slower speed because it needs to fetch pages from the server for each request. The main advantage of MPA is its simple search engine optimization (SEO) scheme. The architecture of MPA allows you to optimize every page for search engines. Developer can easily add the Meta tags for any page. Another advantage is that developing a multi-page application is much easier than SPA because MPA requires a smaller stack of technologies. MPA is more suitable for developing websites.
SPA is a web application that fits on a single web page with dynamic actions without the need to refresh the page. It is fast, as most resources like HTHL, CSS, and Scripts are only loaded once throughout the lifespan of the application. SPA allows you to simulate the work of a desktop application. The architecture of SPA is arranged in such a way that when you go to a new page, only a portion of the content is updated. Thus, there is no need to reload the same elements from the server.
SPA is highly decoupled between client-side and server, and the codes for both ends can be kept separately. SPA interacts with the server only through the web service (or web API) built by server-side developers to fetch and display data. On the other hand, MPA has a tight coupling of front-end and back-end. It is usually one big project, which will house all the codes together. The codes in MPA for both ends are often inter-dependent. SPA also simplifies mobile development as we can reuse the same backend for web application and native mobile application, while MPA usually requires separate code base for mobile application.
SPA is a good fit for web application development. However, building single-page applications is a complicated task. It can be challenge to integrate server-side and client-side code well or even just to choose a productive project setup in the first place. This means that SPA usually requires more development effort to build than MPA does.
Technical Stack for Modern MPA
In real world, a modern and complicated web site is often built with a hybrid approach – the simplest being host one or more mini-SPA-like applications within a larger multiple-page web application.
The technical stack is a set of tools and frameworks that are used in development of a software product. In web development for modern MPAs, the technical stack consists of a very specific set of components that work together to create a functioning web application. Technical stack for web applications can be divided into two groups according to the aspects it concerns: backend and frontend.
The technical stack for backend or server-side consists of operating system, web server, database, and programming logic. Here are some of commonly used platforms:
- LAMP – Linux, Apache, MySQL, PHP/Perl/Python.
- .NET – Windows, IIS, SQL Server, C#/ASP.NET/ASP.NET Core MVC/ASP.NET Core Razor Pages.
- MEAN – Mongo DB, Express.js, AngularJS, Node.js.
In this book, we will choose ASP.NET Core Razor Pages as our backend framework. As we all known, there are two choices in ASP.NET Core web applications: MVC or Razor Pages. ASP.NET MVC is a complex framework that contains many concepts such as Route collection, ViewData, Lambda expression, custom route, Controller Action, etc., all of which tie the Model, View, and Controller. On the other hand, Razor Pages apps are more organized compared to MVC. In Razor Pages, we have a Razor view and a code behind file, similar to that the old ASP.NET WebForms did. Each page is self-contained with its own view and code organized together, resulting in a simpler app. Razor Pages make coding page-focused scenarios easier and more productive.
The front-end or client-side components enable the user’s interaction with the web application. The front-end stack for web applications includes
- Package Managements – npm, yarn.
- Bundle, Minify, Task Runners – Gulp, Grunt, WebPack, Browserify, Rullup, etc.
- HTTP Requests – Ajax, RxJS.
Currently, when starting a new web application project, building system setup is among the top priority tasks. We often use task runners, such as Gulp and Grunt, to automate tasks like static code analysis, test cases execution, etc. On the other hand, WebPack is a bundler and it comes in handy when you need to pack multiple assets together into a dependency graph. There is a rising tendency to use WebPack instead of Gulp and Grunt because WebPack can fulfil nearly all tasks that you would perform through a task runner; it comes with minification options and bundle source maps; it can serve as middleware via a custom server, namely webpack-dev-server, which supports both hot and live reloading.
What this Book Includes
This book and its sample code listings, which are available for download at my website at https://drxudotnet.com, provide you with
- Ready-to-run example programs that allow you to explore the MPA web programming techniques and understand how a variety of technical stacks work together. You can modify the code examples or add new features to them to form the basis of your own MPA projects. The example code listings provided in this book will be a good start point that you can use directly in your own real-world MPA projects.
- Many classes and components in the sample code listings that you will find useful in your MPA development. These classes and components include Razor Pages basics, Razor Class Library creation, TypeScript and Webpack configurations, infrastructure for building Angular elements, RxJS workflow, and the integration of various parts together to form our final product – a modern MPA project. You can extract these classes and components and plug them into your own web applications.
Is this Book for You?
Web programmers and .NET developers can use the majority of the example programs in this book routinely. Throughout the book, I emphasize the usefulness of web application development to real-world problems. If you follow the instructions presented in this book closely, you will easily be able to develop modern multiple-page web applications with various technical stacks, including Razor Pages, Razor Class Library, TypeScript, WebPack, RxJS, Angular Elements, and mini SPAs. At the same time, I will not spend too much time discussing the programming style, execution speed, and code optimization, because a plethora of books out there already deals with these topics. Most of the example programs you will find in this book omit error handlings, which makes the code easier to understand by focusing only on the key concepts and practical applications.
What Do You Need to Use this Book
You will need no special equipment to make the best use of this book and understand the algorithms. This book will take full advantage of open source frameworks and libraries. To run and modify the sample programs, you will need a computer capable of running either Windows 7, 8, or 10. The server-side software installed on your computer should include Visual Studio 2019 (Community version is fine), the .NET Core 3.0 SDK or higher, and SQL Server Express 2016 or higher. For the front end, you need to install Node.js version 12.4 or later, Angular 7 or later, and Angular 7 CLI or later.
If you have .NET Core 2.2 or 2.1 SDK, SQL Server Express 2014 or older versions, and Angular 6 or older version, you can also run most of the sample code with few modifications. Please remember, however, that this book is intended for .NET Core 3.0, SQL Server Express 2016, and Angular 7 and that all of the example programs were created and tested on this platform, so it is best to run the sample code on the same platform.
How the Book Is Organized
This book is organized into thirteen chapters, each of which covers a different topic about modern multiple-page web application using .NET Core Razor Pages and Angular Elements. The following summaries of each chapter should give you an overview of the book’s content:
Chapter 1, Introduction to ASP.NET Core Razor Pages
This chapter provides an overview of ASP.NET Core Razor Pages, which explains how to set up the tools and development environment required for building the back-end framework using Razor Pages. It also illustrate how to create Razor Page applications using examples.
Chapter 2, TypeScript and WebPack
This chapter provides a quick review of TypeScript and WebPack, which shows how to set up tools, packages, and development environment for TypeScript and WebPack to work in an ASP.NET Core Razor Page project.
Chapter 3, RxJS Basics
This chapter provides an introduction to RxJS that is a library for transforming, composing, and querying streams of data. We use various example to explains how RxJS allows you to work with asynchronous data streams with ease and how it allows to state what your code should do, without having to code every step to do it. This results in a more reliable and maintainable approach to building your Web applications.
Chapter 4, Database and Web Services
This chapter introduces the SQL Server and Entity Framework Core, which are built-in features and shipped as part of the core product of Visual Studio 2019. It shows you how to create simple database and how to interact with the data using RxJS via ASP.NET API web service.
Chapter 5, Angular Basics
This chapter provides a quick review of various features available in the Angular framework, including one- and two-way data binding, components, templates, routing, dependency injection, directives, and Inputs/Output decorators.
Chapter 6, Introduction to Angular Elements
This chapter explains the concept of Angular Elements that is a new package in Angular that help you publish Angular components as custom elements. It shows how to create Angular Elements and how to use them in our MPA development environment.
Chapter 7, Web Services in Angular Elements
This chapter introduces the HttpClient module and explains how to use it in Angular Elements to interact with the ASP.NET Core Web API and perform CRUD operations on database.
Chapter 8, Charts and Graphics
Data visualization plays a critical role in a modern web application. This chapter shows you how to use a client chart library called ECharts to create various charts and display the data from database via Angular Elements.
Chapter 9, Real-Time Apps with SignalR
This chapter show how to use SignalR to create real-time applications within both the Razor Page and Angular Elements using various examples, including a simple real-time chat, real-time whiteboard, streaming, and real-time stock chart.
Chapter 10, Machine Learning
This chapter discusses the advanced analysis techniques: machine learning. Machine-learning technique has become one of the most promising fields in various business applications. This chapter explains how to use Microsoft ML.NET package to solve various machine-learning problems, including binary classification, multiclass classification, regression, and clustering.
Chapter 11, Mini SPAs
Chapter 12, Authentication and Authorization
This chapter explains how to add the ASP.NET Core Identity to our existing MPA project and how specific pages can be restricted to users who are registered and logged in to the application.
Chapter 13, Advanced Topics in Razor Pages
This chapter covers some advanced topics in Razor Pages, including the detailed explanation on dependency injection, custom tag helper, and Razor class Library.
Using Code Examples
You may use the code in this book in your own applications and documentation. You do not need to contact the author or the publisher for permission unless you are reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing the example code listings does require permission. Incorporating a significant amount of example code from this book into your applications and documentation also requires permission. Integrating the example code from this book into commercial products is not allowed without written permission of the author.
I am always interested in hearing from readers, and enjoy learning of your thoughts on this book. You can send me comments by e-mail to: jxu at DrXuDotNet.com. I also provide updates, bug fixes, and ongoing support via my website:
You can also obtain the complete source code for all of examples in this book from the foregoing website. At the same time, I created a code demonstration website at https://quant.gincker.com that shows a live demo for all examples included in this book.