-
Notifications
You must be signed in to change notification settings - Fork 0
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
bd287bf
commit 07c1f12
Showing
9 changed files
with
209 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
Creational/DesignPatterns.Builder.UnitTests/DesignPatterns.Builder.UnitTests.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,36 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="AutoFixture" Version="4.18.1" /> | ||
<PackageReference Include="FluentAssertions" Version="6.12.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /> | ||
<PackageReference Include="Moq" Version="4.20.70" /> | ||
<PackageReference Include="Shouldly" Version="4.2.1" /> | ||
<PackageReference Include="xunit" Version="2.6.5" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector" Version="6.0.0"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.msbuild" Version="6.0.0"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\DesignPatterns.Builder\DesignPatterns.Builder.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
43 changes: 43 additions & 0 deletions
43
Creational/DesignPatterns.Builder.UnitTests/EmailBuilderTests.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,43 @@ | ||
using FluentAssertions.Execution; | ||
using Shouldly; | ||
using Xunit; | ||
|
||
namespace DesignPatterns.Builder.UnitTests; | ||
|
||
public class EmailBuilderTests | ||
{ | ||
[Fact] | ||
public void Given_WhenConstructEmail_ThenResultAsExpected() | ||
{ | ||
const string sender = "[email protected]"; | ||
const string recipient = "[email protected]"; | ||
const string subject = "Test Email"; | ||
const string body = "This is a test email body."; | ||
var attachments = new [] { "file.txt", "image.jpg" }; | ||
|
||
var emailDirector = new EmailDirector(); | ||
|
||
var email = emailDirector.Construct(builder => | ||
{ | ||
builder | ||
.SetSender(sender) | ||
.SetRecipient(recipient) | ||
.SetSubject(subject) | ||
.SetBody(body); | ||
|
||
foreach(var attachment in attachments) | ||
{ | ||
builder.AddAttachment(attachment); | ||
} | ||
}); | ||
|
||
using (new AssertionScope()) | ||
{ | ||
email.Sender.ShouldBe(sender); | ||
email.Recipient.ShouldBe(recipient); | ||
email.Subject.ShouldBe(subject); | ||
email.Body.ShouldBe(body); | ||
email.Attachments.ShouldBe(attachments); | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
Creational/DesignPatterns.Builder/DesignPatterns.Builder.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,10 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |
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,41 @@ | ||
namespace DesignPatterns.Builder; | ||
|
||
public class EmailBuilder : IEmailBuilder | ||
{ | ||
private readonly EmailMessage emailMessage = new(); | ||
|
||
public IEmailBuilder SetSender(string sender) | ||
{ | ||
this.emailMessage.Sender = sender; | ||
return this; | ||
} | ||
|
||
public IEmailBuilder SetRecipient(string recipient) | ||
{ | ||
this.emailMessage.Recipient = recipient; | ||
return this; | ||
} | ||
|
||
public IEmailBuilder SetSubject(string subject) | ||
{ | ||
this.emailMessage.Subject = subject; | ||
return this; | ||
} | ||
|
||
public IEmailBuilder SetBody(string body) | ||
{ | ||
this.emailMessage.Body = body; | ||
return this; | ||
} | ||
|
||
public IEmailBuilder AddAttachment(string attachment) | ||
{ | ||
this.emailMessage.Attachments.Add(attachment); | ||
return this; | ||
} | ||
|
||
public EmailMessage Build() | ||
{ | ||
return this.emailMessage; | ||
} | ||
} |
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,11 @@ | ||
namespace DesignPatterns.Builder; | ||
|
||
public class EmailDirector | ||
{ | ||
public EmailMessage Construct(Action<IEmailBuilder> builderAction) | ||
{ | ||
var builder = new EmailBuilder(); | ||
builderAction(builder); | ||
return builder.Build(); | ||
} | ||
} |
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,25 @@ | ||
namespace DesignPatterns.Builder; | ||
|
||
public class EmailMessage | ||
{ | ||
public EmailMessage() | ||
{ | ||
this.Attachments = new List<string>(); | ||
} | ||
|
||
public string Sender { get; set; } | ||
public string Recipient { get; set; } | ||
public string Subject { get; set; } | ||
public string Body { get; set; } | ||
public List<string> Attachments { get; set; } | ||
|
||
public void Send() | ||
{ | ||
Console.WriteLine("Email sent:"); | ||
Console.WriteLine($"From: {this.Sender}"); | ||
Console.WriteLine($"To: {this.Recipient}"); | ||
Console.WriteLine($"Subject: {this.Subject}"); | ||
Console.WriteLine($"Body: {this.Body}"); | ||
Console.WriteLine($"Attachments: {string.Join(", ", this.Attachments)}"); | ||
} | ||
} |
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,11 @@ | ||
namespace DesignPatterns.Builder; | ||
|
||
public interface IEmailBuilder | ||
{ | ||
IEmailBuilder SetSender(string sender); | ||
IEmailBuilder SetRecipient(string recipient); | ||
IEmailBuilder SetSubject(string subject); | ||
IEmailBuilder SetBody(string body); | ||
IEmailBuilder AddAttachment(string attachment); | ||
EmailMessage Build(); | ||
} |
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,18 @@ | ||
using DesignPatterns.Builder; | ||
|
||
var emailDirector = new EmailDirector(); | ||
|
||
var email = emailDirector.Construct(builder => | ||
{ | ||
builder | ||
.SetSender("[email protected]") | ||
.SetRecipient("[email protected]") | ||
.SetSubject("Test Email") | ||
.SetBody("This is a test email body.") | ||
.AddAttachment("file.txt") | ||
.AddAttachment("image.jpg"); | ||
}); | ||
|
||
email.Send(); | ||
|
||
Console.WriteLine(); |
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