167 lines
4.1 KiB
C#
167 lines
4.1 KiB
C#
using System;
|
||
using System.ComponentModel.DataAnnotations;
|
||
using Volo.Abp;
|
||
using Volo.Abp.Domain.Entities.Auditing;
|
||
|
||
namespace Sozsoft.Platform.Entities;
|
||
|
||
/// <summary>
|
||
/// Tenant bazında dinamik olarak tanımlanmış AppService'lerin kod ve meta verilerini saklar
|
||
/// </summary>
|
||
public class DynamicService : FullAuditedEntity<Guid>
|
||
{
|
||
/// <summary>
|
||
/// AppService'in benzersiz adı (örn: "DynamicCustomerService")
|
||
/// </summary>
|
||
[Required]
|
||
[StringLength(256)]
|
||
public string Name { get; set; }
|
||
|
||
/// <summary>
|
||
/// AppService'in kullanıcı dostu başlığı
|
||
/// </summary>
|
||
[StringLength(512)]
|
||
public string? DisplayName { get; set; }
|
||
|
||
/// <summary>
|
||
/// AppService açıklaması
|
||
/// </summary>
|
||
[StringLength(2000)]
|
||
public string? Description { get; set; }
|
||
|
||
/// <summary>
|
||
/// Tam C# sınıf kodu (using'ler ve namespace dahil)
|
||
/// </summary>
|
||
[Required]
|
||
public string Code { get; set; }
|
||
|
||
/// <summary>
|
||
/// AppService aktif mi?
|
||
/// </summary>
|
||
public bool IsActive { get; set; }
|
||
|
||
/// <summary>
|
||
/// Son derleme durumu
|
||
/// </summary>
|
||
public CompilationStatus CompilationStatus { get; set; }
|
||
|
||
/// <summary>
|
||
/// Son derleme hatası (varsa)
|
||
/// </summary>
|
||
public string? LastCompilationError { get; set; }
|
||
|
||
/// <summary>
|
||
/// Son başarılı derleme zamanı
|
||
/// </summary>
|
||
public DateTime? LastSuccessfulCompilation { get; set; }
|
||
|
||
/// <summary>
|
||
/// Kod versiyonu (her güncelleme ile artan numara)
|
||
/// </summary>
|
||
public int Version { get; set; }
|
||
|
||
/// <summary>
|
||
/// Kod hash'i (değişiklik kontrolü için)
|
||
/// </summary>
|
||
[StringLength(64)]
|
||
public string? CodeHash { get; set; }
|
||
|
||
/// <summary>
|
||
/// AppService'in kullandığı ana entity türü (opsiyonel)
|
||
/// </summary>
|
||
[StringLength(256)]
|
||
public string? PrimaryEntityType { get; set; }
|
||
|
||
/// <summary>
|
||
/// Swagger'da görünecek controller adı
|
||
/// </summary>
|
||
[StringLength(256)]
|
||
public string? ControllerName { get; set; }
|
||
|
||
protected DynamicService()
|
||
{
|
||
// EF Core constructor
|
||
}
|
||
|
||
public DynamicService(
|
||
Guid id,
|
||
string name,
|
||
string code) : base(id)
|
||
{
|
||
Name = Check.NotNullOrWhiteSpace(name, nameof(name), maxLength: 256);
|
||
Code = Check.NotNullOrWhiteSpace(code, nameof(code));
|
||
IsActive = true;
|
||
CompilationStatus = CompilationStatus.Pending;
|
||
Version = 1;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Kodu günceller ve versiyonu artırır
|
||
/// </summary>
|
||
public void UpdateCode(string newCode)
|
||
{
|
||
Check.NotNullOrWhiteSpace(newCode, nameof(newCode));
|
||
|
||
if (Code != newCode)
|
||
{
|
||
Code = newCode;
|
||
Version++;
|
||
CompilationStatus = CompilationStatus.Pending;
|
||
LastCompilationError = null;
|
||
CodeHash = GenerateCodeHash(newCode);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Derleme başarısını işaretler
|
||
/// </summary>
|
||
public void MarkCompilationSuccess()
|
||
{
|
||
CompilationStatus = CompilationStatus.Success;
|
||
LastCompilationError = null;
|
||
LastSuccessfulCompilation = DateTime.Now;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Derleme hatasını işaretler
|
||
/// </summary>
|
||
public void MarkCompilationError(string error)
|
||
{
|
||
CompilationStatus = CompilationStatus.Failed;
|
||
LastCompilationError = error;
|
||
}
|
||
|
||
private string GenerateCodeHash(string code)
|
||
{
|
||
using var sha256 = System.Security.Cryptography.SHA256.Create();
|
||
var hash = sha256.ComputeHash(System.Text.Encoding.UTF8.GetBytes(code));
|
||
return Convert.ToHexString(hash);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Derleme durumu enum'u
|
||
/// </summary>
|
||
public enum CompilationStatus
|
||
{
|
||
/// <summary>
|
||
/// Henüz derlenmedi
|
||
/// </summary>
|
||
Pending = 0,
|
||
|
||
/// <summary>
|
||
/// Derleme başarılı
|
||
/// </summary>
|
||
Success = 1,
|
||
|
||
/// <summary>
|
||
/// Derleme başarısız
|
||
/// </summary>
|
||
Failed = 2,
|
||
|
||
/// <summary>
|
||
/// Derleme işlemi devam ediyor
|
||
/// </summary>
|
||
InProgress = 3
|
||
}
|
||
|