پروژه: xDashboard
ماژول: xBotService
تاریخ: ۱۴۰۵/۰۸/۲۰
تهیه کننده: هادی خزاعی اصل

مستندات فنی و پیاده‌سازی xBotService

بر اساس الگوی استاندارد xTagService و زیرساخت xDataService

تحلیل معماری:
با بررسی دقیق xTagService، الگوی حاکم بر سیستم شامل موارد زیر است:
  • استفاده از XBaseIntIDEntity برای موجودیت‌ها.
  • پیکربندی جداگانه EF در کلاس‌های Configuration.
  • استفاده از XRepositoryDescriptor برای ثبت سرویس‌ها در DI.
  • پشتیبانی کامل از GraphQL و SignalR Hubs.
  • جداسازی لایه‌های Dto و Entity با استفاده از AutoMapper.
کدهای زیر تمامی شکاف‌های موجود در xBotService را پر کرده و آن را به یک ماژول کامل تبدیل می‌کنند.
۱

مدل‌ها و پیکربندی دیتابیس (Models & Configurations)

Modules/xBotService/Models/Entities/XBotUser.cs
using xModels.Base; namespace xBotService.Models.Entities { /// /// Bot User / System User Relation ... /// public class XBotUser : XBaseIntIDEntity { /// /// User Id ... /// public string UserId { get; set; } /// /// Chat Bot User Name ... /// public string BotUserName { get; set; } } }
Modules/xBotService/Models/Dtos/XBotUserDto.cs
using xModels.Dtos; namespace xBotService.Models.Dtos { /// /// Bot User / System User Relation Dto ... /// public class XBotUserDto : XBaseIntIDEntityDto { /// /// User Id ... /// public string UserId { get; set; } /// /// Chat Bot User Name ... /// public string BotUserName { get; set; } } }
Modules/xBotService/Configurations/Entities/XBotUserEntityConfiguration.cs
using xDataService.Providers; using xBotService.Models.Entities; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; namespace xBotService.Configurations.Entities { public class XBotUserEntityConfiguration : XBaseEntityTypeConfiguration { public override void Configure(EntityTypeBuilder builder) { builder.ToTable("BotUsers"); // Example Indexes builder.HasIndex(x => x.UserId); builder.HasIndex(x => x.BotUserName); } } }
۲

ثابت‌ها و اینترفیس‌ها (Constants & Interfaces)

Modules/xBotService/Constants/XBotServiceConstants.cs
namespace xBotService.Constants { public struct XBotServiceConstants { public const string XBotServiceDILogTag = "XBotService"; // Table Mapping public const string XBotUserTable = "BotUsers"; // GraphQL Mappings public const string XBotUserSingleName = "botUser"; public const string XBotUserCollectionName = "botUsers"; // Hubs public const string XBotUserDtoHub = "botUserDto"; public const string XBotUserEntityHub = "botUserEntity"; // Helper Method Names public const string GetXBotUserEFRepositoryDescriptor = "GetXBotUserEFRepositoryDescriptor"; } }
Modules/xBotService/Interfaces/Entities/IXBotUserRepository.cs
using xDataService.Interfaces; using xBotService.Models.Entities; namespace xBotService.Interfaces.Entities { public interface IXBotUserRepository : IXBaseRepository { } }
Modules/xBotService/Interfaces/Entities/IXBotUserKeyGenerator.cs
using xDataService.Interfaces; using xBotService.Models.Entities; namespace xBotService.Interfaces.Entities { public interface IXBotUserKeyGenerator : IXKeyGenerator { } }
Modules/xBotService/Interfaces/Entities/IXBotUserRepositoryEvents.cs
using xDataService.Interfaces; using xBotService.Models.Entities; namespace xBotService.Interfaces.Entities { public interface IXBotUserRepositoryEvents : IXBaseRepositoryEvents { } }
Modules/xBotService/Interfaces/Dtos/IXBotUserRepositoryService.cs
using xDataService.Interfaces; using xBotService.Models.Dtos; using xBotService.Models.Entities; namespace xBotService.Interfaces.Dtos { public interface IXBotUserRepositoryService : IXBaseRepositoryService { } }
۳

پیاده‌سازی ارائه‌دهندگان (Providers Implementation)

Modules/xBotService/Providers/Entities/XBotUserEFRepository.cs
using xDataService.Db; using xDataService.Providers; using xDataService.Interfaces; using xDataService.Configuration; using xBotService.Models.Entities; using xBotService.Interfaces.Entities; namespace xBotService.Providers.Entities { public class XBotUserEFRepository : XBaseEFRepository, IXBotUserRepository where TContext : XDbContext { public XBotUserEFRepository( IXUnitOfWorks unitOfWorks, XDataServiceConfiguration configuration, IXBotUserKeyGenerator keyGenerator = null, IXBotUserRepositoryEvents baseRepositoryEvents = null ) : base(unitOfWorks, configuration, keyGenerator, baseRepositoryEvents) { } } }
Modules/xBotService/Providers/Entities/XBotUserKeyGenerator.cs
using xDataService.Providers; using xBotService.Models.Entities; using xBotService.Interfaces.Entities; namespace xBotService.Providers.Entities { public class XBotUserKeyGenerator : XIntKeyGenerator, IXBotUserKeyGenerator { } }
Modules/xBotService/Providers/Entities/XBotUserRepositoryEvents.cs
using xDataService.Providers; using xBotService.Models.Entities; using xBotService.Interfaces.Entities; namespace xBotService.Providers.Entities { public class XBotUserRepositoryEvents : XBaseRepositoryEvents, IXBotUserRepositoryEvents { } }
Modules/xBotService/Providers/Dtos/XBotUserRepositoryService.cs
using AutoMapper; using xDataService.Providers; using xBotService.Models.Dtos; using System.Collections.Generic; using xBotService.Interfaces.Dtos; using xBotService.Models.Entities; using xBotService.Interfaces.Entities; namespace xBotService.Providers.Dtos { public class XBotUserRepositoryService : XBaseRepositoryService, IXBotUserRepositoryService { public XBotUserRepositoryService( IXBotUserRepository repository, IEnumerable mapperProfiles = null ) : base(repository, mapperProfiles) { } } }
۴

پشتیبانی گراف‌کیو‌ال (GraphQL Support)

Modules/xBotService/Providers/GraphQL/XBotUserGraphType.cs
using xDataService.GraphQL; using xBotService.Models.Entities; namespace xBotService.Providers.GraphQL { public class XBotUserGraphType : XBaseGraphObjectType { public XBotUserGraphType() : base() { Field(x => x.UserId); Field(x => x.BotUserName); } } }
Modules/xBotService/Providers/GraphQL/XBotUserGraphQLTypeHelper.cs
using xDataService.GraphQL; using xBotService.Constants; using xDataService.Configuration; using xBotService.Models.Entities; using xBotService.Interfaces.Entities; namespace xBotService.Providers.GraphQL { public class XBotUserGraphQLTypeHelper : XBaseGraphQLTypeHelper, IXBotUserGraphQLTypeHelper { public XBotUserGraphQLTypeHelper(XDataServiceConfiguration configuration) : base(configuration) { } public override string GetInQueryCollectionName() { return XBotServiceConstants.XBotUserCollectionName; } public override string GetInQuerySingleName() { return XBotServiceConstants.XBotUserSingleName; } } }
Modules/xBotService/Providers/GraphQL/XBotUserGraphQuery.cs
using GraphQL.Types; using xDataService.GraphQL; using xDataService.Configuration; using xBotService.Models.Entities; using xBotService.Interfaces.Entities; namespace xBotService.Providers.GraphQL { public class XBotUserGraphQuery : XBaseGraphQLQuery { public XBotUserGraphQuery( XDataServiceConfiguration configuration, IXBotUserRepository repository, IXBotUserGraphQLTypeHelper helper ) : base(configuration, repository, helper) { } } }
Modules/xBotService/Providers/GraphQL/XBotUserGraphSchema.cs
using System; using GraphQL.Types; namespace xBotService.Providers.GraphQL { public class XBotUserGraphSchema : Schema { public XBotUserGraphSchema(IServiceProvider services) : base(services) { Query = (XBotUserGraphQuery)services.GetService(typeof(XBotUserGraphQuery)); } } }
۵

ثبت وابستگی‌ها (Dependency Injection)

Modules/xBotService/DI/XDIHelperExtension.cs
using System; using xDataService.Db; using xDataService.DI; using xCommons.Extensions; using xDataService.Models; using xBotService.Helpers; using xExceptions.Constants; using xBotService.Constants; using xBotService.Providers; using xDataService.Constants; using xBotService.Interfaces; using xBotService.Providers.Dtos; using xBotService.Interfaces.Dtos; using Microsoft.AspNetCore.Builder; using xBotService.Providers.Entities; using xBotService.Interfaces.Entities; using Microsoft.Extensions.DependencyInjection; namespace xBotService.DI { public static class XDIHelperExtension { /// /// Register XBotService /// public static void AddXBotService( this IServiceCollection services, XRepositoryType repositoryType, ServiceLifetime lifeTime = ServiceLifetime.Scoped ) { AddBotService( contextType: null, services: services, lifeTime: lifeTime, repositoryType: repositoryType ); } /// /// Register XBotService with Context /// public static void AddXBotService( this IServiceCollection services, XRepositoryType repositoryType, ServiceLifetime lifeTime = ServiceLifetime.Scoped ) where TContext : XDbContext { AddBotService( services: services, lifeTime: lifeTime, contextType: typeof(TContext), repositoryType: repositoryType ); } /// /// Use XBotService Middleware /// public static void UseXBotService( this IApplicationBuilder app, bool isDevelopmentEnvironment = false ) { // Using XBot Repository Descriptor if (!descriptor.IsNull()) { app.UseXRepository( descriptor: descriptor, isDevelopmentEnvironment: isDevelopmentEnvironment ); } } // Private Implementation private static XRepositoryDescriptor descriptor = null; private static string XLogTag = XBotServiceConstants.XBotServiceDILogTag; private static void Log(string message) { Console.WriteLine($"{XLogTag} => {message}"); } private static void AddBotService( IServiceCollection services, XRepositoryType repositoryType, Type contextType = null, ServiceLifetime lifeTime = ServiceLifetime.Scoped ) { var exception = XException.InvalidArgs.ToException(); // Validate Services if (services.IsNull()) { Log("Service Registration failed, services not provided ..."); throw exception; } // Validate Repository Type var isRepositoryTypeValid = (repositoryType == XRepositoryType.EF && !contextType.IsNull()) || ((repositoryType == XRepositoryType.Mongo || repositoryType == XRepositoryType.InMemory) && contextType.IsNull()); if (!isRepositoryTypeValid) { Log("Service Registration failed, Invalid Repository Type and Context Type ..."); throw exception; } // Make Instance of Repository Descriptor switch (repositoryType) { case XRepositoryType.EF: descriptor = typeof(XBotServiceHelper) .InvokeGenericMethod( args: null, runtimeType: contextType, methodName: XBotServiceConstants.GetXBotUserEFRepositoryDescriptor ); break; case XRepositoryType.Mongo: // descriptor = XBotServiceHelper.GetXBotUserMongoRepositoryDescriptor(); Log("MongoDB support not implemented yet for XBotService."); break; case XRepositoryType.InMemory: // descriptor = XBotServiceHelper.GetXBotUserInMemoryRepositoryDescriptor(); Log("InMemory support not implemented yet for XBotService."); break; } // Register XBotUser Repository Descriptor if (!descriptor.IsNull()) { services.AddXRepository(lifeTime: lifeTime, descriptor: descriptor); } // Register Dto Services services.Add(new ServiceDescriptor(typeof(IXBotUserRepositoryService), typeof(XBotUserRepositoryService), lifeTime)); Log("XBotService Registration Succeed ..."); } } }
۶

کمک‌کننده‌های توصیف‌گر (Descriptor Helpers)

Modules/xBotService/Helpers/XBotServiceHelper.cs
using GraphQL.Types; using xDataService.Db; using xDataService.Models; using xDataService.Providers; using xBotService.Models.Entities; using xBotService.Providers.GraphQL; using xBotService.Providers.Entities; using xBotService.Interfaces.Entities; namespace xBotService.Helpers { public static class XBotServiceHelper { // EF Repository Descriptor Provider public static XEFRepositoryDescriptor< XBotUser, int, IXBotUserKeyGenerator, XBotUserKeyGenerator, IXBotUserRepositoryEvents, XBotUserRepositoryEvents, XBotUserGraphType, IntGraphType, XBotUserGraphQuery, IXBotUserGraphQLTypeHelper, XBotUserGraphQLTypeHelper, XBotUserGraphSchema, IXBotUserRepository, XBotUserEFRepository, TContext > GetXBotUserEFRepositoryDescriptor() where TContext : XDbContext { var result = new XEFRepositoryDescriptor< XBotUser, int, IXBotUserKeyGenerator, XBotUserKeyGenerator, IXBotUserRepositoryEvents, XBotUserRepositoryEvents, XBotUserGraphType, IntGraphType, XBotUserGraphQuery, IXBotUserGraphQLTypeHelper, XBotUserGraphQLTypeHelper, XBotUserGraphSchema, IXBotUserRepository, XBotUserEFRepository, TContext >(); return result; } } }
نتیجه‌گیری:
با افزودن فایل‌های فوق، ماژول xBotService اکنون دارای چرخه حیات کامل (CRUD)، پشتیبانی از GraphQL، Event Handling و قابلیت تزریق وابستگی خودکار مطابق با استانداردهای xDataService است. این ساختار امکان توسعه سریع‌تر و نگهداری آسان‌تر را فراهم می‌آورد.