Adding first tasks from DI
This commit is contained in:
166
DependencyInjection.cs
Normal file
166
DependencyInjection.cs
Normal file
@@ -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<ILogger, ConsoleLogger>();
|
||||
serviceCollection.AddTransient<ILogger, FileLogger>();
|
||||
serviceCollection.AddTransient<UserService>();
|
||||
|
||||
|
||||
// Byt de 2 udkommentering for at skrifte processor
|
||||
|
||||
bool usePaypal = false;
|
||||
|
||||
if (usePaypal)
|
||||
{
|
||||
serviceCollection.AddTransient<IPaymentProcessor, PayPalPaymentProcessor>();
|
||||
}
|
||||
else
|
||||
{
|
||||
serviceCollection.AddTransient<IPaymentProcessor, StripePaymentProcessor>();
|
||||
}
|
||||
|
||||
serviceCollection.AddTransient<CheckoutService>();
|
||||
|
||||
serviceCollection.AddTransient<IEmailSender, SmtpEmailSender>();
|
||||
serviceCollection.AddTransient<IEmailSender, ConsoleEmailSender>();
|
||||
|
||||
serviceCollection.AddTransient<NotificationService>();
|
||||
|
||||
var sp = serviceCollection.BuildServiceProvider();
|
||||
|
||||
sp.GetRequiredService<UserService>().Log();
|
||||
sp.GetRequiredService<CheckoutService>().process();
|
||||
sp.GetRequiredService<NotificationService>().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<ILogger> logger;
|
||||
|
||||
public UserService(IEnumerable<ILogger> 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<IEmailSender> emailSender;
|
||||
|
||||
public NotificationService(IEnumerable<IEmailSender> emailSender)
|
||||
{
|
||||
this.emailSender = emailSender;
|
||||
}
|
||||
|
||||
public void SendNotification(string message)
|
||||
{
|
||||
foreach (var sender in emailSender)
|
||||
{
|
||||
sender.SendEmail("test", "Notification", message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,4 +7,8 @@
|
||||
<Nullable>disable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.8" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -56,4 +56,6 @@ List<IWeapon> weapons = new List<IWeapon>()
|
||||
foreach (var weapon in weapons)
|
||||
{
|
||||
Console.WriteLine(weapon.Attack());
|
||||
}
|
||||
}
|
||||
|
||||
new ImplementDependencyInjections().Implement();
|
||||
Reference in New Issue
Block a user