Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Windows Tipps & SecurityDarum bekommen Sie mit der DEAN bald eine zweite Kontonummer(24.09.2026 um 08:20 Uhr)
Sichere ProgrammierungTimescaleDB Course – PostgreSQL for Time-Series Data(24.09.2026 um 04:04 Uhr)
Sichere ProgrammierungAI agents can't tell who's giving the orders. So I built a tiny gate.(24.09.2026 um 08:22 Uhr)
Sichere ProgrammierungCan a Developer Ship a Frontend With No DNS or Centralized Backend?(24.09.2026 um 08:26 Uhr)
Sichere ProgrammierungD33:它猜錯方向,卻把價位框對了(24.09.2026 um 08:33 Uhr)
Sichere ProgrammierungMigrating Off Opsgenie Before Sunset: A Regen Walkthrough(24.09.2026 um 08:33 Uhr)
Windows Tipps & SecurityDarum bekommen Sie mit der DEAN bald eine zweite Kontonummer(24.09.2026 um 08:20 Uhr)
Sichere ProgrammierungTimescaleDB Course – PostgreSQL for Time-Series Data(24.09.2026 um 04:04 Uhr)
Sichere ProgrammierungAI agents can't tell who's giving the orders. So I built a tiny gate.(24.09.2026 um 08:22 Uhr)
Sichere ProgrammierungCan a Developer Ship a Frontend With No DNS or Centralized Backend?(24.09.2026 um 08:26 Uhr)
Sichere ProgrammierungD33:它猜錯方向,卻把價位框對了(24.09.2026 um 08:33 Uhr)
Sichere ProgrammierungMigrating Off Opsgenie Before Sunset: A Regen Walkthrough(24.09.2026 um 08:33 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Δυναμική Εφαρμογή Επιχειρησιακών Κανόνων σε C# με JSON και Func

Περιγραφή: Σε αυτό το άρθρο παρουσιάζουμε πώς να υλοποιήσετε μια καθαρή και ευέλικτη αρχιτεκτονική για την εκτέλεση επιχειρησιακών κανόνων σε μια εφαρμογή C#. Οι κανόνες φορτώνονται δυναμικά από ένα αρχείο JSON και αξιολογούνται χρησιμοπο…

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




Περιγραφή:



Σε αυτό το άρθρο παρουσιάζουμε πώς να υλοποιήσετε μια καθαρή και ευέλικτη αρχιτεκτονική για την εκτέλεση επιχειρησιακών κανόνων σε μια εφαρμογή C#. Οι κανόνες φορτώνονται δυναμικά από ένα αρχείο JSON και αξιολογούνται χρησιμοποιώντας το Func, αποφεύγοντας έτσι μεγάλα μπλοκ if/else. Η μέθοδος αυτή επιτρέπει εύκολη συντήρηση, προσθήκη εκατοντάδων κανόνων και γρήγορη προσαρμογή της λογικής χωρίς αλλαγές στον κώδικα.



H υλοποίηση του Rule Engine ακολουθεί τις SOLID principles






1 Δημιουργία των Interfaces




public interface IRuleLoader
{
List<RuleDefinition> LoadRules(string path);
}

public interface IRuleEngine
{
decimal CalculateDiscount(Order order, List<RuleDefinition> rules);
}






Αυτό ικανοποιεί ISP και DIP: η υψηλού επιπέδου λογική δεν εξαρτάται από συγκεκριμένες υλοποιήσεις.






2 Models




public class Customer
{
public bool IsVIP { get; set; }
}

public class Order
{
public decimal Total { get; set; }
public Customer Customer { get; set; }
}

public class RuleDefinition
{
public string Name { get; set; } = string.Empty;
public string Condition { get; set; } = string.Empty; // π.χ. "Total > 1000 && Customer.IsVIP"
public decimal Discount { get; set; }
}









3 Loader με JSON




using System.Text.Json;

public class JsonRuleLoader : IRuleLoader
{
public List<RuleDefinition> LoadRules(string path)
{
if (!File.Exists(path))
{
Console.WriteLine($"Το αρχείο κανόνων δεν βρέθηκε: {path}");
return new List<RuleDefinition>();
}

var json = File.ReadAllText(path);
if (string.IsNullOrWhiteSpace(json))
{
Console.WriteLine($"Το αρχείο κανόνων είναι κενό: {path}");
return new List<RuleDefinition>();
}

var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
};

var rules = JsonSerializer.Deserialize<List<RuleDefinition>>(json, options);
return rules ?? new List<RuleDefinition>();
}
}






Τηρεί SRP: μόνο για φόρτωση κανόνων.






4 Rule Engine με Dynamic Expresso



Install-Package DynamicExpresso.Core




using DynamicExpresso;

public class DynamicRuleEngine : IRuleEngine
{
public decimal CalculateDiscount(Order order, List<RuleDefinition> rules)
{
var interpreter = new Interpreter();
interpreter.SetVariable("Total", order.Total);
interpreter.SetVariable("Customer", order.Customer);

foreach (var rule in rules)
{
try
{
// Εκτέλεση συνθήκης από JSON δυναμικά
bool isMatch = interpreter.Eval<bool>(rule.Condition);
if (isMatch)
{
Console.WriteLine($"Ταιριάξε ο κανόνας: {rule.Name}");
return order.Total * rule.Discount;
}
}
catch (Exception ex)
{
Console.WriteLine($"Σφάλμα στην εκτέλεση κανόνα '{rule.Name}': {ex.Message}");
}
}

return 0;
}
}






✅ Τηρεί OCP: νέες συνθήκες προστίθενται στο JSON χωρίς αλλαγή κώδικα.

✅ Τηρεί DIP: το Program δουλεύει με IRuleEngine.

✅ Τηρεί SRP: η κλάση ασχολείται μόνο με εκτέλεση κανόνων.







5 Παράδειγμα JSON (rules.json)




[
{ "Name": "HighValueOrder", "Condition": "Total > 1000", "Discount": 0.10 },
{ "Name": "VIPCustomer", "Condition": "Customer.IsVIP == true", "Discount": 0.20 },
{ "Name": "SmallOrder", "Condition": "Total < 100", "Discount": 0.05 }
]






6 Χρήση στην Main




class Program
{
static void Main()
{
IRuleLoader loader = new JsonRuleLoader();
IRuleEngine engine = new DynamicRuleEngine();

var rules = loader.LoadRules("rules.json");
if (rules.Count == 0)
{
Console.WriteLine("Δεν φορτώθηκαν κανόνες. Τερματισμός.");
return;
}

var order = new Order
{
Total = 1200,
Customer = new Customer { IsVIP = true }
};

var discount = engine.CalculateDiscount(order, rules);
Console.WriteLine($"Έκπτωση που εφαρμόστηκε: {discount:C}");
}
}






Output



✅ Ταιριάξε ο κανόνας: HighValueOrder

💰 Έκπτωση που εφαρμόστηκε: 120,00 €



🔹 Τι κερδίζουμε με αυτή τη SOLID έκδοση




  1. SRP: Κάθε κλάση έχει μια ευθύνη

  2. OCP: Νέες συνθήκες προστίθενται στο JSON χωρίς να αλλάζει ο κώδικας

  3. LSP: Μπορούμε να αντικαταστήσουμε τον IRuleEngine με άλλη υλοποίηση

  4. ISP: Τα interfaces είναι μικρά και συγκεκριμένα

  5. DIP: Το υψηλού επιπέδου module (Program) εξαρτάται από abstraction (IRuleEngine, IRuleLoader)





Παρακάτω θα δούμε και ένα βήμα παραπέρα με δυο παραδείγματα cashing:



🔹 Caching των κανόνων (ώστε να μην ξαναφορτώνονται από JSON κάθε φορά)

🔹 Caching των compiled expressions (ώστε να μην ξανα-ερμηνεύονται / ξαναμεταγλωττίζονται κάθε φορά που εκτελείς έναν κανόνα)



🧩 1️⃣ Caching των κανόνων (JSON)



Αυτό είναι απλό και γίνεται στον RuleLoader.

Αν οι κανόνες δεν αλλάζουν συχνά, μπορείς να τους διαβάζεις μία φορά και να τους κρατάς σε στατική μεταβλητή ή memory cache.




public class CachedRuleLoader : IRuleLoader
{
private static List<RuleDefinition>? _cachedRules;

public List<RuleDefinition> LoadRules(string path)
{
if (_cachedRules != null)
return _cachedRules;

if (!File.Exists(path))
{
Console.WriteLine($"Το αρχείο κανόνων δεν βρέθηκε: {path}");
return new List<RuleDefinition>();
}

var json = File.ReadAllText(path);
if (string.IsNullOrWhiteSpace(json))
{
Console.WriteLine($"Το αρχείο κανόνων είναι κενό: {path}");
return new List<RuleDefinition>();
}

var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
_cachedRules = JsonSerializer.Deserialize<List<RuleDefinition>>(json, options) ?? new List<RuleDefinition>();

return _cachedRules;
}
}






🟢 Πλεονέκτημα: Το αρχείο διαβάζεται μόνο την πρώτη φορά.

🔴 Μειονέκτημα: Αν αλλάξεις το rules.json, πρέπει να γίνει restart ή refresh.



🧩 2️⃣ Caching των συνθηκών (expressions)



Αυτό είναι το πιο σημαντικό.

Η Dynamic Expresso μεταγλωττίζει (compile) τα expressions κάθε φορά που τα εκτελείς — και αυτό έχει κόστος.



Αν έχεις 500 κανόνες, μπορούμε να κάνουμε caching του compiled delegate (Func) στη μνήμη.



Νέα έκδοση του DynamicRuleEngine με expression cache:




using DynamicExpresso;
using System.Collections.Concurrent;

public class CachedDynamicRuleEngine : IRuleEngine
{
private readonly Interpreter _interpreter;
private readonly ConcurrentDictionary<string, Lambda> _compiledCache;

public CachedDynamicRuleEngine()
{
_interpreter = new Interpreter();
_compiledCache = new ConcurrentDictionary<string, Lambda>();
}

public decimal CalculateDiscount(Order order, List<RuleDefinition> rules)
{
_interpreter.SetVariable("Total", order.Total);
_interpreter.SetVariable("Customer", order.Customer);

foreach (var rule in rules)
{
try
{
// Αν ο κανόνας υπάρχει ήδη στο cache, χρησιμοποίησέ τον
var lambda = _compiledCache.GetOrAdd(rule.Condition, condition =>
_interpreter.Parse(condition, typeof(bool))
);

// Δημιουργία delegate που εκτελείται πάνω σε συγκεκριμένο αντικείμενο
bool isMatch = (bool)lambda.Invoke();

if (isMatch)
{
Console.WriteLine($"Ταιριάξε ο κανόνας: {rule.Name}");
return order.Total * rule.Discount;
}
}
catch (Exception ex)
{
Console.WriteLine($"Σφάλμα στην εκτέλεση κανόνα '{rule.Name}': {ex.Message}");
}
}

return 0;
}
}






Τι κάνει αυτό:




  • Η πρώτη φορά που τρέχει κάθε Condition → μεταγλωττίζεται και αποθηκεύεται στο _compiledCache.


  • Οι επόμενες φορές → χρησιμοποιούν τον ίδιο μεταγλωττισμένο delegate (καμία καθυστέρηση).




✅ Εξαιρετικά γρήγορο για πολλά Order αντικείμενα

✅ Τηρεί ακόμη OCP, DIP, SRP

✅ Λειτουργεί για εκατοντάδες κανόνες






💾 Πού γίνεται τελικά το caching;























Τύπος Cache Πού εφαρμόζεται Σκοπός
Rules cache Στον RuleLoader
Να μη φορτώνονται ξανά οι κανόνες από JSON
Expression cache Στον RuleEngine
Να μη μεταγλωττίζονται ξανά οι ίδιες συνθήκες





⚙️ Τελική Σύνοψη



Με αυτό το setup έχεις:




  • SOLID αρχιτεκτονική

  • Dynamic evaluation των κανόνων

  • Caching σε δύο επίπεδα

  • Υψηλή απόδοση για 500+ κανόνες και πολλά orders

  • Ευκολία επεκτασιμότητας χωρίς να αλλάξεις ούτε μια γραμμή κώδικα όταν
    προσθέτεις νέο rule

SOC Incident Playbook: Vulnerability Remediation & Verification
title: Detect Exploitation - Δυναμική Εφαρμογή Επιχειρησιακών Κανόνων σε C# με JSON και Func
id: 90b9b836-d416-476f-9d8c-0b86312b6012
status: experimental
description: Automatisch generierte SIEM-Erkennungsregel basierend auf CTI Intelligence
references:
  - https://tsecurity.de/
author: iShareStuff CTI Automated Detection Engine
date: 2026-09-24
logsource:
  category: network_connection
  product: any
detection:
  selection:
      CommandLine|contains:
        - 'exploit'
  condition: selection
falsepositives:
  - Legitime administrative Zugriffe oder Penetrationstests
level: high
tags:
  - attack.initial_access
rule CTI_Threat_Indicator {
    meta:
        author = "iShareStuff CTI Automated Detection Engine"
        date = "2026-09-24"
        description = "YARA Signature for "
    strings:
        $str = "Δυναμική Εφαρμογή Επιχειρησιακ" ascii wide
    condition:
        any of them
}
tsecurity.de Cognitive Threat RAG
Fokus-Vektor:

Kognitive Analyse für identifizierte Bedrohung: Erhöhte Bedrohungslage im Bereich Δυναμική Εφαρμογή Επιχειρησιακών Κανόνων.... Basierend auf 368k Vektor-Korrelationen werden sofortige Isolationsmaßnahmen für betroffene Endpunkte empfohlen.

🛡️ Angriffsfläche & Exposure

Netzwerk/Remote-Zugriff ohne Vorauthentifizierung möglich.

Empfohlene Sofortmaßnahmen
  • 1. Perimeter-Inspektion: Relevante Portfreigaben und exponierte Endpunkte unverzüglich scannen.
  • 2. Patch-Applikation: Hersteller-Hotfix einspielen oder betroffene Daemons in isolierte DMZ-Segmente überführen.
  • 3. Telemetrie & EDR-Alerts: Prozessaufrufe und Child-Processes auf anomale Shell-Spawns überwachen.
🔗 Semantisch verwandte Zero-Days MariaDB 11.7 VEC
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Δυναμική Εφαρμογή Επιχειρησιακών Κανόνων σε C# με JSON και Func

Thematisch verwandte Begriffe: Δυναμική, Εφαρμογή, Επιχειρησιακών, Κανόνων · 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-96772 | A security flaw has been discovered in Intelliants Subrion CMS up to 4.2…
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 TTP ⏱️ 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