Hands-On Design Patterns with C# and .NET Core

Book description

Apply design patterns to solve problems in software architecture and programming using C# 7.x and .NET Core 2

Key Features

  • Enhance your programming skills by implementing efficient design patterns for C# and .NET
  • Explore design patterns for functional and reactive programming to build robust and scalable applications
  • Discover how to work effectively with microservice and serverless architectures

Book Description

Design patterns are essentially reusable solutions to common programming problems. When used correctly, they meet crucial software requirements with ease and reduce costs. This book will uncover effective ways to use design patterns and demonstrate their implementation with executable code specific to both C# and .NET Core.

Hands-On Design Patterns with C# and .NET Core begins with an overview of object-oriented programming (OOP) and SOLID principles. It provides an in-depth explanation of the Gang of Four (GoF) design patterns, including creational, structural, and behavioral. The book then takes you through functional, reactive, and concurrent patterns, helping you write better code with streams, threads, and coroutines. Toward the end of the book, you’ll learn about the latest trends in architecture, exploring design patterns for microservices, serverless, and cloud native applications. You’ll even understand the considerations that need to be taken into account when choosing between different architectures such as microservices and MVC.

By the end of the book, you will be able to write efficient and clear code and be comfortable working on scalable and maintainable projects of any size.

What you will learn

  • Make your code more flexible by applying SOLID principles
  • Follow the test-driven development (TDD) approach in your .NET Core projects
  • Get to grips with efficient database migration, data persistence, and testing techniques
  • Convert a console application to a web application using the right MVP
  • Write asynchronous, multithreaded, and parallel code
  • Implement MVVM and work with RxJS and AngularJS to deal with changes in databases
  • Explore the features of microservices, serverless programming, and cloud computing

Who this book is for

If you have a basic understanding of C# and the .NET Core framework, this book will help you write code that is easy to reuse and maintain with the help of proven design patterns that you can implement in your code.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Hands-On Design Patterns with C# and .NET Core
  3. Dedication
  4. About Packt
    1. Why subscribe?
  5. Foreword
  6. Contributors
    1. About the authors
    2. About the reviewers
    3. Packt is searching for authors like you
  7. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Code in Action
      3. Download the color images
      4. Conventions used
    4. Get in touch
      1. Reviews
  8. Section 1: Essentials of Design Patterns in C# and .NET Core
  9. Overview of OOP in .NET Core and C#
    1. Technical requirements
      1. Installing Visual Studio
      2. Setting up .NET Core
      3. The models used in this book
    2. OOP and how classes and objects work
      1. Explaining OOP
      2. A class
      3. An object
        1. Associations
      4. An interface
    3. Inheritance
      1. Types of inheritance
    4. Encapsulation
    5. Polymorphism
      1. Static polymorphism
      2. Dynamic polymorphism
        1. Interface polymorphism
        2. Inheritance polymorphism
        3. Generics
    6. Summary
    7. Questions
  10. Modern Software Design Patterns and Principles
    1. Technical requirements
      1. Installing Visual Studio
      2. Setting up .NET Core
    2. Design principles
      1. DRY – Don't Repeat Yourself
      2. KISS – Keep It Simple Stupid
      3. YAGNI – You Aren't Gonna Need It
      4. MVP – Minimum Viable Product
      5. SOLID
        1. Single responsibility principle
        2. Open/closed principle
        3. Liskov substitution principle
        4. Interface segregation principle
        5. Dependency inversion principle
    3. Software patterns
      1. GoF patterns
        1. Creational patterns
        2. Structural patterns
          1. Decorator patterns
        3. Behavioral patterns
          1. Chain of responsibility
          2. Observer pattern
      2. Enterprise integration patterns
        1. Topology
        2. Patterns
          1. Messaging
          2. Transformation
          3. Routing
    4. Software development life cycle patterns
      1. Waterfall SDLC
      2. Agile SDLC
    5. Summary
    6. Questions
  11. Section 2: Deep Dive into Utilities and Patterns in .NET Core
  12. Implementing Design Patterns - Basics Part 1
    1. Technical requirements
      1. Installing Visual Studio
      2. Setting up .NET Core
    2. Minimum Viable Product
      1. Requirements
      2. How does MVP fit with future development?
    3. Test-driven development
      1. Why did the team choose TDD?
      2. Setting up the projects
      3. Initial unit test definitions
    4. Abstract Factory design pattern
      1. InventoryCommand abstract class
    5. SOLID principles
      1. Single responsibility principle (SRP)
      2. Open/closed principle (OCP)
      3. Liskov substitution principle (LSP)
      4. Interface segregation principle (ISP)
      5. Dependency inversion principle
      6. InventoryCommand unit tests
        1. Access modifiers
        2. Helper TestUserInterface
        3. Example unit test – QuitCommand
    6. Summary
    7. Questions
  13. Implementing Design Patterns - Basics Part 2
    1. Technical requirements
      1. Installing Visual Studio
      2. Setting up .NET Core
    2. The singleton pattern
      1. Processes and threads
      2. The repository pattern
      3. Unit tests
      4. A race condition illustration
      5. AddInventoryCommand
        1. TestInventoryContext
        2. AddInventoryCommandTest
      6. UpdateQuantityCommand
        1. UpdateQuantityCommandTest
      7. GetInventoryCommand
        1. GetInventoryCommandTest
    3. The factory pattern
      1. Unit tests
        1. Issue one – UnknownCommand
          1. InventoryCommandFactoryTests
        2. Issue two – case-insensitive text commands
    4. Features in .NET Core
      1. IServiceCollection
      2. CatalogService
      3. IServiceProvider
    5. Console application
    6. Summary
    7. Questions
  14. Implementing Design Patterns - .NET Core
    1. Technical requirements
      1. Installing Visual Studio
      2. Setting up .NET Core
    2. .Net Core service lifetimes
      1. Transient
      2. Scoped
      3. Singleton
      4. Back to FlixOne
        1. Unit tests
      5. Scope
    3. Implementation factory
      1. IInventoryContext
      2. IInventoryReadContext
      3. IInventoryWriteContext
      4. InventoryCommandFactory
      5. InventoryCommand
        1. Implementation factory using a function
        2. Using services
        3. Using third-party containers
    4. Summary
    5. Questions
  15. Implementing Design Patterns for Web Applications - Part 1
    1. Technical requirements
      1. Installing Visual Studio
      2. Setting up .NET Core
      3. Installing SQL Server
    2. Creating a .Net Core web application
      1. Kicking off the project
        1. Developing requirements
      2. Crafting a web application
        1. Web applications and how they work
        2. Coding the web application
    3. Implementing CRUD pages
    4. Summary
    5. Questions
    6. Further reading
  16. Implementing Design Patterns for Web Applications - Part 2
    1. Technical requirements
      1. Installing Visual Studio
      2. Setting up .NET Core
      3. Installing SQL Server
    2. Extending the .NET Core web application
      1. Project kickoff
        1. Requirements
          1. Business requirements
          2. Technical requirements
        2. Challenges
          1. Challenges for developers
          2. Challenges for businesses
          3. Finding a solution to the problems/challenges
    3. Authentication and authorization
      1. Authentication in action
        1. Why does it make a difference?
      2. Authorization in action
      3. Creating a web test project
    4. Summary
    5. Questions
    6. Further reading
  17. Section 3: Functional Programming, Reactive Programming, and Coding for the Cloud
  18. Concurrent Programming in .NET Core
    1. Technical requirements
      1. Installing Visual Studio
      2. Setting up .NET Core
      3. Installing SQL Server
    2. Concurrency in the real world
    3. Multithreading and asynchronous programming
    4. Async/Await – why is blocking bad?
    5. Concurrent collections
    6. Patterns and practices – TDD and Parallel LINQ
    7. Summary
    8. Questions
    9. Further reading
  19. Functional Programming Practices
    1. Technical requirements
      1. Installing Visual Studio
      2. Setting up .NET Core
      3. Installing SQL Server
    2. Understanding functional programming
    3. Enhancing our inventory application
      1. Requirements
      2. Back to FlixOne
    4. Strategy pattern and functional programming
    5. Summary
    6. Questions
  20. Reactive Programming Patterns and Techniques
    1. Technical requirements
      1. Installing Visual Studio
      2. Setting up .NET Core
      3. Installing SQL Server
    2. The principles of reactive programming
      1. Be reactive with reactive programming
        1. Reactive streams in action
    3. Reactive and IObservable
      1. Observer pattern – implementation using IObservable<T>
    4. Reactive extensions – .NET Rx extensions
    5. Inventory application use case
      1. Starting the project
        1. Requirements
          1. Business requirements
      2. Getting inventory with a filter, paging, and sorting
    6. Patterns and Practices – MVVM
      1. Implementation of MVVM
    7. Summary
    8. Questions
    9. Further reading
  21. Advanced Database Design and Application Techniques
    1. Technical requirements
      1. Installing Visual Studio
      2. Setting up .NET Core
      3. Installing SQL Server
    2. Use case discussion
      1. Project kickoff
      2. Requirements
        1. Business requirements
        2. Technical requirements
        3. Challenges
          1. Challenges for developers
          2. Challenges for businesses
          3. Providing a solution to the problems/challenges
    3. Database discussion
    4. Database processing
      1. OLTP
      2. OLAP
      3. Ledger-style databases
    5. Implementing the CQRS pattern
    6. Summary
    7. Questions
  22. Coding for the Cloud
    1. Technical requirements
    2. Key considerations when building solutions in the cloud
    3. Scalability
      1. Workload
      2. Solution patterns
        1. Vertical scaling
        2. Horizontal scaling
        3. Auto-scaling
        4. Microservices
          1. Small
          2. Business capability
          3. Loosely coupled
          4. Independently maintainable
          5. Isolated state
          6. Advantages
    4. Resiliency/availability
      1. Solution pattern
        1. EDA
          1. Queue-Based Load Leveling
          2. Publisher Subscriber
          3. Priority Queue
          4. Compensating transaction
    5. Security
      1. Solution patterns
        1. Federated security
    6. Application design
      1. Solution patterns
        1. Cache
          1. Cache-aside
          2. Write-through cache
          3. Static Content Hosting
        2. Command and Query Responsibility Segregation
          1. Challenges of CQRS
          2. Why CQRS?
    7. DevOps
      1. Solution patterns
        1. Telemetry
        2. Continuous integration/continuous deployment
    8. Summary
    9. Questions
    10. Further reading
  23. Miscellaneous Best Practices
    1. Technical requirements
      1. Installation of Visual Studio
    2. Use case discussion
      1. UML diagram
        1. Types of UML diagram
    3. Best practices
    4. Other design patterns
    5. Summary
    6. Questions
    7. Further reading
  24. Assessments
    1. Chapter 1 – Overview of OOP in .NET Core and C#
    2. Chapter 2 – Modern Software Design Patterns and Principles
    3. Chapter 3 – Implementing Design Patterns – Basics Part 1
    4. Chapter 4 – Implementing Design Patterns – Basics Part 2
    5. Chapter 5 – Implementing Design Patterns – .NET Core
    6. Chapter 6 – Implementing Design Patterns for Web Applications – Part 1
    7. Chapter 7 – Implementing Design Patterns for Web Applications – Part 2
    8. Chapter 8 – Concurrent Programming in .NET Core
    9. Chapter 9 – Functional Programming Practices – an Approach
    10. Chapter 10 – Reactive Programming Patterns and Techniques
    11. Chapter 11 – Advanced Database Design and Application Techniques
    12. Chapter 12 – Coding for the Cloud
    13. Appendix A – Miscellaneous Best Practices
  25. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Hands-On Design Patterns with C# and .NET Core
  • Author(s): Gaurav Aroraa, Jeffrey Chilberto
  • Release date: July 2019
  • Publisher(s): Packt Publishing
  • ISBN: 9781789133646