+
هرگز موجودیتها را مستقیماً به خروجی API ندهید؛ چون روابط دوطرفه باعث خطای A possible object cycle was detected میشود. بهجای آن از DTO و Projection استفاده کنید.
+
+
Dtos/ProductDtos.cs
+
public record ProductSummaryDto(
+ string Id, string Title, string? Description, string? Owner, int ProjectCount);
+
+public record ProjectDto(
+ string Id, string Title, string? Type, string? RepositoryUrl, string? Framework,
+ List<string> Dependencies, List<string> Exploits, int DeploymentCount);
+
+public record ProductDetailDto(
+ string Id, string Title, string? Description, string? Owner,
+ DateTime CreatedAt, List<ProjectDto> Projects);
+
+public record CreateProductDto(string Title, string? Description, string? Owner);
+
+public record UpdateProductDto(string Title, string? Description, string? Owner);
+
+
Services/IProductService.cs + ProductService.cs
+
public interface IProductService
+{
+ Task<IReadOnlyList<ProductSummaryDto>> GetAllAsync(CancellationToken ct = default);
+ Task<ProductDetailDto?> GetAsync(string id, CancellationToken ct = default);
+ Task<string> CreateAsync(CreateProductDto dto, CancellationToken ct = default);
+ Task<bool> UpdateAsync(string id, UpdateProductDto dto, CancellationToken ct = default);
+ Task<bool> DeleteAsync(string id, CancellationToken ct = default);
+}
+
+public class ProductService : IProductService
+{
+ private readonly IUnitOfWork _uow;
+
+ public ProductService(IUnitOfWork uow) => _uow = uow;
+
+ public async Task<IReadOnlyList<ProductSummaryDto>> GetAllAsync(CancellationToken ct = default)
+ => await _uow.Products.Query() // AsNoTracking
+ .OrderBy(p => p.Title)
+ .Select(p => new ProductSummaryDto(
+ p.Id, p.Title, p.Description, p.Metadata.Owner, p.Projects.Count))
+ .ToListAsync(ct);
+
+ public async Task<ProductDetailDto?> GetAsync(string id, CancellationToken ct = default)
+ => await _uow.Products.Query()
+ .Where(p => p.Id == id)
+ .Select(p => new ProductDetailDto(
+ p.Id, p.Title, p.Description, p.Metadata.Owner, p.Metadata.CreatedAt,
+ p.Projects.Select(pr => new ProjectDto(
+ pr.Id, pr.Title, pr.Type, pr.RepositoryUrl, pr.Metadata.Framework,
+ pr.Dependencies.Select(d => d.DependsOnProjectId).ToList(),
+ pr.Exploits.Select(e => e.ExploitedProjectId).ToList(),
+ pr.Deployments.Count)).ToList()))
+ .FirstOrDefaultAsync(ct);
+
+ public async Task<string> CreateAsync(CreateProductDto dto, CancellationToken ct = default)
+ {
+ var product = new Product
+ {
+ Id = $"prod-{DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}",
+ Title = dto.Title,
+ Description = dto.Description,
+ Metadata = new Metadata
+ {
+ CreatedAt = DateTime.UtcNow,
+ Owner = dto.Owner,
+ CustomFields = new Dictionary<string, string>()
+ }
+ };
+
+ await _uow.Products.AddAsync(product, ct);
+ await _uow.SaveChangesAsync(ct);
+ return product.Id;
+ }
+
+ public async Task<bool> UpdateAsync(string id, UpdateProductDto dto, CancellationToken ct = default)
+ {
+ var product = await _uow.Products.GetByIdAsync(id, ct);
+ if (product is null) return false;
+
+ product.Title = dto.Title;
+ product.Description = dto.Description;
+ product.Metadata.Owner = dto.Owner;
+ product.Metadata.UpdatedAt = DateTime.UtcNow;
+
+ _uow.Products.Update(product);
+ await _uow.SaveChangesAsync(ct);
+ return true;
+ }
+
+ public async Task<bool> DeleteAsync(string id, CancellationToken ct = default)
+ {
+ var product = await _uow.Products.GetByIdAsync(id, ct);
+ if (product is null) return false;
+
+ _uow.Products.Remove(product); // Cascade: پروژهها، وابستگیها و استقرارها هم حذف میشوند
+ await _uow.SaveChangesAsync(ct);
+ return true;
+ }
+}
+
+
Services/BoardService.cs — نمونه عملیات دامنهای (جابجایی کارت)
+
public interface IBoardService
+{
+ Task<Board?> GetBoardAsync(string boardId, CancellationToken ct = default);
+ Task<bool> MoveCardAsync(string cardId, string targetColumnId, CancellationToken ct = default);
+ Task<string> CreateCardAsync(string columnId, string title, string? description, CancellationToken ct = default);
+}
+
+public class BoardService : IBoardService
+{
+ private readonly IUnitOfWork _uow;
+ public BoardService(IUnitOfWork uow) => _uow = uow;
+
+ public Task<Board?> GetBoardAsync(string boardId, CancellationToken ct = default)
+ => _uow.Boards.Query()
+ .AsSplitQuery()
+ .Include(b => b.Columns.OrderBy(c => c.Order))
+ .ThenInclude(c => c.Cards)
+ .FirstOrDefaultAsync(b => b.Id == boardId, ct);
+
+ public async Task<bool> MoveCardAsync(string cardId, string targetColumnId, CancellationToken ct = default)
+ {
+ var card = await _uow.Cards.GetByIdAsync(cardId, ct);
+ if (card is null) return false;
+
+ var targetColumn = await _uow.Columns.GetByIdAsync(targetColumnId, ct);
+ if (targetColumn is null || targetColumn.BoardId != card.Column.BoardId) return false;
+
+ var fromTitle = card.Column?.Title ?? "—";
+
+ card.ColumnId = targetColumnId;
+ card.UpdatedAt = DateTime.UtcNow;
+ card.History.Add(new CardHistoryItem
+ {
+ Type = "move",
+ Title = $"جابجایی به \"{targetColumn.Title}\"",
+ From = fromTitle,
+ To = targetColumn.Title,
+ Date = DateTime.UtcNow
+ });
+
+ _uow.Cards.Update(card);
+ await _uow.SaveChangesAsync(ct);
+ return true;
+ }
+
+ public async Task<string> CreateCardAsync(string columnId, string title, string? description,
+ CancellationToken ct = default)
+ {
+ var card = new BoardCard
+ {
+ Id = $"card-{DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}",
+ ColumnId = columnId,
+ Title = title,
+ Description = description,
+ CreatedAt = DateTime.UtcNow,
+ UpdatedAt = DateTime.UtcNow
+ };
+ card.History.Add(new CardHistoryItem
+ {
+ Type = "create", Title = "ایجاد کارت", From = "—", To = "ستون مقصد", Date = DateTime.UtcNow
+ });
+
+ await _uow.Cards.AddAsync(card, ct);
+ await _uow.SaveChangesAsync(ct);
+ return card.Id;
+ }
+}
+