Skip to content

Commit 7ae7dce

Browse files
committed
Add fluent interface design pattern
1 parent 922252b commit 7ae7dce

File tree

14 files changed

+232
-3
lines changed

14 files changed

+232
-3
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<h1 align="center">Design Patterns Library</h1>
22
<p align="center">
3-
32 Design Patterns • 65 moderately realistic examples
3+
33 Design Patterns • 73 moderately realistic examples
44
</p>
55

66
## What are Design Patterns?
@@ -57,6 +57,7 @@ This repository contains a comprehensive design patterns library implemented in
5757
| Design Pattern | Type | Description |
5858
| ------------- |:-------------:| -----|
5959
| [Event Aggregator](https://github.com/nemanjarogic/DesignPatternsLibrary/tree/main/src/AdditionalPatterns/EventAggregator/StoreManagement) | Behavioral | Channel events from multiple objects into a single object to simplify registration for clients.|
60+
| [Fluent Interface](https://github.com/nemanjarogic/DesignPatternsLibrary/tree/main/src/AdditionalPatterns/FluentInterface/FluentInterfaceLibrary) | Creational | Provides an easy-readable, flowing interface, that often mimics a domain specific language. Using this pattern results in code that can be read nearly as human language.|
6061
| [Interpreter](https://github.com/nemanjarogic/DesignPatternsLibrary/tree/main/src/AdditionalPatterns/Interpreter/InterpreterLibrary) | Behavioral | Defines a grammatical representation for a language and provides an interpreter to evaluate sentences in a language.|
6162
| [Lazy Load](https://github.com/nemanjarogic/DesignPatternsLibrary/tree/main/src/AdditionalPatterns/LazyLoad/LazyLoadLibrary) | Data Access | Defers initialization of an object until the point at which it is needed. It can contribute to efficiency in the program's operation if properly and appropriately used.|
6263
| [Null Object](https://github.com/nemanjarogic/DesignPatternsLibrary/tree/main/src/AdditionalPatterns/NullObject/NullObjectLibrary) | Behavioral | Encapsulates the absence of an object by providing a substitutable alternative that offers suitable default do nothing behavior.|

assets/images/console-menu.png

448 Bytes
Loading
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using BuildingBlocks;
2+
3+
namespace FluentInterfaceLibrary.BlobStorageExample;
4+
5+
public static class BlobStorageExecutor
6+
{
7+
public static void Execute()
8+
{
9+
ConsoleExtension.WriteSeparator("Blob Storage example");
10+
11+
BlobStorageManager
12+
.Connect("DefaultEndpointsProtocol=https;AccountName=myAccountName;AccountKey=<account-key>")
13+
.OnBlob("container", "blob")
14+
.Download("blobStorageManual.pdf")
15+
.ToFolder(@"D:\DesignPatternsLibrary\Downloads");
16+
}
17+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using FluentInterfaceLibrary.BlobStorageExample.Contracts;
2+
3+
namespace FluentInterfaceLibrary.BlobStorageExample;
4+
5+
public sealed class BlobStorageManager : IBlobStorageSelector, IBlobStorageAction, IRead, IWrite
6+
{
7+
private readonly string _connectionString;
8+
private string _containerName;
9+
private string _blobName;
10+
11+
private BlobStorageManager(string connectionString)
12+
{
13+
_connectionString = connectionString;
14+
_containerName = string.Empty;
15+
_blobName = string.Empty;
16+
}
17+
18+
#region Blob storage connection
19+
20+
public static IBlobStorageSelector Connect(string connectionString)
21+
{
22+
Console.WriteLine($"Connecting to the storage account using the connection string: {connectionString}");
23+
var connection = new BlobStorageManager(connectionString);
24+
Console.WriteLine("Connection with the storage account is successfully established.");
25+
26+
return connection;
27+
}
28+
29+
public IBlobStorageAction OnBlob(string containerName, string blobName)
30+
{
31+
_containerName = containerName;
32+
_blobName = blobName;
33+
34+
Console.WriteLine($"The blob storage /{containerName}/{blobName} is ready for incoming requests.");
35+
return this;
36+
}
37+
38+
#endregion Blob storage connection
39+
40+
#region Download
41+
42+
public IWrite Download(string fileName)
43+
{
44+
Console.WriteLine($"The file {fileName} will be download from the /{_containerName}/{_blobName}");
45+
return this;
46+
}
47+
48+
public void ToFolder(string folderPath) =>
49+
Console.WriteLine($"The file is downloaded from the /{_containerName}/{_blobName} to the directory {folderPath}.");
50+
51+
#endregion Download
52+
53+
#region Upload
54+
55+
public IRead Upload(string fileName)
56+
{
57+
Console.WriteLine($"The file {fileName} will be uploaded to the /{_containerName}/{_blobName}");
58+
return this;
59+
}
60+
61+
public void FromFile(string filePath) =>
62+
Console.WriteLine($"The file is uploaded from the user's machine to the /{_containerName}/{_blobName}.");
63+
64+
public void FromStream(Stream stream) =>
65+
Console.WriteLine($"The file is uploaded from the stream to the /{_containerName}/{_blobName}.");
66+
67+
#endregion Upload
68+
69+
#region Preview
70+
71+
public void Preview(string fileName)
72+
{
73+
Console.WriteLine($"Previewing the file {fileName} from the /{_containerName}/{_blobName}...");
74+
}
75+
76+
#endregion Preview
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace FluentInterfaceLibrary.BlobStorageExample.Contracts;
2+
3+
// Interfaces IWrite and IRead are used as a prevention mechanism for invalid method combinations.
4+
public interface IBlobStorageAction
5+
{
6+
IWrite Download(string fileName);
7+
IRead Upload(string fileName);
8+
void Preview(string fileName);
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace FluentInterfaceLibrary.BlobStorageExample.Contracts;
2+
3+
public interface IBlobStorageSelector
4+
{
5+
IBlobStorageAction OnBlob(string containerName, string blobName);
6+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace FluentInterfaceLibrary.BlobStorageExample.Contracts;
2+
3+
public interface IRead
4+
{
5+
void FromFile(string filePath);
6+
void FromStream(Stream stream);
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace FluentInterfaceLibrary.BlobStorageExample.Contracts;
2+
3+
public interface IWrite
4+
{
5+
void ToFolder(string folderPath);
6+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using BuildingBlocks;
2+
using FluentInterfaceLibrary.BlobStorageExample;
3+
using FluentInterfaceLibrary.LinqExample;
4+
5+
namespace FluentInterfaceLibrary;
6+
7+
public class Executor : PatternExecutor
8+
{
9+
public override string Name => "Fluent Interface - Creational Pattern";
10+
11+
public override void Execute()
12+
{
13+
BlobStorageExecutor.Execute();
14+
LinqExecutor.Execute();
15+
}
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<ProjectReference Include="..\..\..\BuildingBlocks\BuildingBlocks.csproj" />
11+
</ItemGroup>
12+
13+
</Project>

0 commit comments

Comments
 (0)