From 410d615641993db0917030cfef0b0ed145c0c59f Mon Sep 17 00:00:00 2001 From: smallbenji Date: Wed, 20 Aug 2025 09:09:32 +0200 Subject: [PATCH] Adding first tasks from DI --- DependencyInjection.cs | 166 +++++++++++++++++++++++++++++++++++++ DependencyInjection.csproj | 4 + Program.cs | 4 +- 3 files changed, 173 insertions(+), 1 deletion(-) create mode 100644 DependencyInjection.cs diff --git a/DependencyInjection.cs b/DependencyInjection.cs new file mode 100644 index 0000000..2895c2d --- /dev/null +++ b/DependencyInjection.cs @@ -0,0 +1,166 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; + +namespace DependencyInjections +{ + public class ImplementDependencyInjections + { + public void Implement() + { + ServiceCollection serviceCollection = new ServiceCollection(); + + serviceCollection.AddTransient(); + serviceCollection.AddTransient(); + serviceCollection.AddTransient(); + + + // Byt de 2 udkommentering for at skrifte processor + + bool usePaypal = false; + + if (usePaypal) + { + serviceCollection.AddTransient(); + } + else + { + serviceCollection.AddTransient(); + } + + serviceCollection.AddTransient(); + + serviceCollection.AddTransient(); + serviceCollection.AddTransient(); + + serviceCollection.AddTransient(); + + var sp = serviceCollection.BuildServiceProvider(); + + sp.GetRequiredService().Log(); + sp.GetRequiredService().process(); + sp.GetRequiredService().SendNotification("Hello, There"); + + } + } + + public interface ILogger + { + void Log(string message); + } + + public class ConsoleLogger : ILogger + { + public void Log(string message) + { + Console.WriteLine(message); + } + } + + public class FileLogger : ILogger + { + public void Log(string message) + { + var currentPath = Directory.GetCurrentDirectory(); + + var path = Path.Combine(currentPath, "test.txt"); + + using (StreamWriter sw = File.AppendText(path)) + { + sw.WriteLine(message); + } + } + } + + public class UserService + { + private readonly IEnumerable logger; + + public UserService(IEnumerable logger) + { + this.logger = logger; + } + + public void Log() + { + foreach (var log in logger) + { + log.Log("Hello from user service"); + } + } + } + + public interface IPaymentProcessor + { + void Process(decimal amount); + } + + public class StripePaymentProcessor : IPaymentProcessor + { + public void Process(decimal amount) + { + Console.WriteLine($"Stripe processing for {amount}..."); + } + } + + public class PayPalPaymentProcessor : IPaymentProcessor + { + public void Process(decimal amount) + { + Console.WriteLine($"Paypal processing for {amount}..."); + } + } + + public class CheckoutService + { + private readonly IPaymentProcessor paymentProcessor; + + public CheckoutService(IPaymentProcessor paymentProcessor) + { + this.paymentProcessor = paymentProcessor; + } + + public void process() + { + paymentProcessor.Process(5); + } + } + + public interface IEmailSender + { + void SendEmail(string to, string subject, string body); + } + + public class ConsoleEmailSender : IEmailSender + { + public void SendEmail(string to, string subject, string body) + { + Console.WriteLine($"To: {to} subject: {subject} body: {body}"); + } + } + + public class SmtpEmailSender : IEmailSender + { + public void SendEmail(string to, string subject, string body) + { + Console.WriteLine($"SENDING EMAIL:: To: {to} subject: {subject} body: {body}"); + } + } + + public class NotificationService + { + private readonly IEnumerable emailSender; + + public NotificationService(IEnumerable emailSender) + { + this.emailSender = emailSender; + } + + public void SendNotification(string message) + { + foreach (var sender in emailSender) + { + sender.SendEmail("test", "Notification", message); + } + } + } +} \ No newline at end of file diff --git a/DependencyInjection.csproj b/DependencyInjection.csproj index 45ea52c..12f8585 100644 --- a/DependencyInjection.csproj +++ b/DependencyInjection.csproj @@ -7,4 +7,8 @@ disable + + + + diff --git a/Program.cs b/Program.cs index 8fe7625..cfad612 100644 --- a/Program.cs +++ b/Program.cs @@ -56,4 +56,6 @@ List weapons = new List() foreach (var weapon in weapons) { Console.WriteLine(weapon.Attack()); -} \ No newline at end of file +} + +new ImplementDependencyInjections().Implement(); \ No newline at end of file