ASP.NET 8 Best Practices

Book description

Improve your ASP.NET skills with industry-proven techniques and practices to make your code efficient and maintainable throughout the software development lifecycle

Key Features

  • Get to grips with standard guidelines for every phase of the SDLC, encompassing pre-coding, coding, and post-coding stages
  • Build high-quality software by employing industry best practices throughout the development process
  • Apply proven techniques to improve your coding, debugging, and deployment processes for websites
  • Purchase of the print or Kindle book includes a free PDF eBook

Book Description

As .NET 8 emerges as a long-term support (LTS) release designed to assist developers in migrating legacy applications to ASP.NET, this best practices book becomes your go-to guide for exploring the intricacies of ASP.NET and advancing your skills as a software engineer, full-stack developer, or web architect.

This book will lead you through project structure and layout, setting up robust source control, and employing pipelines for automated project building. You’ll focus on ASP.NET components and gain insights into their commonalities. As you advance, you’ll cover middleware best practices, learning how to handle frontend tasks involving JavaScript, CSS, and image files. You’ll examine the best approach for working with Blazor applications and familiarize yourself with controllers and Razor Pages. Additionally, you’ll discover how to leverage Entity Framework Core and exception handling in your application. In the later chapters, you’ll master components that enhance project organization, extensibility, security, and performance.

By the end of this book, you’ll have acquired a comprehensive understanding of industry-proven concepts and best practices to build real-world ASP.NET 8.0 websites confidently.

What you will learn

  • Explore the common IDE tools used in the industry
  • Identify the best approach for organizing source control, projects, and middleware
  • Uncover and address top web security threats, implementing effective strategies to protect your code
  • Optimize Entity Framework for faster query performance using best practices
  • Automate software through continuous integration/continuous deployment
  • Gain a solid understanding of the .NET Core coding fundamentals for building websites
  • Harness HtmlHelpers, TagHelpers, ViewComponents, and Blazor for component-based development

Who this book is for

This book is for developers who have working knowledge of ASP.NET and want to advance in their careers by learning best practices followed in developer communities or corporate environments. Beginners can use this book as a springboard for integrating best practices into their learning journey, and as a reference to gain clarity on advanced ASP.NET topics at a later time.

Table of contents

  1. ASP.NET 8 Best Practices
  2. Contributors
  3. About the author
  4. About the reviewers
  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. Conventions used
    6. Get in touch
    7. Share Your Thoughts
    8. Download a free PDF copy of this book
  6. Chapter 1: Taking Control with Source Control
    1. Technical requirements
    2. Branching Strategies
      1. GitFlow
      2. Hotfix branches
      3. GitHub Flow
      4. GitLab Flow
    3. Creating short-lived branches
    4. Understanding Common Practices
      1. Rebase when Private, Merge when Public
      2. Always “Get Latest” Before Committing
      3. Always Build and Test Before Committing
      4. Avoid Committing Binaries
      5. Use tags for versioning
    5. Summary
  7. Chapter 2: CI/CD – Building Quality Software Automatically
    1. Technical requirements
    2. What is CI/CD?
    3. Preparing your Code
      1. Building Flawlessly
      2. Avoiding Relative Path Names with File-based Operations
      3. Confirming that your Unit Tests are Unit Tests
      4. Creating Environment Settings
    4. Understanding the Pipeline
      1. Pulling Code
      2. Building the application
      3. Running Unit Tests/Code Analysis
      4. Creating Artifacts
      5. Creating a Container
      6. Deploying the software
    5. The Two “Falling” Approaches
      1. Falling Backward (or fallback)
      2. Falling Forward
    6. Deploying Databases
      1. Backing up Before Deploying
      2. Creating a Strategy for Table Structures
      3. Creating a Database Project
      4. Using Entity Framework Core’s Migrations
    7. The three Types of Build Providers
    8. CI/CD Providers
      1. Microsoft Azure Pipelines
      2. GitHub Actions
      3. Amazon CodePipeline
      4. Google CI
    9. Walkthrough of Azure Pipelines
      1. Preparing the Application
      2. Introducing Azure Pipelines
      3. Identifying the Repository
      4. Creating the Build
      5. Creating the Artifacts
      6. Creating a Release
      7. Deploying the Build
    10. Summary
  8. Chapter 3: Best Approaches for Middleware
    1. Technical requirements
    2. Using Middleware
      1. Understanding the Middleware Pipeline
      2. Using Request Delegates – Run, Use, and Map
    3. Common Practices for Middleware
      1. Defer to Asynchronous
      2. Prioritizing the Order
      3. Consolidating existing Middleware
      4. Encapsulating your Middleware
    4. Creating an Emoji Middleware Component
      1. Encapsulating the Middleware
      2. Examining the Component’s Pipeline
    5. Summary
  9. Chapter 4: Applying Security from the Start
    1. Technical requirements
    2. Developing Security
      1. Do I have any sensitive data to protect?
      2. Am I exposing anything through the application?
      3. Am I sanitizing user input?
      4. Securing Access
    3. Common Security Practices
      1. Logging
      2. Keep your Framework and Libraries Current
      3. Always Force SSL
      4. Never Trust the Client
      5. Always Encode User Input
      6. Securing Your Headers
      7. Securing Entity Framework Core
      8. Use Microsoft Entra for Securing Applications
      9. Protecting Your Pages with Anti-Forgery
    4. Safeguarding Against the Top 3 Security Threats
      1. Broken Access Control
      2. Cryptographic Failures
      3. Injection
    5. Summary
  10. Chapter 5: Optimizing Data Access with Entity Framework Core
    1. Technical requirements
    2. Entity Framework Core Implementations
      1. Repository/Unit of Work
      2. The Specification Pattern
      3. Extension Methods
    3. Common Entity Framework Core Practices
      1. Confirming Your Model
      2. Using Async/Await
      3. Logging Your Queries
      4. Using Resources for Large Seed Data
      5. Understanding Deferred Execution
      6. Using a Read-Only State with .AsNoTracking()
      7. Leveraging the Database
      8. Avoiding the Manual Property Mapping
    4. Implementing the Theme Park Example
      1. Overview
      2. Creating the Database
      3. Adding an Asynchronous Read-Only Mode
      4. Including Child Entities
      5. Extending your Model
    5. Summary
  11. Chapter 6: Best Practices with Web User Interfaces
    1. Technical requirements
    2. Using a task runner
      1. What is a task runner?
      2. Setting up the Task Runner
      3. Structure of a gulpfile
      4. Running automatically
      5. Creating a workflow structure
      6. Defining our workflow paths
      7. Transpiling TypeScript
      8. Bundling and minifying
      9. Implementing additional tasks
    3. Applying standards to UIs
      1. Centralizing your site links
      2. Keeping controllers/pages small
      3. Using ViewComponents
      4. Using Tag Helpers instead of HTML Helpers
      5. Creating SEO-friendly URLs
    4. Introducing Buck’s coffee shop project
      1. Setting up Buck’s website
      2. Updating the links
      3. Creating an OffCanvas Tag Helper
    5. Summary
  12. Chapter 7: Testing Your Code
    1. Technical requirements
    2. Understand testing concepts
      1. Unit tests
      2. Integration tests
      3. Regression tests
      4. Load testing
      5. System testing (end-to-end or E2E)
      6. UI testing
    3. Best approaches for testing
      1. Why do we write tests?
      2. The “100% test coverage” myth
      3. Using AAA
      4. Avoid writing unit test code for your code
      5. Avoid large unit tests
      6. Avoid unnecessary mocks, fakes, or stubs
      7. Using tests as documentation
      8. Identifying slow integration tests
      9. Find a bug, write a test
      10. Avoid testing .NET
    4. Testing data access
      1. Adding the SQLite provider
      2. Creating the AttractionService test
      3. Creating the LocationService test
    5. Summary
  13. Chapter 8: Catching Exceptions with Exception Handling
    1. Technical requirements
    2. Using exception handling
      1. What is exception handling?
      2. When to use exception handling
    3. Handling global exceptions
    4. Performance considerations
    5. Common exception handling techniques
      1. Prevention before exception
      2. Use logging
      3. Apply a unit testing methodology
      4. Avoid empty catch statements
      5. Use exception filtering and pattern matching
      6. Use finally blocks for cleanup
      7. Knowing when to throw
    6. Summary
  14. Chapter 9: Creating Better Web APIs
    1. Technical requirements
    2. Creating APIs quickly
      1. Using Visual Studio
      2. Why minimal APIs?
    3. Designing APIs
      1. Disconnecting from existing schemas
      2. Identifying the resources
      3. Relating HTTP verbs to resources
      4. Returning HTTP status codes
    4. Testing Web APIs
      1. Visual Studio Endpoints Explorer
      2. Integration testing APIs
    5. Standardized Web API techniques
      1. Using the right HTTP verbs and status codes
      2. Beware dependent resources
      3. Pagination in API results
      4. Versioning APIs
      5. Use DTOs, not entities!
      6. Avoid new instances of HttpClient
    6. Summary
  15. Chapter 10: Push Your Application with Performance
    1. Technical requirements
    2. Why Performance Matters
    3. Establishing Baselines
      1. Using Client-Side Tools
      2. Using Server-Side Tools
      3. Databases
    4. Applying Performance Best Practices
      1. Optimizing client-side performance
      2. Common Server-side Practices
      3. Understanding caching
    5. Summary
  16. Chapter 11: Appendix
    1. Technical requirements
    2. Programming guidelines
      1. DRY
      2. YAGNI
      3. KISS
      4. Separation of concerns
      5. Refactoring as a process
      6. SOLID principles
    3. Project structure
      1. Understanding the project landscape
      2. Creating project layers
    4. Summary
      1. Thank you!
  17. Index
    1. Why subscribe?
  18. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share Your Thoughts
    3. Download a free PDF copy of this book

Product information

  • Title: ASP.NET 8 Best Practices
  • Author(s): Jonathan R. Danylko
  • Release date: December 2023
  • Publisher(s): Packt Publishing
  • ISBN: 9781837632121