-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
922252b
commit 7ae7dce
Showing
14 changed files
with
232 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions
17
...Patterns/FluentInterface/FluentInterfaceLibrary/BlobStorageExample/BlobStorageExecutor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using BuildingBlocks; | ||
|
||
namespace FluentInterfaceLibrary.BlobStorageExample; | ||
|
||
public static class BlobStorageExecutor | ||
{ | ||
public static void Execute() | ||
{ | ||
ConsoleExtension.WriteSeparator("Blob Storage example"); | ||
|
||
BlobStorageManager | ||
.Connect("DefaultEndpointsProtocol=https;AccountName=myAccountName;AccountKey=<account-key>") | ||
.OnBlob("container", "blob") | ||
.Download("blobStorageManual.pdf") | ||
.ToFolder(@"D:\DesignPatternsLibrary\Downloads"); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
...lPatterns/FluentInterface/FluentInterfaceLibrary/BlobStorageExample/BlobStorageManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using FluentInterfaceLibrary.BlobStorageExample.Contracts; | ||
|
||
namespace FluentInterfaceLibrary.BlobStorageExample; | ||
|
||
public sealed class BlobStorageManager : IBlobStorageSelector, IBlobStorageAction, IRead, IWrite | ||
{ | ||
private readonly string _connectionString; | ||
private string _containerName; | ||
private string _blobName; | ||
|
||
private BlobStorageManager(string connectionString) | ||
{ | ||
_connectionString = connectionString; | ||
_containerName = string.Empty; | ||
_blobName = string.Empty; | ||
} | ||
|
||
#region Blob storage connection | ||
|
||
public static IBlobStorageSelector Connect(string connectionString) | ||
{ | ||
Console.WriteLine($"Connecting to the storage account using the connection string: {connectionString}"); | ||
var connection = new BlobStorageManager(connectionString); | ||
Console.WriteLine("Connection with the storage account is successfully established."); | ||
|
||
return connection; | ||
} | ||
|
||
public IBlobStorageAction OnBlob(string containerName, string blobName) | ||
{ | ||
_containerName = containerName; | ||
_blobName = blobName; | ||
|
||
Console.WriteLine($"The blob storage /{containerName}/{blobName} is ready for incoming requests."); | ||
return this; | ||
} | ||
|
||
#endregion Blob storage connection | ||
|
||
#region Download | ||
|
||
public IWrite Download(string fileName) | ||
{ | ||
Console.WriteLine($"The file {fileName} will be download from the /{_containerName}/{_blobName}"); | ||
return this; | ||
} | ||
|
||
public void ToFolder(string folderPath) => | ||
Console.WriteLine($"The file is downloaded from the /{_containerName}/{_blobName} to the directory {folderPath}."); | ||
|
||
#endregion Download | ||
|
||
#region Upload | ||
|
||
public IRead Upload(string fileName) | ||
{ | ||
Console.WriteLine($"The file {fileName} will be uploaded to the /{_containerName}/{_blobName}"); | ||
return this; | ||
} | ||
|
||
public void FromFile(string filePath) => | ||
Console.WriteLine($"The file is uploaded from the user's machine to the /{_containerName}/{_blobName}."); | ||
|
||
public void FromStream(Stream stream) => | ||
Console.WriteLine($"The file is uploaded from the stream to the /{_containerName}/{_blobName}."); | ||
|
||
#endregion Upload | ||
|
||
#region Preview | ||
|
||
public void Preview(string fileName) | ||
{ | ||
Console.WriteLine($"Previewing the file {fileName} from the /{_containerName}/{_blobName}..."); | ||
} | ||
|
||
#endregion Preview | ||
} |
9 changes: 9 additions & 0 deletions
9
...FluentInterface/FluentInterfaceLibrary/BlobStorageExample/Contracts/IBlobStorageAction.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace FluentInterfaceLibrary.BlobStorageExample.Contracts; | ||
|
||
// Interfaces IWrite and IRead are used as a prevention mechanism for invalid method combinations. | ||
public interface IBlobStorageAction | ||
{ | ||
IWrite Download(string fileName); | ||
IRead Upload(string fileName); | ||
void Preview(string fileName); | ||
} |
6 changes: 6 additions & 0 deletions
6
...uentInterface/FluentInterfaceLibrary/BlobStorageExample/Contracts/IBlobStorageSelector.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace FluentInterfaceLibrary.BlobStorageExample.Contracts; | ||
|
||
public interface IBlobStorageSelector | ||
{ | ||
IBlobStorageAction OnBlob(string containerName, string blobName); | ||
} |
7 changes: 7 additions & 0 deletions
7
...onalPatterns/FluentInterface/FluentInterfaceLibrary/BlobStorageExample/Contracts/IRead.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace FluentInterfaceLibrary.BlobStorageExample.Contracts; | ||
|
||
public interface IRead | ||
{ | ||
void FromFile(string filePath); | ||
void FromStream(Stream stream); | ||
} |
6 changes: 6 additions & 0 deletions
6
...nalPatterns/FluentInterface/FluentInterfaceLibrary/BlobStorageExample/Contracts/IWrite.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace FluentInterfaceLibrary.BlobStorageExample.Contracts; | ||
|
||
public interface IWrite | ||
{ | ||
void ToFolder(string folderPath); | ||
} |
16 changes: 16 additions & 0 deletions
16
src/AdditionalPatterns/FluentInterface/FluentInterfaceLibrary/Executor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using BuildingBlocks; | ||
using FluentInterfaceLibrary.BlobStorageExample; | ||
using FluentInterfaceLibrary.LinqExample; | ||
|
||
namespace FluentInterfaceLibrary; | ||
|
||
public class Executor : PatternExecutor | ||
{ | ||
public override string Name => "Fluent Interface - Creational Pattern"; | ||
|
||
public override void Execute() | ||
{ | ||
BlobStorageExecutor.Execute(); | ||
LinqExecutor.Execute(); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/AdditionalPatterns/FluentInterface/FluentInterfaceLibrary/FluentInterfaceLibrary.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\BuildingBlocks\BuildingBlocks.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
65 changes: 65 additions & 0 deletions
65
src/AdditionalPatterns/FluentInterface/FluentInterfaceLibrary/LinqExample/LinqExecutor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using BuildingBlocks; | ||
|
||
namespace FluentInterfaceLibrary.LinqExample; | ||
|
||
public static class LinqExecutor | ||
{ | ||
public static void Execute() | ||
{ | ||
ConsoleExtension.WriteSeparator("LINQ example"); | ||
|
||
var englishToSerbianDictionary = new Dictionary<string, string> | ||
{ | ||
{"adventure", "avantura"}, | ||
{"bird", "ptica"}, | ||
{"fish", "riba"}, | ||
{"football", "fudbal"}, | ||
{"programming", "programiranje"}, | ||
}; | ||
|
||
DisplayDictionary(englishToSerbianDictionary); | ||
|
||
Console.WriteLine("\nFinding translations for English words containing the letter 'a', sorted by length and displayed in uppercase..."); | ||
FindTranslationsProgressively(englishToSerbianDictionary); | ||
FindTranslationsUsingFluentInterface(englishToSerbianDictionary); | ||
} | ||
|
||
private static void DisplayDictionary(Dictionary<string, string> englishToSerbianDictionary) | ||
{ | ||
Console.WriteLine("\nContent of the dictionary: "); | ||
foreach (var (englishWord, serbianWord) in englishToSerbianDictionary) | ||
{ | ||
Console.WriteLine($"{englishWord} - {serbianWord}"); | ||
} | ||
} | ||
|
||
private static void FindTranslationsProgressively(Dictionary<string, string> englishToSerbianDictionary) | ||
{ | ||
var filtered = englishToSerbianDictionary.Where(t => t.Key.Contains("a")); | ||
var sorted = filtered.OrderBy(t => t.Value.Length); | ||
var finalTranslations = sorted.Select(t => t.Value.ToUpper()); | ||
|
||
Console.WriteLine("\nProgressive translations: "); | ||
DisplayWords(finalTranslations); | ||
} | ||
|
||
private static void FindTranslationsUsingFluentInterface(Dictionary<string, string> englishToSerbianDictionary) | ||
{ | ||
// C# uses fluent programming extensively in LINQ to build queries using "standard query operators". | ||
var finalTranslations = englishToSerbianDictionary | ||
.Where(t => t.Key.Contains("a")) | ||
.OrderBy(t => t.Value.Length) | ||
.Select(t => t.Value.ToUpper()); | ||
|
||
Console.WriteLine("\nFluent interface translations: "); | ||
DisplayWords(finalTranslations); | ||
} | ||
|
||
private static void DisplayWords(IEnumerable<string> words) | ||
{ | ||
foreach (var word in words) | ||
{ | ||
Console.WriteLine(word); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters