You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: learn-pr/wwl-azure/design-resilient-code-interfaces/8-knowledge-check.yml
+33-22Lines changed: 33 additions & 22 deletions
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ title: Module assessment
4
4
metadata:
5
5
title: Module assessment
6
6
description: "Knowledge check"
7
-
ms.date: 02/17/2025
7
+
ms.date: 04/15/2025
8
8
author: wwlpublish
9
9
ms.author: eric
10
10
ms.topic: unit
@@ -16,36 +16,47 @@ content: |
16
16
quiz:
17
17
title: "Check your knowledge"
18
18
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."
30
19
- content: "What is a key benefit of using interfaces to reduce code dependencies?"
31
20
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."
35
21
- content: "Interfaces provide default implementations for all methods."
36
22
isCorrect: false
37
23
explanation: "Incorrect. Interfaces do not provide default implementations for methods; they only define the contract that implementing classes must follow."
38
24
- content: "Interfaces allow for multiple inheritance of classes."
39
25
isCorrect: false
40
26
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?"
42
53
choices:
43
-
- content: "Implement multiple interfaces in the class."
54
+
- content: "By isolating behavior and ensuring responsibilities are clearly divided."
44
55
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."
47
58
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."
50
61
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."
Copy file name to clipboardExpand all lines: learn-pr/wwl-azure/design-resilient-code-interfaces/includes/6-refactor-use-interfaces.md
+48Lines changed: 48 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -134,6 +134,54 @@ public class Library
134
134
135
135
Now, the `Library` class can work with any object that implements the `IBorrowable` interface, making it more flexible and easier to extend.
136
136
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
+
publicclassLibrary
149
+
{
150
+
privateIBorrowable_item;
151
+
152
+
publicLibrary(IBorrowableitem) // Dependency is injected here
153
+
{
154
+
_item=item;
155
+
}
156
+
157
+
publicvoidBorrowItem()
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
+
137
185
## Adding new borrowable items
138
186
139
187
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