using System.Diagnostics; using System.Reflection.Metadata; using Microsoft.Extensions.DependencyInjection; 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(); bool UseSQL = false; if (UseSQL) { serviceCollection.AddTransient(); } else { serviceCollection.AddSingleton(); } serviceCollection.AddTransient(); serviceCollection.AddTransient(); serviceCollection.AddTransient(); serviceCollection.AddTransient(); var sp = serviceCollection.BuildServiceProvider(); sp.GetRequiredService().Log(); sp.GetRequiredService().process(); sp.GetRequiredService().SendNotification("Hello, There"); sp.GetRequiredService().SeedProducts(); sp.GetRequiredService().DisplayAllProducts(); sp.GetRequiredService().Start(); } } 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); } } } public interface IProductRepository { List GetAllProducts(); void AddProduct(string name); } public class InMemoryProductReposity : IProductRepository { List products = new List(); public void AddProduct(string name) { products.Add(name); } public List GetAllProducts() { return products; } } public class SqlProductRepository : IProductRepository { public void AddProduct(string name) { Console.WriteLine($"Wrote {name} to SQL Database"); } public List GetAllProducts() { Console.WriteLine("Got all products from SQL Database"); return new List(); } } public class ProductService { private readonly IProductRepository productRepository; public ProductService(IProductRepository productRepository) { this.productRepository = productRepository; } public void SeedProducts() { productRepository.AddProduct("Skovl"); productRepository.AddProduct("Spade"); productRepository.AddProduct("Spand"); productRepository.AddProduct("Teslacoil"); } public void DisplayAllProducts() { foreach (var prod in productRepository.GetAllProducts()) { Console.WriteLine($"Got: {prod}"); } } } public interface IGameEngine { void Play(); } public class GuessNumberGame : IGameEngine { public void Play() { int rightNumber = new Random().Next(1, 11); Console.WriteLine("Guess number between 1 and 10"); var readline = Console.ReadLine(); if (int.TryParse(readline, out var number)) { if (number.Equals(rightNumber)) { Console.WriteLine("You won."); } else { Console.WriteLine("WRONG"); } } else { Console.WriteLine("Is that even a number?"); } } } public class RockPaperScissorsGame : IGameEngine { public void Play() { var posibilities = new List() { "scissor", "paper", "rock", }; Console.WriteLine("Choose between"); for (int i = 0; i < posibilities.Count; i++) { Console.WriteLine($"{i}: {posibilities[i]}"); } var readline = Console.ReadLine(); if (int.TryParse(readline, out var number)) { if (number > posibilities.Count) { Console.WriteLine("Must choose inside the range"); } var computerPlay = new Random().Next(0, 3); Console.WriteLine($"Computer choose: {posibilities[computerPlay]}"); if (computerPlay > number || (computerPlay == 0 && number == 2)) { Console.WriteLine("Computer won..."); } else { Console.WriteLine("You won"); } } else { Console.WriteLine("Is that even a number?"); } } } public class GameManager { private readonly IEnumerable gameEngines; public GameManager(IEnumerable gameEngines) { this.gameEngines = gameEngines; } public void Start() { Console.WriteLine("Choose game"); for (int i = 0; i < gameEngines.Count(); i++) { Console.WriteLine(i + ": " + gameEngines.ToList()[i].GetType().Name); } var readline = Console.ReadLine(); if (int.TryParse(readline, out var number)) { if (number > gameEngines.Count()) { Console.WriteLine("Must be in range"); } else { gameEngines.ToList()[number].Play(); } } } } }