Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Examples/Examples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "RealExample", "RealExample"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Payment", "..\Payment\Payment.csproj", "{E7F0B3A7-6105-43CB-B4E1-3733111D4EB8}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ReportingSystem", "ReportingSystem", "{7F5E5CAF-808E-4B01-9020-FFFB3E91E830}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Reporting", "Reporting\Reporting.csproj", "{E3DE53A2-4252-4A90-91B8-2F4881F0169A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -29,12 +33,17 @@ Global
{E7F0B3A7-6105-43CB-B4E1-3733111D4EB8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E7F0B3A7-6105-43CB-B4E1-3733111D4EB8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E7F0B3A7-6105-43CB-B4E1-3733111D4EB8}.Release|Any CPU.Build.0 = Release|Any CPU
{E3DE53A2-4252-4A90-91B8-2F4881F0169A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E3DE53A2-4252-4A90-91B8-2F4881F0169A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E3DE53A2-4252-4A90-91B8-2F4881F0169A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E3DE53A2-4252-4A90-91B8-2F4881F0169A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{E7F0B3A7-6105-43CB-B4E1-3733111D4EB8} = {BEFC8EDE-A568-4AC3-B3EA-B68427103370}
{E3DE53A2-4252-4A90-91B8-2F4881F0169A} = {7F5E5CAF-808E-4B01-9020-FFFB3E91E830}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {22B9C537-2B7B-4D6D-B152-E2C4BA31F193}
Expand Down
166 changes: 166 additions & 0 deletions Examples/Reporting/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
using Microsoft.Extensions.DependencyInjection;
using System.ComponentModel;
using System.Reflection;

public class Program
{
public static void Main()
{
// DI
var serviceProvider = ConfigureServices();
var connectors = serviceProvider.GetRequiredService<Connectors>();

Console.Write("Enter Title: ");
var title = Console.ReadLine();

Console.Write("Enter Description: ");
var description = Console.ReadLine();

Console.Write("Enter Severity(Dangerous, Medium, LowImportance): ");
var severityInput = Console.ReadLine();
var severity = Enum.TryParse(severityInput, out Severity parsedSeverity) ? parsedSeverity : Severity.LowImportance;

var report = new BugReport
{
Title = title,
Description = description,
Severity = severity
};

report.Validate();

Console.Write("Enter Connector (Discord, Jira, Slack): ");
var connectorType = Console.ReadLine();

connectors.Create(connectorType);
}

// Config DI
private static ServiceProvider ConfigureServices()
{
return new ServiceCollection()
.AddTransient<IConnectorsFactory, DiscordFactory>()
.AddTransient<IConnectorsFactory, SlackFactory>()
.AddTransient<IConnectorsFactory, JiraFactory>()
.AddTransient<Connectors>()
.BuildServiceProvider();
}
}

public class Connectors
{
private readonly IEnumerable<IConnectorsFactory> _factories;

public Connectors(IEnumerable<IConnectorsFactory> factories)
{
_factories = factories;
}

public IConnectors Create(string connectorType)
{
var factory = _factories.FirstOrDefault(f => f.GetType().Name.ToLower().Contains(connectorType));
return factory?.CreateInstance();
}
}

public class BugReport
{
public string Title { get; init; } = default!;
public string Description { get; init; } = default!;
public Severity Severity { get; init; }
public DateTime CreationDate { get;} = DateTime.UtcNow;

public void Validate()
{
if (string.IsNullOrWhiteSpace(Title))
throw new ArgumentException("Title cannot be null, empty, or whitespace.", nameof(Title));

if (string.IsNullOrWhiteSpace(Description))
throw new ArgumentException("Description cannot be null, empty, or whitespace.", nameof(Description));
}

public string Format()
{
var severityLabel = Severity.GetDescription();
return $"[{severityLabel}] {Title}: {Description} (Created On: {CreationDate:yyyy-MM-dd HH:mm:ss})";
}
}

public enum Severity
{
[Description("High")]
Dangerous = 0,
[Description("Medium")]
Medium = 1,
[Description("Low")]
LowImportance = 2
}

public static class EnumExtensions
{
public static string GetDescription(this Enum value)
{
var field = value.GetType().GetField(value.ToString());
var attribute = field!.GetCustomAttribute<DescriptionAttribute>();
return attribute == null ? value.ToString() : attribute.Description;
}
}

public interface IConnectors
{
void SendBugReport(BugReport report);
}

public class Jira : IConnectors
{
public void SendBugReport(BugReport report)
{
Console.WriteLine($"[Jira] Bug Report Sent: {report.Format()}");
}
}

public class Slack : IConnectors
{
public void SendBugReport(BugReport report)
{
Console.WriteLine($"[Slack] Bug Report Sent: {report.Format()}");
}
}

public class Discord : IConnectors
{
public void SendBugReport(BugReport report)
{
Console.WriteLine($"[Discord] Bug Report Sent: {report.Format()}");
}
}

public interface IConnectorsFactory
{
IConnectors CreateInstance();
}

public class JiraFactory : IConnectorsFactory
{
public IConnectors CreateInstance()
{
return new Jira();
}
}

public class SlackFactory : IConnectorsFactory
{
public IConnectors CreateInstance()
{
return new Slack();
}
}

public class DiscordFactory : IConnectorsFactory
{
public IConnectors CreateInstance()
{
return new Discord();
}
}

15 changes: 15 additions & 0 deletions Examples/Reporting/Reporting.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="9.0.0" />
</ItemGroup>

</Project>
5 changes: 5 additions & 0 deletions Payment/Payment.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="9.0.0" />
</ItemGroup>

</Project>
Loading