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: srp/README.md
+22-3Lines changed: 22 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,7 +12,7 @@ Another way of putting this would be to say each class should do one thing and d
12
12
13
13
## Reasons for Change
14
14
15
-
What is the reason for the [FileStore class](./src/before/fileStore.ts) to change (Answer is below!):
15
+
What is the reason for the [FileStore class](./src/before/FileStore.ts) to change (Answer is below!):
16
16
17
17
The answers are:
18
18
@@ -25,11 +25,30 @@ The answers are:
25
25
26
26
What we do is take each one of the reasons for change listed above and extract into a separate class.
27
27
28
-
So firstly, let's extract all _logging logic_ into a new class called [StoreLogger](./src/after/storeLogger.ts) like so. Note that it is domain specific for our Store and not just a generic Logger.
28
+
So firstly, let's extract all _logging logic_ into a new class called [StoreLogger](./src/after/StoreLogger.ts) like so. Note that it is domain specific for our Store and not just a generic Logger.
29
29
30
30
What we have done is extracted all the various log calls to a new class. Now this means that if we want to change the logging framework that we use - perhaps because the open source project that built the logger framework becomes depreciated or we find a better solution well now we can change in just this one place.
31
31
32
-
To use this new logger, we create an instance of it in the [message store class](./src/after/messageStore.ts) - perviously called FileStore (I'll explain why the name was changed below).
32
+
To use this new logger, we create an instance of it in the [message store class](./src/after/MessageStore.ts) - perviously called FileStore (I'll explain why the name was changed below).
33
+
34
+
Now we replace the calls to `console.xyz(...)` to `this.log.xyz(...)` for example:
35
+
36
+
* Replace: `console.log(“some message: ”, id)`
37
+
* With: `this.log.Saving(id)`
38
+
39
+
The next thing we need to extract is the logic for caching which can be applied in pretty much the same way. We create a new class [StoreCache](./src/after/StoreCache.ts).
40
+
41
+
Then we need to create an instance of this class in our MessageStore class and call that instead through the implementation.
42
+
43
+
```
44
+
this.cache = new StoreCache(); // in the MessageStore constructor
45
+
---
46
+
this.cache.AddOrUpdate(id, message); // in the Save method
47
+
```
48
+
49
+
Next reason to change that we can address is the way that we apply storage. This will allow us to change where we save files - perhaps to a relational database instead of a filestore- who knows! So what we do is create a separate class called FileSore that is just for reading / writing files to the filestore and use that in our MessageStore class in the same way as before.
50
+
51
+
So what are we left with? A better implementation for this class that is now split into other classes each with a Single Responsibility!
0 commit comments