Skip to content

Commit cb0dc42

Browse files
committed
Update proxy pattern examples
1 parent e1a673f commit cb0dc42

File tree

20 files changed

+68
-95
lines changed

20 files changed

+68
-95
lines changed

src/StructuralPatterns/Proxy/ProxyLibrary/CachingProxyExample/CachingProxyExecutor.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using BuildingBlocks;
2+
using ProxyLibrary.CachingProxyExample.Manager;
23
using ProxyLibrary.CachingProxyExample.Proxy;
34
using ProxyLibrary.CachingProxyExample.ThirdParty;
45

@@ -19,7 +20,7 @@ public static void Execute()
1920
youtubeManager.PlayVideo(2);
2021

2122
// Video 1 should be cached.
22-
// Request to third party service will not be issued.
23+
// Therefore, a request to the third party service shouldn't be issued.
2324
youtubeManager.PlayVideo(1);
2425
}
25-
}
26+
}

src/StructuralPatterns/Proxy/ProxyLibrary/CachingProxyExample/Common/IYoutubeOperations.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
public interface IYoutubeOperations
77
{
88
VideoMetadata GetVideoMetadata(int id);
9-
109
Video DownloadVideo(int id);
11-
1210
IEnumerable<VideoMetadata> ShowHomepage();
13-
}
11+
}

src/StructuralPatterns/Proxy/ProxyLibrary/CachingProxyExample/Common/Video.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@
33
public class Video
44
{
55
public int Id { get; set; }
6-
7-
public string Content { get; set; }
8-
}
6+
public string Content { get; set; } = string.Empty;
7+
}

src/StructuralPatterns/Proxy/ProxyLibrary/CachingProxyExample/Common/VideoMetadata.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
public class VideoMetadata
44
{
55
public int Id { get; set; }
6-
7-
public string Name { get; set; }
8-
9-
public string Description { get; set; }
10-
}
6+
public string Name { get; set; } = string.Empty;
7+
public string Description { get; set; } = string.Empty;
8+
}

src/StructuralPatterns/Proxy/ProxyLibrary/CachingProxyExample/Manager/YoutubeManager.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
using ProxyLibrary.CachingProxyExample.Common;
22

3-
namespace ProxyLibrary.CachingProxyExample;
3+
namespace ProxyLibrary.CachingProxyExample.Manager;
44

55
/// <summary>
66
/// The client class, which used to work directly with a service object,
77
/// stays unchanged as long as it works with the service object through an interface.
8-
/// We can safely pass a proxy object instead of a real service object since
9-
/// they both implement the same interface.
8+
/// We can safely pass a proxy object instead of a real service object since they both implement the same interface.
109
/// </summary>
1110
public class YoutubeManager
1211
{
@@ -31,10 +30,10 @@ public void PlayVideo(int id)
3130
{
3231
Console.WriteLine("\nPlaying video...");
3332

34-
var video = _youtube.DownloadVideo(id);
35-
Console.WriteLine($"Streaming started... Content: {video.Content}");
36-
3733
var metadata = _youtube.GetVideoMetadata(id);
3834
Console.WriteLine($"Metadata loaded...Name: '{metadata.Name}' and description: '{metadata.Description}'");
35+
36+
var video = _youtube.DownloadVideo(id);
37+
Console.WriteLine($"Streaming started... Content: {video.Content}");
3938
}
40-
}
39+
}

src/StructuralPatterns/Proxy/ProxyLibrary/CachingProxyExample/Proxy/CachedYoutube.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public CachedYoutube(Youtube youtubeService)
2626

2727
public Video DownloadVideo(int id)
2828
{
29-
if (!_contentCache.TryGetValue(id, out Video video))
29+
if (!_contentCache.TryGetValue(id, out var video))
3030
{
3131
video = _youtubeService.DownloadVideo(id);
3232
_contentCache.Add(video.Id, video);
@@ -37,7 +37,7 @@ public Video DownloadVideo(int id)
3737

3838
public VideoMetadata GetVideoMetadata(int id)
3939
{
40-
if (!_metadataCache.TryGetValue(id, out VideoMetadata metadata))
40+
if (!_metadataCache.TryGetValue(id, out var metadata))
4141
{
4242
metadata = _youtubeService.GetVideoMetadata(id);
4343
_metadataCache.Add(metadata.Id, metadata);
@@ -48,18 +48,20 @@ public VideoMetadata GetVideoMetadata(int id)
4848

4949
public IEnumerable<VideoMetadata> ShowHomepage()
5050
{
51-
// Naive logic - prepare homepage using only cached data
52-
IEnumerable<VideoMetadata> videosMetadataForHomepage = _metadataCache.Values;
51+
// Naive logic - prepare the homepage using only cached data.
52+
IEnumerable<VideoMetadata> metadataForHomepage = _metadataCache.Values;
5353

54-
if (!videosMetadataForHomepage.Any())
54+
if (!metadataForHomepage.Any())
5555
{
56-
videosMetadataForHomepage = _youtubeService.ShowHomepage();
57-
foreach (var videoMetadata in videosMetadataForHomepage)
56+
// Nothing is cached on our side yet.
57+
// Send a network request to the Youtube in order to get enough information for showing the homepage.
58+
metadataForHomepage = _youtubeService.ShowHomepage();
59+
foreach (var videoMetadata in metadataForHomepage)
5860
{
5961
_metadataCache.Add(videoMetadata.Id, videoMetadata);
6062
}
6163
}
6264

63-
return videosMetadataForHomepage;
65+
return metadataForHomepage;
6466
}
65-
}
67+
}

src/StructuralPatterns/Proxy/ProxyLibrary/CachingProxyExample/ThirdParty/ContentStorage.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ public ContentStorage()
1818

1919
public Video GetById(int id)
2020
{
21-
Console.WriteLine($"Get content for video with ID: {id} from the content storage...");
21+
Console.WriteLine($"Getting content for the video with ID: {id} from the content storage...");
2222

23-
if (!_videos.TryGetValue(id, out Video content))
23+
if (!_videos.TryGetValue(id, out var video))
2424
{
2525
throw new Exception($"ID: {id} is unknown to content storage.");
2626
}
2727

28-
return content;
28+
return video;
2929
}
30-
}
30+
}

src/StructuralPatterns/Proxy/ProxyLibrary/CachingProxyExample/ThirdParty/MetadataStorage.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@ public MetadataStorage()
1818

1919
public IEnumerable<VideoMetadata> GetAll()
2020
{
21-
Console.WriteLine("Get metadata for all videos from the metadata storage...");
21+
Console.WriteLine("Getting metadata for all videos from the metadata storage...");
2222
return _videos.Values;
2323
}
2424

2525
public VideoMetadata GetById(int id)
2626
{
27-
Console.WriteLine($"Get metadata for video with ID: {id} from the metadata storage...");
27+
Console.WriteLine($"Getting metadata for the video with ID: {id} from the metadata storage...");
2828

29-
if (!_videos.TryGetValue(id, out VideoMetadata video))
29+
if (!_videos.TryGetValue(id, out var videoMetadata))
3030
{
3131
throw new Exception($"ID: {id} is unknown to metadata storage.");
3232
}
3333

34-
return video;
34+
return videoMetadata;
3535
}
36-
}
36+
}

src/StructuralPatterns/Proxy/ProxyLibrary/CachingProxyExample/ThirdParty/Youtube.cs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,12 @@ namespace ProxyLibrary.CachingProxyExample.ThirdParty;
1111
/// </summary>
1212
public class Youtube : IYoutubeOperations
1313
{
14-
private readonly ContentStorage _contentStorage = new ContentStorage();
15-
private readonly MetadataStorage _metadataStorage = new MetadataStorage();
14+
private readonly ContentStorage _contentStorage = new();
15+
private readonly MetadataStorage _metadataStorage = new();
1616

17-
public Video DownloadVideo(int id)
18-
{
19-
return _contentStorage.GetById(id);
20-
}
17+
public Video DownloadVideo(int id) => _contentStorage.GetById(id);
2118

22-
public VideoMetadata GetVideoMetadata(int id)
23-
{
24-
return _metadataStorage.GetById(id);
25-
}
19+
public VideoMetadata GetVideoMetadata(int id) => _metadataStorage.GetById(id);
2620

27-
public IEnumerable<VideoMetadata> ShowHomepage()
28-
{
29-
return _metadataStorage.GetAll();
30-
}
31-
}
21+
public IEnumerable<VideoMetadata> ShowHomepage() => _metadataStorage.GetAll();
22+
}

src/StructuralPatterns/Proxy/ProxyLibrary/LoggingProxyExample/Client.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,5 @@
88
/// </summary>
99
public class Client
1010
{
11-
public void PerformOperation(ISubject subject)
12-
{
13-
subject.Request();
14-
}
15-
}
11+
public void PerformOperation(ISubject subject) => subject.Request();
12+
}

0 commit comments

Comments
 (0)