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
<p>Build the menu by getting all the <code>ICommand</code> implementations through constructor DI. When a menu entry is picked, it will execute that command by calling the <code>Execute()</code> function.</p>
18
+
<p>Build the menu by getting all the <code>ICommand</code> implementations through constructor DI.
19
+
When a menu entry is picked, it will execute that command by calling the <code>Execute()</code> function.
20
+
The <code>MovieConsoleApplication</code> constructor can import all <code>IConsole</code> implementations as an <code>IEnumerable<IConsole></code>:</p>
21
+
<pre><code> public MovieConsoleApplication(IEnumerable<ICommand> commands)
22
+
{
23
+
}
24
+
</code></pre>
19
25
<h4id="create-an-abstraction-for-the-console-iconsole">2. Create an abstraction for the console: <code>IConsole</code></h4>
20
26
<oltype="a">
21
-
<li><p>Why would we abstract the existent static Console class through IConsole?</p>
27
+
<li><p>Register in the DI Container the <code>IConsole</code> with the <code>AppConsole</code> implementation and refactor the code to use the <code>IConsole</code></p>
22
28
</li>
23
-
<li><p>Register in the DIC the <code>IConsole</code> with the <code>AppConsoleImplementation</code> and refactor the code to use the <code>IConsole</code></p>
29
+
<li><p>What benefits can you see?</p>
24
30
</li>
25
31
</ol>
26
32
<h4id="integrate-the-servicelocator-into-the-app">3. Integrate the <code>ServiceLocator</code> into the app</h4>
@@ -34,34 +40,60 @@ <h4 id="rewrite-the-moviesconsolecreator">5. Rewrite the <code>MoviesConsoleCrea
34
40
<li>Reimplement the <code>MoviesConsoleCreator</code> command to support the following functionality:</li>
35
41
</ol>
36
42
<ul>
37
-
<li>read more properties of a movie one by one. They UI will present the user the name of the property that she should enter</li>
38
-
<li>The Movie should have at least the following fields: <code>Title :string</code> and <code>Rating :int</code></li>
43
+
<li>read more properties of a movie one by one. They UI will show to the user the name of the property that she should enter</li>
44
+
<li>The Movie should have at least the following fields: <code>Title :string</code> and <code>Rating :int</code> (see <code>Movie.cs</code>)</li>
39
45
<li>when all properties of a movie were read, ask the user if she wants to add a new movie</li>
<li>Write unit tests that prove that the new <code>MoviesConsoleCreator</code> works</li>
62
68
</ol>
63
69
<h4id="implement-ientityreader-and-ientityfieldsreader">6. Implement <code>IEntityReader</code> and <code>IEntityFieldsReader</code></h4>
64
-
<p><em>Suggestion</em>: Use <em>Service Locator Pattern</em> to get and return the <code>IEntityFieldsReader<T></code> implementations on the <code>IEntityReader<T>()</code> function.</p>
70
+
<pre><code>class EntityReader : IEntityReader
71
+
{
72
+
public IEntityFieldsReader<TEntity> BeginEntityRead<TEntity>()
73
+
{
74
+
...
75
+
}
76
+
}
77
+
78
+
class MovieFieldsReader : IEntityFieldsReader<Movie>
79
+
{
80
+
public IEnumerable<string> GetFields()
81
+
{
82
+
...
83
+
}
84
+
85
+
public void StoreFieldValue(string name, string value)
86
+
{
87
+
...
88
+
}
89
+
90
+
public Movie GetEntity()
91
+
{
92
+
...
93
+
}
94
+
}
95
+
</code></pre>
96
+
<p><em>Suggestion</em>: Use <em>Service Locator Pattern</em> to get and return the <code>IEntityFieldsReader<TEntity></code> implementations on the <code>IEntityReader<TEntity>()</code> function.</p>
65
97
<h4id="make-a-generic-implementation-of-ientityfieldsreadert">7. Make a generic implementation of <code>IEntityFieldsReader<T></code></h4>
66
98
<p>We may have a generic class that implements <code>IEntityFieldsReader<T></code> for any T by using reflection. It should be registered to DI by using the open generic type.</p>
Copy file name to clipboardExpand all lines: LessonsSamples/LessonsSamples/Lesson6/DI-Demo/_Exercises.md
+48-12Lines changed: 48 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,13 +12,21 @@ public interface ICommand
12
12
}
13
13
```
14
14
15
-
Build the menu by getting all the `ICommand` implementations through constructor DI. When a menu entry is picked, it will execute that command by calling the `Execute()` function.
15
+
Build the menu by getting all the `ICommand` implementations through constructor DI.
16
+
When a menu entry is picked, it will execute that command by calling the `Execute()` function.
17
+
The `MovieConsoleApplication` constructor can import all `IConsole` implementations as an `IEnumerable<IConsole>`:
18
+
19
+
```
20
+
public MovieConsoleApplication(IEnumerable<ICommand> commands)
21
+
{
22
+
}
23
+
```
16
24
17
25
#### 2. Create an abstraction for the console: `IConsole`
18
26
19
-
a) Why would we abstract the existent static Console class through IConsole?
27
+
a) Register in the DI Container the `IConsole` with the `AppConsole` implementation and refactor the code to use the `IConsole`
20
28
21
-
b) Register in the DIC the `IConsole` with the `AppConsoleImplementation` and refactor the code to use the `IConsole`
29
+
b) What benefits can you see?
22
30
23
31
#### 3. Integrate the `ServiceLocator` into the app
24
32
@@ -30,29 +38,29 @@ b) Register in the DIC the `IConsole` with the `AppConsoleImplementation` and re
30
38
#### 5. Rewrite the `MoviesConsoleCreator`
31
39
32
40
a) Reimplement the `MoviesConsoleCreator` command to support the following functionality:
33
-
- read more properties of a movie one by one. They UI will present the user the name of the property that she should enter
34
-
- The Movie should have at least the following fields: `Title :string` and `Rating :int`
41
+
- read more properties of a movie one by one. They UI will show to the user the name of the property that she should enter
42
+
- The Movie should have at least the following fields: `Title :string` and `Rating :int` (see `Movie.cs`)
35
43
- when all properties of a movie were read, ask the user if she wants to add a new movie
@@ -61,7 +69,35 @@ b) Write unit tests that prove that the new `MoviesConsoleCreator` works
61
69
62
70
#### 6. Implement `IEntityReader` and `IEntityFieldsReader`
63
71
64
-
*Suggestion*: Use *Service Locator Pattern* to get and return the `IEntityFieldsReader<T>` implementations on the `IEntityReader<T>()` function.
72
+
```
73
+
class EntityReader : IEntityReader
74
+
{
75
+
public IEntityFieldsReader<TEntity> BeginEntityRead<TEntity>()
76
+
{
77
+
...
78
+
}
79
+
}
80
+
81
+
class MovieFieldsReader : IEntityFieldsReader<Movie>
82
+
{
83
+
public IEnumerable<string> GetFields()
84
+
{
85
+
...
86
+
}
87
+
88
+
public void StoreFieldValue(string name, string value)
89
+
{
90
+
...
91
+
}
92
+
93
+
public Movie GetEntity()
94
+
{
95
+
...
96
+
}
97
+
}
98
+
```
99
+
100
+
*Suggestion*: Use *Service Locator Pattern* to get and return the `IEntityFieldsReader<TEntity>` implementations on the `IEntityReader<TEntity>()` function.
65
101
66
102
#### 7. Make a generic implementation of `IEntityFieldsReader<T>`
0 commit comments