Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
IT NachrichtenRechnungshof: EU nicht genug gegen Cyberangriffe gewappnet(22.09.2026 um 07:42 Uhr)
Android Tipps & SecuritySamsung lässt Besitzer der Galaxy-S26-Smartphones weiter zappeln(22.09.2026 um 07:57 Uhr)
IT NachrichtenRechnungshof: EU nicht genug gegen Cyberangriffe gewappnet(22.09.2026 um 07:42 Uhr)
Android Tipps & SecuritySamsung lässt Besitzer der Galaxy-S26-Smartphones weiter zappeln(22.09.2026 um 07:57 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Dependency Injection In .NET Core With Strategy Pattern

In the previous post, we gave an introduction and explained the basic concept of the Strategy Pattern. Now, we want to go a bit further and demonstrate how it works in practice alongside a dependency injection in .NET Core. Instead of…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!

In the previous post, we gave an introduction and explained the basic concept of the Strategy Pattern. Now, we want to go a bit further and demonstrate how it works in practice alongside a dependency injection in .NET Core. Instead of manually instantiating strategies, we let the .NET Core DI container inject the correct implementation at runtime.






Example



For the showing purposes, let us asume that we are implementing some payment service, and we want to use one service at the time, depending on user input.



For our strategy pattern, we would need following components:



Interface




public interface IPaymentStrategy
{
void Pay(decimal amount);
}






Concrete Strategies




public class CreditCardPayment : IPaymentStrategy
{
public void Pay(decimal amount)
{
Console.WriteLine($"Paid ${amount} using Credit Card.");
}
}

public class PayPalPayment : IPaymentStrategy
{
public void Pay(decimal amount)
{
Console.WriteLine($"Paid ${amount} using PayPal.");
}
}

public class BitcoinPayment : IPaymentStrategy
{
public void Pay(decimal amount)
{
Console.WriteLine($"Paid ${amount} using Bitcoin.");
}
}







Context




public class PaymentContext
{
private readonly IPaymentStrategy _paymentStrategy;

public PaymentContext(IPaymentStrategy paymentStrategy)
{
_paymentStrategy = paymentStrategy;
}

public void ExecutePayment(decimal amount)
{
_paymentStrategy.Pay(amount);
}
}







Next, we are implementing our controller:




using Microsoft.AspNetCore.Mvc;

[Route("api/[controller]")]
[ApiController]
public class PaymentController : ControllerBase
{
private readonly IServiceProvider _serviceProvider;

public PaymentController(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}

[HttpPost("{paymentMethod}")]
public IActionResult ProcessPayment(string paymentMethod, [FromBody] decimal amount)
{
IPaymentStrategy paymentStrategy = paymentMethod.ToLower() switch
{
"creditcard" => _serviceProvider.GetService<CreditCardPayment>(),
"paypal" => _serviceProvider.GetService<PayPalPayment>(),
"bitcoin" => _serviceProvider.GetService<BitcoinPayment>(),
_ => throw new ArgumentException("Invalid payment method")
};

var paymentContext = new PaymentContext(paymentStrategy);
paymentContext.ExecutePayment(amount);

return Ok($"Payment of ${amount} processed using {paymentMethod}.");
}
}







And, we register each payment strategy in Program.cs.




using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddTransient<CreditCardPayment>();
builder.Services.AddTransient<PayPalPayment>();
builder.Services.AddTransient<BitcoinPayment>();

builder.Services.AddControllers();

var app = builder.Build();

app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();

app.Run();










How It Works




  • The DI container registers each strategy (CreditCardPayment, PayPalPayment, BitcoinPayment).

  • The controller receives an IServiceProvider, which allows it to dynamically resolve the appropriate payment strategy.

  • When a user makes a payment request, the API dynamically selects the strategy based on the URL parameter (creditcard, paypal, bitcoin).

  • The selected strategy is injected into the PaymentContext and executed.






Conclusion



Finally, we can highlight some key points on why this approach offers significant benefits to the code structure.





  • Loose Coupling – The controller does not depend on concrete implementations, making the code more flexible.


  • Easy Maintenance – New payment methods can be added without modifying existing code.


  • Testability – The strategies can be mocked and tested independently.


  • Runtime Flexibility – The payment method can be selected dynamically at runtime.

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Dependency Injection In .NET Core With Strategy Pattern

Thematisch verwandte Begriffe: Dependency, Injection, Core, With · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-61647 | NotebookLM MCP is an MCP server and HTTP service for interacting with Go…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick