Skip to content

Commit ac18ead

Browse files
authored
Merge pull request #49998 from wwlpublish/4333daa99459f1d6e5fef3951daa7f652695e0a6b2a70ab58d437cf33e2425d2-live
Modules/M03-design-resilient-code-interfaces
2 parents 8ce99c4 + 9a0b9f9 commit ac18ead

11 files changed

+90
-31
lines changed

learn-pr/wwl-azure/design-resilient-code-interfaces/1-introduction.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Introduction
44
metadata:
55
title: Introduction
66
description: "Introduction to designing resilient code with interfaces in C#."
7-
ms.date: 02/17/2025
7+
ms.date: 04/15/2025
88
author: wwlpublish
99
ms.author: eric
1010
ms.topic: unit

learn-pr/wwl-azure/design-resilient-code-interfaces/2-examine-interface-designs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Examine interface designs
44
metadata:
55
title: Examine interface designs
66
description: "Examine interface designs and SOLID design principles."
7-
ms.date: 02/17/2025
7+
ms.date: 04/15/2025
88
author: wwlpublish
99
ms.author: eric
1010
ms.topic: unit

learn-pr/wwl-azure/design-resilient-code-interfaces/3-implement-explicit-interface-members.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Implement explicit interface members in a class
44
metadata:
55
title: Implement explicit interface members in a class
66
description: "Implement explicit interface members in a class using C#."
7-
ms.date: 02/17/2025
7+
ms.date: 04/15/2025
88
author: wwlpublish
99
ms.author: eric
1010
ms.topic: unit

learn-pr/wwl-azure/design-resilient-code-interfaces/4-use-combine-multiple-interfaces.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Use and combine multiple interfaces
44
metadata:
55
title: Use and combine multiple interfaces
66
description: "Use and combine multiple interfaces in your C# classes."
7-
ms.date: 02/17/2025
7+
ms.date: 04/15/2025
88
author: wwlpublish
99
ms.author: eric
1010
ms.topic: unit

learn-pr/wwl-azure/design-resilient-code-interfaces/5-understand-tightly-coupled-code.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Understand tightly coupled code
44
metadata:
55
title: Understand tightly coupled code
66
description: "Discover how tightly coupled code impacts the maintainability applications."
7-
ms.date: 02/17/2025
7+
ms.date: 04/15/2025
88
author: wwlpublish
99
ms.author: eric
1010
ms.topic: unit

learn-pr/wwl-azure/design-resilient-code-interfaces/6-refactor-use-interfaces.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Refactor using interfaces
44
metadata:
55
title: Refactor using interfaces
66
description: "Use interfaces in refactoring to reduce dependencies."
7-
ms.date: 02/17/2025
7+
ms.date: 04/15/2025
88
author: wwlpublish
99
ms.author: eric
1010
ms.topic: unit

learn-pr/wwl-azure/design-resilient-code-interfaces/7-exercise-decouple-code-with-interfaces.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: 'Exercise: Decouple code with interfaces'
44
metadata:
55
title: 'Exercise: Decouple code with interfaces'
66
description: "Exercise: Decouple code with interfaces in C# code."
7-
ms.date: 02/17/2025
7+
ms.date: 04/15/2025
88
author: wwlpublish
99
ms.author: eric
1010
ms.topic: unit

learn-pr/wwl-azure/design-resilient-code-interfaces/8-knowledge-check.yml

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Module assessment
44
metadata:
55
title: Module assessment
66
description: "Knowledge check"
7-
ms.date: 02/17/2025
7+
ms.date: 04/15/2025
88
author: wwlpublish
99
ms.author: eric
1010
ms.topic: unit
@@ -16,36 +16,47 @@ content: |
1616
quiz:
1717
title: "Check your knowledge"
1818
questions:
19-
- content: "Which solution allows access for explicit interface members in C#?"
20-
choices:
21-
- content: "By casting the class instance to the interface type."
22-
isCorrect: true
23-
explanation: "Correct. Explicit interface members are only accessible through the interface instance, so you need to cast the class instance to the interface type."
24-
- content: "By using the `this` keyword within the class."
25-
isCorrect: false
26-
explanation: "Incorrect. Explicit interface members can't be accessed using the `this` keyword within the class; they're only accessible through the interface instance."
27-
- content: "By using reflection to invoke the members."
28-
isCorrect: false
29-
explanation: "Incorrect. While reflection can be used to invoke members, it is not the standard way to access explicit interface members. The correct approach is to cast the class instance to the interface type."
3019
- content: "What is a key benefit of using interfaces to reduce code dependencies?"
3120
choices:
32-
- content: "Interfaces promote loose coupling and easier testing."
33-
isCorrect: true
34-
explanation: "Correct. Interfaces define a set of methods and properties that implementing classes must provide, promoting loose coupling and making the code easier to test and maintain."
3521
- content: "Interfaces provide default implementations for all methods."
3622
isCorrect: false
3723
explanation: "Incorrect. Interfaces do not provide default implementations for methods; they only define the contract that implementing classes must follow."
3824
- content: "Interfaces allow for multiple inheritance of classes."
3925
isCorrect: false
4026
explanation: "Incorrect. C# does not support multiple inheritance of classes. Interfaces allow a class to inherit behavior from multiple sources without multiple inheritance."
41-
- content: "Imagine the scenario of developing a home automation application with different types of lights. Which approach allows mix and match capabilities in a single class?"
27+
- content: "Interfaces promote loose coupling and easier testing."
28+
isCorrect: true
29+
explanation: "Correct. Interfaces define a set of methods and properties that implementing classes must provide, promoting loose coupling and making the code easier to test and maintain."
30+
- content: "How can the issue of tightly coupled code be addressed?"
31+
choices:
32+
- content: "By modifying existing code to add new features."
33+
isCorrect: false
34+
explanation: "Incorrect. Modifying existing code to add new features violates the Open/Closed Principle."
35+
- content: "By refactoring the code using interfaces to make it more modular and flexible."
36+
isCorrect: true
37+
explanation: "Correct. Using interfaces to decouple components can make the code more modular and flexible, addressing the issue of tightly coupled code."
38+
- content: "By increasing dependencies between components."
39+
isCorrect: false
40+
explanation: "Incorrect. Increasing dependencies would lead to more tightly coupled code, not less."
41+
- content: "What is the main advantage of using an interface in programming?"
42+
choices:
43+
- content: "It allows you to modify the class without adding new type items."
44+
isCorrect: false
45+
explanation: "Incorrect. An interface does not allow modification of the class, but it does allow adding new type items without modifying the class."
46+
- content: "It makes the system more complex and difficult to understand."
47+
isCorrect: false
48+
explanation: "Incorrect. An interface actually simplifies maintenance by making the system easier to understand, test, and extend."
49+
- content: "It isolates behavior, ensuring the class doesn't depend on specific implementations."
50+
isCorrect: true
51+
explanation: "Correct. An interface sets a contract for behavior without detailing its implementation, thus reducing dependencies and improving modularity."
52+
- content: "How does an interface contribute to the principle of Separation of Concerns?"
4253
choices:
43-
- content: "Implement multiple interfaces in the class."
54+
- content: "By isolating behavior and ensuring responsibilities are clearly divided."
4455
isCorrect: true
45-
explanation: "Correct. Implementing multiple interfaces in a class allows mix and match capabilities, providing a common API supported by various types."
46-
- content: "Use multiple inheritance of classes."
56+
explanation: "Correct. An interface isolates behavior, which aligns with the principle of Separation of Concerns by ensuring responsibilities are clearly divided."
57+
- content: "By complicating the system's understanding, testing, and extension."
4758
isCorrect: false
48-
explanation: "Incorrect. C# does not support multiple inheritance of classes. Implementing multiple interfaces is the correct approach."
49-
- content: "Define all functionalities within a single interface."
59+
explanation: "Incorrect. An interface simplifies maintenance by clearly dividing responsibilities."
60+
- content: "By allowing the class to depend on specific implementations."
5061
isCorrect: false
51-
explanation: "Incorrect. Defining all functionalities within a single interface can lead to a bloated interface. Implementing multiple interfaces is a better approach."
62+
explanation: "Incorrect. An interface actually ensures that the class doesn't depend on specific implementations."

learn-pr/wwl-azure/design-resilient-code-interfaces/9-summary.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Summary
44
metadata:
55
title: Summary
66
description: "Summary."
7-
ms.date: 02/17/2025
7+
ms.date: 04/15/2025
88
author: wwlpublish
99
ms.author: eric
1010
ms.topic: unit

learn-pr/wwl-azure/design-resilient-code-interfaces/includes/6-refactor-use-interfaces.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,54 @@ public class Library
134134

135135
Now, the `Library` class can work with any object that implements the `IBorrowable` interface, making it more flexible and easier to extend.
136136

137+
## Using Dependency Injection
138+
139+
Imagine you’re setting up a home entertainment system. Instead of permanently attaching a specific brand of speakers to your stereo, you use speaker "jacks" that can accept multiple types of compatible speaker plugs. This design allows you to easily replace or upgrade the speakers without having to change the entire stereo system.
140+
141+
In software, **Dependency Injection** works similarly. It allows a class to depend on an abstract interface rather than a specific implementation. Dependency Injection makes the system more flexible and easier to maintain because you can "plug in" different implementations without modifying the class itself.
142+
143+
The **constructor** in programming is like the technician connecting the speaker jacks during the setup of your stereo system. For the `Library` class, the constructor (`public Library(IBorrowable item)`) is where the dependency is provided. The constructor allows the `Library` class to work with any compatible implementation, such as `BorrowableBook` or `BorrowableDVD`, without needing to change its internal structure. Just as the speaker jack enables flexibility in choosing different speakers, the constructor facilitates flexibility in using various borrowable items.
144+
145+
Here’s how it works in code:
146+
147+
```csharp
148+
public class Library
149+
{
150+
private IBorrowable _item;
151+
152+
public Library(IBorrowable item) // Dependency is injected here
153+
{
154+
_item = item;
155+
}
156+
157+
public void BorrowItem()
158+
{
159+
if (_item.IsAvailable)
160+
{
161+
_item.Borrow();
162+
}
163+
else
164+
{
165+
Console.WriteLine("The item is not available.");
166+
}
167+
}
168+
}
169+
```
170+
171+
In this example:
172+
173+
- The **constructor** is where the "connection" (the `IBorrowable` dependency) is made, similar to how the speaker plug connects to the "jack."
174+
- The `IBorrowable` interface acts like the "stereo jack," defining the standard connection point.
175+
- The `BorrowableBook` and `BorrowableDVD` are like different types of stereo speakers with unique connectors that get connected to the "jack."
176+
177+
By using Dependency Injection, the `Library` class can work with any implementation of `IBorrowable`. This approach provides:
178+
179+
- **Flexibility**: You can easily switch or add new implementations without modifying the `Library` class.
180+
- **Testability**: You can "plug in" mock implementations for testing purposes.
181+
- **Maintainability**: The `Library` class doesn’t need to know the details of the specific implementation, making it easier to extend and maintain.
182+
183+
This design ensures that the `Library` class is no longer tightly coupled to specific implementations, making the system more modular and adaptable.
184+
137185
## Adding new borrowable items
138186

139187
With the interface in place, we can easily add new types of borrowable items without modifying the `Library` class. For example, here’s a `BorrowableDVD` class:

0 commit comments

Comments
 (0)