Building Blazor WebAssembly Applications with gRPC

Book description

Explore the power of Blazor WebAssembly, gRPC, and source generators for easy and quick web development

Key Features

  • Get to grips with integration between Blazor, gRPC, and source generators in real-life projects
  • Develop a complete Blazor WebAssembly project that takes advantage of gRPC's capabilities
  • Explore best practices for building a high-performance web application with Blazor and C#

Book Description

Building Blazor WebAssembly Applications with gRPC will take you to the next level in your web development career. After working through all the essentials of gRPC, Blazor, and source generators, you will be far from a beginner C# developer and would qualify as a developer with intermediate knowledge of the Blazor ecosystem.

After a quick primer on the basics of Blazor technology, REST, gRPC, and source generators, you'll dive straight into building Blazor WASM applications. You'll learn about everything from two-way bindings and Razor syntax to project setup. The practical emphasis continues throughout the book as you steam through creating data repositories, working with REST, and building and registering gRPC services. The chapters also cover how to manage source generators, C# and debugging best practices, and more. There is no shorter path than this book to solidify your gRPC-enabled web development knowledge.

By the end of this book, your knowledge of building Blazor applications with one of the most modern and powerful frameworks around will equip you with a highly sought-after skill set that you can leverage in the best way possible.

What you will learn

  • Master routing and test your learning with demo applications
  • Create service and controller classes for your API endpoints
  • Use gRPC with Blazor instead of REST and revamp your applications
  • Study partial classes, attributes, and more in source generators
  • Write reusable Razor components and debug your code effectively
  • Understand the semantic model of C# code
  • Discover how to read and navigate through syntax trees
  • Build dynamic websites without using JavaScript

Who this book is for

This book is for beginner C# developers who want to learn how to create more performant web apps with less code using Blazor, the gRPC protocol, and source generators. The book assumes a basic understanding of C#, HTML, and web development concepts.

Table of contents

  1. Building Blazor WebAssembly Applications with gRPC
  2. Contributors
  3. About the author
  4. About the reviewer
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Share Your Thoughts
  6. Chapter 1: Introducing Blazor, gRPC, and Source Generators
    1. Technical requirements
    2. Using the Blazor framework to create websites
      1. Using Blazor WebAssembly for single-page applications
      2. Harnessing advantages of progressive web applications using Blazor
      3. Understanding how Blazor uses Razor syntax
      4. Using JavaScript in Blazor
    3. Understanding the REST API as the default Blazor communication interface
    4. Understanding gRPC and how it is different from REST
      1. How protocol buffers work
      2. Why we still use REST
    5. What are source generators and how do they work?
    6. Summary
    7. Further reading
  7. Chapter 2: Creating a Blazor WebAssembly Application
    1. Technical requirements
    2. Learning how to write Razor syntax
      1. Writing implicit Razor expressions
      2. Writing explicit Razor expressions
      3. Writing inline expressions
      4. Writing code block expressions
      5. Writing control structures
      6. Writing top-level directives
      7. Writing inline directives
    3. Creating Razor components
      1. Using components in other components
      2. Passing parameters to components
      3. Creating components with child content
      4. Communicating with the parent component
      5. Understanding the component life cycle
      6. Structuring component code
    4. Understanding page routing in Blazor
      1. Navigating between pages
      2. Page directive
      3. Route constraints
      4. Catch-all parameters
    5. Project overview and preparation
      1. Creating a demo Blazor WebAssembly project
      2. Examining the project
      3. Preparing the demo project
    6. Summary
  8. Chapter 3: Creating a Database Using Entity Framework Core
    1. Technical requirements
    2. Creating a data repository using EF
      1. Installing NuGet packages
      2. Enabling EF
      3. Creating database representation in C#
      4. Configuring EF Core
      5. Migrating code to the database
      6. Creating entities in the database
    3. Using generic services for data manipulation
      1. Creating models
      2. Mappings between models
    4. Creating data services
      1. Creating a new record
      2. Reading a single record
      3. Reading all records
      4. Updating records
      5. Deleting records
    5. Registering data services
    6. Summary
    7. Further reading
  9. Chapter 4: Connecting Client and Server with REST API
    1. Technical requirements
    2. Exposing CRUD operations in API controllers
    3. Consuming a REST API in Blazor components
      1. Creating and editing data
      2. Viewing the data
      3. Extending the demo project
    4. Summary
  10. Chapter 5: Building gRPC Services
    1. Technical requirements
    2. What are the benefits of using gRPC services?
    3. How does communication work in gRPC?
    4. Understanding the protocol buffer language
      1. Creating messages
      2. Field types
      3. Default values
      4. The reserved keyword
      5. Defining methods
    5. gRPC services in C#
      1. Two ways of implementing
      2. Implementing gRPC services using .proto files
    6. Creating Blazor components
      1. Preparation for generic components
      2. Creating generic list components
      3. Creating generic form components
    7. Summary
  11. Chapter 6: Diving Deep into Source Generators
    1. Technical requirements
    2. What can source generators generate?
      1. What is a syntax tree?
      2. What is a semantic model?
      3. Implementing source generators
    3. Using partial classes and methods
      1. Partial classes
      2. Partial methods
    4. Creating attributes for your needs
    5. Writing the first generator
      1. Required packages
      2. Limiting the generator
      3. Generating the code
      4. Referencing the generators
    6. Exploring generated code
    7. Summary
  12. Chapter 7: Best Practices for C# and gRPC
    1. gRPC is not the new REST
    2. Generated code can be harder to debug
      1. Exploring generated code
      2. Using a debugger
      3. Emitting generated files
      4. Testing the generator
    3. Type less, generate more
    4. Summary
  13. Index
    1. Why subscribe?
  14. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share Your Thoughts

Product information

  • Title: Building Blazor WebAssembly Applications with gRPC
  • Author(s): Václav Pekárek
  • Release date: November 2022
  • Publisher(s): Packt Publishing
  • ISBN: 9781804610558