Skip to content

Commit

Permalink
Add Builder.
Browse files Browse the repository at this point in the history
  • Loading branch information
eminencegrs committed Feb 13, 2024
1 parent bd287bf commit 07c1f12
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 0 deletions.
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 Creational/DesignPatterns.Builder.UnitTests/EmailBuilderTests.cs
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 Creational/DesignPatterns.Builder/DesignPatterns.Builder.csproj
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>
41 changes: 41 additions & 0 deletions Creational/DesignPatterns.Builder/EmailBuilder.cs
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;
}
}
11 changes: 11 additions & 0 deletions Creational/DesignPatterns.Builder/EmailDirector.cs
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();
}
}
25 changes: 25 additions & 0 deletions Creational/DesignPatterns.Builder/EmailMessage.cs
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)}");
}
}
11 changes: 11 additions & 0 deletions Creational/DesignPatterns.Builder/IEmailBuilder.cs
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();
}
18 changes: 18 additions & 0 deletions Creational/DesignPatterns.Builder/Program.cs
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();
14 changes: 14 additions & 0 deletions DesignPatterns.sln
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DesignPatterns.FactoryMetho
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DesignPatterns.Decorator.UnitTests", "Structural\DesignPatterns.Decorator.UnitTests\DesignPatterns.Decorator.UnitTests.csproj", "{3942358E-6D3A-441B-ABD7-7FCB0DF988BD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DesignPatterns.Builder", "Creational\DesignPatterns.Builder\DesignPatterns.Builder.csproj", "{727C96E6-5B0E-43BE-BB4E-2D06F40E51BF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DesignPatterns.Builder.UnitTests", "Creational\DesignPatterns.Builder.UnitTests\DesignPatterns.Builder.UnitTests.csproj", "{774BAD81-C8DA-4491-B53C-F023A8235AA7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -72,6 +76,14 @@ Global
{3942358E-6D3A-441B-ABD7-7FCB0DF988BD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3942358E-6D3A-441B-ABD7-7FCB0DF988BD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3942358E-6D3A-441B-ABD7-7FCB0DF988BD}.Release|Any CPU.Build.0 = Release|Any CPU
{727C96E6-5B0E-43BE-BB4E-2D06F40E51BF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{727C96E6-5B0E-43BE-BB4E-2D06F40E51BF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{727C96E6-5B0E-43BE-BB4E-2D06F40E51BF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{727C96E6-5B0E-43BE-BB4E-2D06F40E51BF}.Release|Any CPU.Build.0 = Release|Any CPU
{774BAD81-C8DA-4491-B53C-F023A8235AA7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{774BAD81-C8DA-4491-B53C-F023A8235AA7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{774BAD81-C8DA-4491-B53C-F023A8235AA7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{774BAD81-C8DA-4491-B53C-F023A8235AA7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{DCE06EB6-BAB5-4573-AF67-128DBCCDB90C} = {7FF0E83F-EBEB-4103-BCD1-2F100E82FCD1}
Expand All @@ -84,5 +96,7 @@ Global
{DAE417C1-F239-4514-9689-6466F0B52A99} = {AE005ED4-5F0B-4AF9-81AE-AAE3AD6F8901}
{15D20D32-A0CA-4D4B-9E6F-11A8B8AED366} = {AE005ED4-5F0B-4AF9-81AE-AAE3AD6F8901}
{3942358E-6D3A-441B-ABD7-7FCB0DF988BD} = {5961ADCC-5FA6-4076-A9F4-C1D3207ABE1A}
{727C96E6-5B0E-43BE-BB4E-2D06F40E51BF} = {AE005ED4-5F0B-4AF9-81AE-AAE3AD6F8901}
{774BAD81-C8DA-4491-B53C-F023A8235AA7} = {AE005ED4-5F0B-4AF9-81AE-AAE3AD6F8901}
EndGlobalSection
EndGlobal

0 comments on commit 07c1f12

Please sign in to comment.