using Microsoft.EntityFrameworkCore; using SmartStack.Application.Common.Interfaces; using {{ProjectName}}.Application.Common.Interfaces; using {{ProjectName}}.Domain.Entities; namespace {{ProjectName}}.Infrastructure.Services; /// /// Example service demonstrating how to use ICoreDataService /// to fetch Core entity data (User, Role, etc.) from Extensions context. /// public class OrderService : IOrderService { private readonly IExtensionsDbContext _context; private readonly ICoreDataService _coreDataService; public OrderService( IExtensionsDbContext context, ICoreDataService coreDataService) { _context = context; _coreDataService = coreDataService; } /// /// Gets an order by ID, including customer information from Core. /// public async Task GetOrderWithCustomerAsync( Guid orderId, CancellationToken cancellationToken = default) { // 1. Get the order from Extensions context var order = await _context.Orders .AsNoTracking() .FirstOrDefaultAsync(o => o.Id == orderId, cancellationToken); if (order == null) return null; // 2. Use ICoreDataService to fetch customer info from Core context // This is the correct way to access Core entities from Extensions! var customer = await _coreDataService.GetUserBasicInfoAsync( order.CustomerId, cancellationToken); // 3. Combine the data return new OrderWithCustomerDto { Id = order.Id, OrderNumber = order.OrderNumber, Status = order.Status.ToString(), TotalAmount = order.TotalAmount, OrderDate = order.OrderDate, CustomerId = order.CustomerId, CustomerEmail = customer?.Email ?? "Unknown", CustomerName = customer?.DisplayName ?? "Unknown" }; } /// /// Gets all orders for a customer. /// public async Task> GetOrdersByCustomerAsync( Guid customerId, CancellationToken cancellationToken = default) { // First verify the customer exists in Core var customer = await _coreDataService.GetUserBasicInfoAsync( customerId, cancellationToken); if (customer == null) throw new InvalidOperationException($"Customer {customerId} not found."); // Get orders from Extensions context return await _context.Orders .AsNoTracking() .Where(o => o.CustomerId == customerId) .OrderByDescending(o => o.OrderDate) .Select(o => new OrderDto { Id = o.Id, OrderNumber = o.OrderNumber, Status = o.Status.ToString(), TotalAmount = o.TotalAmount, OrderDate = o.OrderDate }) .ToListAsync(cancellationToken); } /// /// Creates a new order. /// public async Task CreateOrderAsync( string orderNumber, Guid customerId, decimal totalAmount, CancellationToken cancellationToken = default) { // Validate that the customer exists in Core before creating order var customer = await _coreDataService.GetUserByIdAsync( customerId, cancellationToken); if (customer == null) throw new InvalidOperationException($"Customer {customerId} not found."); if (!customer.IsActive) throw new InvalidOperationException("Cannot create order for inactive customer."); // Create the order in Extensions context var order = Order.Create(orderNumber, customerId, totalAmount); _context.Orders.Add(order); await _context.SaveChangesAsync(cancellationToken); return order.Id; } } // === DTOs === public record OrderDto { public Guid Id { get; init; } public string OrderNumber { get; init; } = null!; public string Status { get; init; } = null!; public decimal TotalAmount { get; init; } public DateTime OrderDate { get; init; } } public record OrderWithCustomerDto : OrderDto { public Guid CustomerId { get; init; } public string CustomerEmail { get; init; } = null!; public string CustomerName { get; init; } = null!; } // === Interface === public interface IOrderService { Task GetOrderWithCustomerAsync(Guid orderId, CancellationToken cancellationToken = default); Task> GetOrdersByCustomerAsync(Guid customerId, CancellationToken cancellationToken = default); Task CreateOrderAsync(string orderNumber, Guid customerId, decimal totalAmount, CancellationToken cancellationToken = default); }