Nergora.CampaignEngine.Core 1.0.0

dotnet add package Nergora.CampaignEngine.Core --version 1.0.0
                    
NuGet\Install-Package Nergora.CampaignEngine.Core -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Nergora.CampaignEngine.Core" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Nergora.CampaignEngine.Core" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Nergora.CampaignEngine.Core" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Nergora.CampaignEngine.Core --version 1.0.0
                    
#r "nuget: Nergora.CampaignEngine.Core, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Nergora.CampaignEngine.Core@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Nergora.CampaignEngine.Core&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Nergora.CampaignEngine.Core&version=1.0.0
                    
Install as a Cake Tool

CampaignEngine.Core

RuleEngine.Core uzerine insa edilmis modern kampanya yonetim sistemi. Kurumsal senaryolar icin onceliklendirme, kota ve audit odakli calisir.

🎯 Özellikler

  • Kural Tabanlı Sistem: RuleEngine.Core ile entegre
  • İndirim Kampanyaları: Yüzde veya sabit tutar indirimleri
  • Ürün Hediye Kampanyaları: Sepet bazlı hediye ürünler
  • Kota Yönetimi: Kampanya kullanım limitleri
  • Öncelik Sistemi: Kampanya önceliklendirme
  • Cache Desteği: Memory cache ile performans optimizasyonu
  • Dependency Injection: Modern .NET DI pattern
  • Sepet Entegrasyonu: ITravelProduct ile kampanya kullanım/geri alma
  • Uygun Kampanyalar: Ürün bazında available campaign hesaplama

📦 Kurulum

dotnet add package Nergora.CampaignEngine.Core

🚀 Hızlı Başlangıç

1. Service Registration

services.AddCampaignEngine();
services.AddLogging();

2. Model Tanımlama

public class MyCampaignInput : RuleInputModel
{
    public decimal TotalAmount { get; set; }
    public string Country { get; set; }
    public int UsageCount { get; set; }
}

public class MyCampaignOutput : CampaignOutput
{
    // CampaignOutput'tan TotalDiscount ve CampaignProductDiscount gelir
}

3. Campaign Manager Oluşturma

var campaignManager = new CampaignManager<MyCampaignInput, MyCampaignOutput>(
    moduleId: 1,
    serviceProvider: serviceProvider,
    logger: logger,
    typeof(Price) // Extra types for rule compilation
);

4. Kampanya Tanımlama

var campaign = new GeneralCampaign
{
    Code = "SUMMER2024",
    Name = "Yaz İndirimi",
    ModulId = 1,
    Priority = 100,
    StartDate = DateTime.Now,
    EndDate = DateTime.Now.AddMonths(3),
    
    // Seçim kuralı - Kampanya ne zaman uygulanır?
    Predicate = "Input.TotalAmount > 500 && Input.Country == \"TR\"",
    
    // Sonuç kuralı - Ne kadar indirim yapılır?
    Result = @"Output.TotalDiscount = Input.TotalAmount * 0.2m;",
    
    // Kullanım kuralı - Kimler kullanabilir?
    Usage = "Input.UsageCount < 10",
    
    CampaignTypes = (int)CampaignTypes.DiscountCampaign,
    Quota = 1000
};

repository.AddCampaign(campaign);

5. Kampanya Uygulama

var input = new MyCampaignInput 
{ 
    TotalAmount = 600,
    Country = "TR",
    UsageCount = 5
};

var campaigns = campaignManager.GetCampaign(input);

foreach (var campaign in campaigns)
{
    Console.WriteLine($"Discount: {campaign.TotalDiscount}");
}

6. Kampanya Setlerini ve Uygun Kampanyalari Alma

var campaignResults = campaignManager.GetCampaign(input, out var ruleSets);

var available = campaignManager.GetAvailableCampaigns(
    productKey: "PRD-001",
    productsInTransaction: products,
    input: input);

7. Sepet Kampanya Kullan / Geri Al

products = campaignManager.UseCampaign(
    productKey: "PRD-001",
    campaignCode: "CITYGIFT50",
    productsInTransaction: products);

campaignManager.DeleteCampaign("CITYGIFT50", products);

8. Demo Seed (Hazir Kampanyalar)

using CampaignEngine.Core.Demo;
using CampaignEngine.Core.Repositories;

if (campaignRepository is InMemoryCampaignRepository memoryRepo)
{
    CampaignSeed.SeedToRepository(memoryRepo, moduleId: 1);
}

📋 Kampanya Tipleri

DiscountCampaign (0)

İndirim kampanyaları - En yüksek öncelikli kampanya uygulanır

Predicate = "Input.TotalAmount > 1000",
Result = "Output.TotalDiscount = new Price(200, \"TRY\");",
CampaignTypes = (int)CampaignTypes.DiscountCampaign

ProductGiftCampaign (1)

Ürün hediye kampanyaları - Tüm uygun kampanyalar uygulanır

Predicate = "Input.ProductCount >= 3",
Result = "Output.TotalDiscount = new Price(50, \"TRY\");",
CampaignTypes = (int)CampaignTypes.ProductGiftCampaign

🔧 Kural Yazımı

Predicate (Seçim) Kuralı

Kampanyanın ne zaman uygulanacağını belirler:

// Basit koşul
"Input.TotalAmount > 500"

// Çoklu koşul
"Input.TotalAmount > 500 && Input.Country == \"TR\""

// Tarih kontrolü
"Input.OrderDate >= DateTime.Now.AddDays(-7)"

// Liste kontrolü
"Input.Categories.Contains(\"Electronics\")"

Result (Sonuç) Kuralı

İndirim miktarını hesaplar:

// Sabit tutar
"Output.TotalDiscount = new Price(100, \"TRY\");"

// Yüzde hesaplama
"Output.TotalDiscount = Input.TotalAmount * 0.15m;"

// Koşullu hesaplama
@"if (Input.TotalAmount > 1000)
    Output.TotalDiscount = Input.TotalAmount * 0.2m;
  else
    Output.TotalDiscount = Input.TotalAmount * 0.1m;"

Usage (Kullanım) Kuralı

Kampanyayı kimlerin kullanabileceğini belirler:

// Kullanım sayısı kontrolü
"Input.UsageCount < 5"

// İlk alışveriş kontrolü
"Input.IsFirstPurchase == true"

// Üyelik kontrolü
"Input.MembershipLevel >= 2"

💡 İleri Seviye Örnekler

Kademeli İndirim

Predicate = "Input.TotalAmount > 100",
Result = @"
    if (Input.TotalAmount > 2000)
        Output.TotalDiscount = Input.TotalAmount * 0.25m;
    else if (Input.TotalAmount > 1000)
        Output.TotalDiscount = Input.TotalAmount * 0.15m;
    else
        Output.TotalDiscount = Input.TotalAmount * 0.10m;"

Kategori Bazlı İndirim

Predicate = "Input.Categories.Any(c => c == \"Electronics\")",
Result = @"
    var electronicsTotal = Input.Products
        .Where(p => p.Category == ""Electronics"")
        .Sum(p => p.Price.Value);
    Output.TotalDiscount = new Price(electronicsTotal * 0.2m, ""TRY"");"

Tarih Bazlı Kampanya

Predicate = @"
    var now = DateTime.Now;
    now.DayOfWeek == DayOfWeek.Friday && 
    now.Hour >= 18 && 
    now.Hour < 22",
Result = "Output.TotalDiscount = Input.TotalAmount * 0.30m;"

🎨 Custom Repository

public class SqlCampaignRepository : ICampaignRepository
{
    private readonly DbContext _context;
    
    public IEnumerable<GeneralCampaign> GetCampaigns(DateTime after, int moduleId)
    {
        return _context.Campaigns
            .Where(c => c.CreateDate > after && c.ModulId == moduleId)
            .ToList();
    }
    
    public IDictionary<string, bool> GetAllCampaigns(IDictionary<string, bool> keys)
    {
        var codes = keys.Keys.ToList();
        var existing = _context.Campaigns
            .Where(c => codes.Contains(c.Code))
            .Select(c => c.Code)
            .ToList();
            
        return keys.ToDictionary(k => k.Key, k => existing.Contains(k.Key));
    }
    
    public bool CheckCampaignQuota(int quota, int campaignId)
    {
        var usageCount = _context.CampaignUsages
            .Count(u => u.CampaignId == campaignId);
        return usageCount < quota;
    }
}

// Register
services.AddSingleton<ICampaignRepository, SqlCampaignRepository>();

🔍 Debugging

Kural hatalarını görmek için:

try
{
    var campaigns = campaignManager.GetCampaign(input);
}
catch (RuleRuntimeException ex)
{
    Console.WriteLine($"Rule Code: {ex.Code}");
    Console.WriteLine($"Input: {ex.Input}");
    Console.WriteLine($"Priority: {ex.Priority}");
    Console.WriteLine($"Error: {ex.Message}");
}

📊 Performans

  • Cache: Memory cache ile tekrarlayan sorgular optimize edilir
  • Compilation: Kurallar bir kez derlenir, cache'lenir
  • Background Processing: Kural güncellemeleri arka planda işlenir
  • Thread-Safe: Concurrent dictionary ile güvenli erişim

🧪 Test

[Fact]
public void Should_Apply_Discount_Campaign()
{
    // Arrange
    var input = new CampaignInput { TotalAmount = 600 };
    
    // Act
    var campaigns = campaignManager.GetCampaign(input);
    
    // Assert
    Assert.NotEmpty(campaigns);
    Assert.True(campaigns.First().TotalDiscount.Value > 0);
}

📚 Daha Fazla Bilgi

🧾 NuGet Notları

  • Paket: Nergora.CampaignEngine.Core
  • Bu paketle gelen yeni eklemeler: available campaign hesaplama, kullanım/geri alma akisi, demo seed yardimcisi
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 132 7/12/2026