Loosely coupled code is easier to maintain, extend, and test. Dependency Injection (DI) help us get there. In this workshop, we'll see how interfaces can add "seams" to our code that makes it more flexible and maintainable. From there, we'll dig into loose coupling with Dependency Injection. DI doesn't have to be complicated. With just a few simple changes to our constructors or properties, we can have code that is easy to extend and test.
After laying a good foundation, we'll take a closer look by diving into various DI patterns (such as constructor injection and property injection) as well as other patterns that help us handle interception and optional dependencies. Along the way, we'll see how DI helps us adhere to the SOLID principles in our code. We'll also we'll look at common stumbling blocks like dealing with constructor over-injection, managing static dependencies, and handling disposable dependencies.
Throughout the day, we'll go hands-on with labs to give you a chance to put the concepts into action.
If you're a C# developer who wants to get better with concepts like abstraction, loose coupling, extensibility, and unit testing, then this is the workshop for you.
To get the most out of the workshop, you should have an understanding of the basics of C# and object oriented programming (classes, inheritance, methods, and properties). No prior experience with dependency injection is necessary. Hardware & Software To participate in the hands-on portion, you will need a computer (Windows, macOS, or Linux) with the following installed:
- .NET 6.0 SDK
https://dotnet.microsoft.com/download
The .NET SDK (Software Development Kit) allows you to build .NET applications. - Visual Studio Code
https://code.visualstudio.com/download
This is a great all-around editor. - VS Code C# Extension
https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csharp
This extension to Visual Studio Code adds syntax highlighing and code completion for C#.
As an alternative to Visual Studio Code, you can use Visual Studio 2022 (any edition).
These are the hands-on portions of the workshop. Labs can be completed with Visual Studio Code or Visual Studio 2022. All labs run on Windows, macOS, and Linux. Each lab consists of the following:
-
Labxx-Instructions (Markdown)
A markdown file containing the lab instructions. This includes the scenario, a set of goals, and step-by-step instructions.
This can be viewed in on GitHub or in Visual Studio Code (just click the "Open Preview to the Side" button in the upper right corner). -
Starter (Folder)
This folder contains the starting code for the lab. -
Completed (Folder)
This folder contains the completed solution. If at any time, you get stuck during the lab, you can check this folder for a solution.
The following connects the topics with the sample code.
Constructor Injection
- PeopleController.cs - Web application controller
- PeopleViewerWindow.xaml.cs - Desktop application main window
- PeopleViewModel.cs - Desktop application view model
Object Composition / Composition Root
- Program.cs - Web application program file (no DI container)
- App.xaml.cs - Desktop application main window (no DI container)
- App.xaml.cs - Desktop application main window (using Ninject)
Decorators
- CachingReader.cs - Local cache decorator
Unit Testing
- PeopleViewModelTests.cs - View Model unit tests (constructor injection)
Dependency Injection Containers
- App.xaml.cs - Desktop application main window (using Ninject)
Property Injection
- CSVReader.cs - CSV File data reader
- CSVReaderTests.cs - CSV Reader unit tests (property injection)
Property Injection
- CSVReader.cs - CSV File data reader
Method Injection
- MainWindow.xaml.cs - person.ToString(selectedFormatter)
Read-Only / Guard Clause
- PeopleViewModel.cs - Desktop application view model
- PeopleViewerWindow.xaml.cs - Desktop application main window
Decorators
- CachingReader.cs - Local cache decorator
- ExceptionLoggingReader.cs - Exception logging decorator
- RetryReader.cs - Retry decorator
- Program.cs - Web application composition (with decorators)
- App.xaml.cs - Desktop application composition (with decorators)
Proxy / IDisposable
- SQLReaderProxy.cs - Proxy to wrap IDisposable SQL Reader
Static Dependencies
- ITimeProvider.cs - Wrapper interface
- CurrentTimeProvider.cs - Default that uses "DateTimeOffset.Now"
- ScheduleHelper.cs - Static property that uses current time by default
- ScheduleHelperTests.cs - Tests that swap out time provider for deterministic tests
Configuration Strings
- ServiceReader.cs - Service reader that has 'string' dependency
- ServiceReaderUri.cs - Strongly-typed wrapper around 'string'
- Program.cs - Web application composition (with ServiceReaderUri)
- App.xaml.cs - Desktop application composition (with ServiceReaderUri)
- App.xaml.cs - Desktop application composition using Ninject container
DI Patterns
- Dependency Injection: The Property Injection Pattern
- Property Injection: Simple vs. Safe
- Dependency Injection: The Service Locator Pattern
Decorators and Async Interfaces
- Async Interfaces, Decorators, and .NET Standard
- Async Interfaces
- Adding Retry with the Decorator Pattern
- Unit Testing Async Methods
- Adding Exception Logging with the Decorator Pattern
- Adding a Client-Side Cache with the Decorator Pattern
- The Real Power of Decorators -- Stacking Functionality
Challenges
- Static Objects: Mocking Current Time with a Simple Time Provider
Related Topics
- Session: DI Why? Getting a Grip on Dependency Injection
- Pluralsight: Getting Started with Dependency Injection in .NET
More information at http://www.jeremybytes.com