Files
2026-11-11 15:48:11 +03:30

754 lines
23 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>پیاده‌سازی ماژول xBotService | مستندات فنی و سورس کد</title>
<style>
@import url('https://fonts.googleapis.com/css2?family=Vazirmatn:wght@300;400;500;600;700;800&display=swap');
:root {
--primary: #1a365d;
--primary-light: #2c5282;
--accent: #c9a961;
--accent-dark: #a8893f;
--bg: #faf9f6;
--card-bg: #ffffff;
--text: #1a202c;
--text-muted: #4a5568;
--border: #e2e8f0;
--success: #38a169;
--code-bg: #2d3748;
--code-text: #e2e8f0;
--shadow: 0 4px 6px rgba(0,0,0,0.05), 0 10px 30px rgba(26,54,93,0.08);
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: 'Vazirmatn', Tahoma, sans-serif;
background: var(--bg);
color: var(--text);
line-height: 1.9;
font-size: 15px;
padding: 30px 15px;
}
.document-container {
max-width: 1100px;
margin: 0 auto;
background: var(--card-bg);
box-shadow: var(--shadow);
border-radius: 12px;
overflow: hidden;
}
.document-header {
background: linear-gradient(135deg, var(--primary) 0%, var(--primary-light) 100%);
color: white;
padding: 50px 40px 40px;
position: relative;
border-bottom: 4px solid var(--accent);
}
.header-top {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 30px;
}
.doc-meta {
text-align: left;
font-size: 12px;
opacity: 0.9;
}
.document-title h1 {
font-size: 28px;
font-weight: 800;
margin-bottom: 10px;
}
.document-title h2 {
font-size: 16px;
font-weight: 400;
opacity: 0.9;
}
.document-body {
padding: 40px;
}
.section {
margin-bottom: 50px;
page-break-inside: avoid;
}
.section-header {
display: flex;
align-items: center;
gap: 15px;
padding-bottom: 12px;
border-bottom: 2px solid var(--border);
margin-bottom: 25px;
}
.section-number {
width: 50px; height: 50px;
background: linear-gradient(135deg, var(--primary), var(--primary-light));
color: white;
border-radius: 12px;
display: flex;
align-items: center;
justify-content: center;
font-weight: 800;
font-size: 20px;
flex-shrink: 0;
}
.section-title h3 {
font-size: 20px;
font-weight: 700;
color: var(--primary);
}
.code-block {
background: var(--code-bg);
color: var(--code-text);
border-radius: 8px;
padding: 20px;
overflow-x: auto;
font-family: 'Consolas', 'Monaco', monospace;
font-size: 13px;
line-height: 1.6;
margin-top: 15px;
border: 1px solid var(--border);
direction: ltr;
text-align: left;
white-space: pre;
}
.file-path {
color: var(--accent);
font-weight: 600;
margin-bottom: 10px;
display: block;
font-family: 'Consolas', monospace;
font-size: 12px;
}
.info-box {
background: linear-gradient(to left, #ebf8ff, #e6f6ff);
border: 1px solid #90cdf4;
border-right: 4px solid var(--primary-light);
padding: 18px 22px;
border-radius: 8px;
margin: 20px 0;
font-size: 14px;
}
.success-box {
background: linear-gradient(to left, #f0fff4, #e6fffa);
border: 1px solid #9ae6b4;
border-right: 4px solid var(--success);
padding: 18px 22px;
border-radius: 8px;
margin: 20px 0;
font-size: 14px;
}
.document-footer {
background: var(--primary);
color: white;
padding: 25px 40px;
text-align: center;
font-size: 12px;
margin-top: 40px;
}
.footer-brand {
font-weight: 700;
color: var(--accent);
margin-bottom: 5px;
font-size: 14px;
}
@media print {
body { background: white; padding: 0; }
.document-container { box-shadow: none; }
.code-block { white-space: pre-wrap; }
}
</style>
</head>
<body>
<div class="document-container">
<header class="document-header">
<div class="header-top">
<div class="doc-meta">
<div>پروژه: xDashboard</div>
<div>ماژول: xBotService</div>
<div>تاریخ: ۱۴۰۵/۰۸/۲۰</div>
<div>تهیه کننده: هادی خزاعی اصل</div>
</div>
</div>
<div class="document-title">
<h1>مستندات فنی و پیاده‌سازی xBotService</h1>
<h2>بر اساس الگوی استاندارد xTagService و زیرساخت xDataService</h2>
</div>
</header>
<div class="document-body">
<div class="info-box">
<strong>تحلیل معماری:</strong><br>
با بررسی دقیق <code>xTagService</code>، الگوی حاکم بر سیستم شامل موارد زیر است:
<ul style="padding-right: 20px; margin-top: 10px;">
<li>استفاده از <strong>XBaseIntIDEntity</strong> برای موجودیت‌ها.</li>
<li>پیکربندی جداگانه EF در کلاس‌های <strong>Configuration</strong>.</li>
<li>استفاده از <strong>XRepositoryDescriptor</strong> برای ثبت سرویس‌ها در DI.</li>
<li>پشتیبانی کامل از <strong>GraphQL</strong> و <strong>SignalR Hubs</strong>.</li>
<li>جداسازی لایه‌های Dto و Entity با استفاده از AutoMapper.</li>
</ul>
کدهای زیر تمامی شکاف‌های موجود در <code>xBotService</code> را پر کرده و آن را به یک ماژول کامل تبدیل می‌کنند.
</div>
<!-- Section 1: Models & Configurations -->
<div class="section">
<div class="section-header">
<div class="section-number">۱</div>
<div class="section-title">
<h3>مدل‌ها و پیکربندی دیتابیس (Models & Configurations)</h3>
</div>
</div>
<span class="file-path">Modules/xBotService/Models/Entities/XBotUser.cs</span>
<div class="code-block">using xModels.Base;
namespace xBotService.Models.Entities
{
/// &lt;summary&gt;
/// Bot User / System User Relation ...
/// &lt;/summary&gt;
public class XBotUser : XBaseIntIDEntity
{
/// &lt;summary&gt;
/// User Id ...
/// &lt;/summary&gt;
public string UserId { get; set; }
/// &lt;summary&gt;
/// Chat Bot User Name ...
/// &lt;/summary&gt;
public string BotUserName { get; set; }
}
}</div>
<span class="file-path">Modules/xBotService/Models/Dtos/XBotUserDto.cs</span>
<div class="code-block">using xModels.Dtos;
namespace xBotService.Models.Dtos
{
/// &lt;summary&gt;
/// Bot User / System User Relation Dto ...
/// &lt;/summary&gt;
public class XBotUserDto : XBaseIntIDEntityDto
{
/// &lt;summary&gt;
/// User Id ...
/// &lt;/summary&gt;
public string UserId { get; set; }
/// &lt;summary&gt;
/// Chat Bot User Name ...
/// &lt;/summary&gt;
public string BotUserName { get; set; }
}
}</div>
<span class="file-path">Modules/xBotService/Configurations/Entities/XBotUserEntityConfiguration.cs</span>
<div class="code-block">using xDataService.Providers;
using xBotService.Models.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace xBotService.Configurations.Entities
{
public class XBotUserEntityConfiguration : XBaseEntityTypeConfiguration&lt;XBotUser, int&gt;
{
public override void Configure(EntityTypeBuilder&lt;XBotUser&gt; builder)
{
builder.ToTable("BotUsers");
// Example Indexes
builder.HasIndex(x =&gt; x.UserId);
builder.HasIndex(x =&gt; x.BotUserName);
}
}
}</div>
</div>
<!-- Section 2: Constants & Interfaces -->
<div class="section">
<div class="section-header">
<div class="section-number">۲</div>
<div class="section-title">
<h3>ثابت‌ها و اینترفیس‌ها (Constants & Interfaces)</h3>
</div>
</div>
<span class="file-path">Modules/xBotService/Constants/XBotServiceConstants.cs</span>
<div class="code-block">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";
}
}</div>
<span class="file-path">Modules/xBotService/Interfaces/Entities/IXBotUserRepository.cs</span>
<div class="code-block">using xDataService.Interfaces;
using xBotService.Models.Entities;
namespace xBotService.Interfaces.Entities
{
public interface IXBotUserRepository : IXBaseRepository&lt;XBotUser, int&gt;
{
}
}</div>
<span class="file-path">Modules/xBotService/Interfaces/Entities/IXBotUserKeyGenerator.cs</span>
<div class="code-block">using xDataService.Interfaces;
using xBotService.Models.Entities;
namespace xBotService.Interfaces.Entities
{
public interface IXBotUserKeyGenerator : IXKeyGenerator&lt;XBotUser, int&gt;
{
}
}</div>
<span class="file-path">Modules/xBotService/Interfaces/Entities/IXBotUserRepositoryEvents.cs</span>
<div class="code-block">using xDataService.Interfaces;
using xBotService.Models.Entities;
namespace xBotService.Interfaces.Entities
{
public interface IXBotUserRepositoryEvents : IXBaseRepositoryEvents&lt;XBotUser&gt;
{
}
}</div>
<span class="file-path">Modules/xBotService/Interfaces/Dtos/IXBotUserRepositoryService.cs</span>
<div class="code-block">using xDataService.Interfaces;
using xBotService.Models.Dtos;
using xBotService.Models.Entities;
namespace xBotService.Interfaces.Dtos
{
public interface IXBotUserRepositoryService : IXBaseRepositoryService&lt;XBotUser, XBotUserDto, int&gt;
{
}
}</div>
</div>
<!-- Section 3: Providers (Implementation) -->
<div class="section">
<div class="section-header">
<div class="section-number">۳</div>
<div class="section-title">
<h3>پیاده‌سازی ارائه‌دهندگان (Providers Implementation)</h3>
</div>
</div>
<span class="file-path">Modules/xBotService/Providers/Entities/XBotUserEFRepository.cs</span>
<div class="code-block">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&lt;TContext&gt; : XBaseEFRepository&lt;XBotUser, int, TContext&gt;, IXBotUserRepository
where TContext : XDbContext
{
public XBotUserEFRepository(
IXUnitOfWorks&lt;TContext&gt; unitOfWorks,
XDataServiceConfiguration configuration,
IXBotUserKeyGenerator keyGenerator = null,
IXBotUserRepositoryEvents baseRepositoryEvents = null
) : base(unitOfWorks, configuration, keyGenerator, baseRepositoryEvents)
{
}
}
}</div>
<span class="file-path">Modules/xBotService/Providers/Entities/XBotUserKeyGenerator.cs</span>
<div class="code-block">using xDataService.Providers;
using xBotService.Models.Entities;
using xBotService.Interfaces.Entities;
namespace xBotService.Providers.Entities
{
public class XBotUserKeyGenerator : XIntKeyGenerator&lt;XBotUser&gt;, IXBotUserKeyGenerator
{
}
}</div>
<span class="file-path">Modules/xBotService/Providers/Entities/XBotUserRepositoryEvents.cs</span>
<div class="code-block">using xDataService.Providers;
using xBotService.Models.Entities;
using xBotService.Interfaces.Entities;
namespace xBotService.Providers.Entities
{
public class XBotUserRepositoryEvents : XBaseRepositoryEvents&lt;XBotUser&gt;, IXBotUserRepositoryEvents
{
}
}</div>
<span class="file-path">Modules/xBotService/Providers/Dtos/XBotUserRepositoryService.cs</span>
<div class="code-block">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&lt;XBotUser, XBotUserDto, int&gt;, IXBotUserRepositoryService
{
public XBotUserRepositoryService(
IXBotUserRepository repository,
IEnumerable&lt;Profile&gt; mapperProfiles = null
) : base(repository, mapperProfiles)
{
}
}
}</div>
</div>
<!-- Section 4: GraphQL Support -->
<div class="section">
<div class="section-header">
<div class="section-number">۴</div>
<div class="section-title">
<h3>پشتیبانی گراف‌کیو‌ال (GraphQL Support)</h3>
</div>
</div>
<span class="file-path">Modules/xBotService/Providers/GraphQL/XBotUserGraphType.cs</span>
<div class="code-block">using xDataService.GraphQL;
using xBotService.Models.Entities;
namespace xBotService.Providers.GraphQL
{
public class XBotUserGraphType : XBaseGraphObjectType&lt;XBotUser, int&gt;
{
public XBotUserGraphType() : base()
{
Field(x =&gt; x.UserId);
Field(x =&gt; x.BotUserName);
}
}
}</div>
<span class="file-path">Modules/xBotService/Providers/GraphQL/XBotUserGraphQLTypeHelper.cs</span>
<div class="code-block">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&lt;XBotUser, int&gt;, IXBotUserGraphQLTypeHelper
{
public XBotUserGraphQLTypeHelper(XDataServiceConfiguration configuration)
: base(configuration)
{
}
public override string GetInQueryCollectionName()
{
return XBotServiceConstants.XBotUserCollectionName;
}
public override string GetInQuerySingleName()
{
return XBotServiceConstants.XBotUserSingleName;
}
}
}</div>
<span class="file-path">Modules/xBotService/Providers/GraphQL/XBotUserGraphQuery.cs</span>
<div class="code-block">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&lt;XBotUser, int, XBotUserGraphType, IntGraphType&gt;
{
public XBotUserGraphQuery(
XDataServiceConfiguration configuration,
IXBotUserRepository repository,
IXBotUserGraphQLTypeHelper helper
) : base(configuration, repository, helper)
{
}
}
}</div>
<span class="file-path">Modules/xBotService/Providers/GraphQL/XBotUserGraphSchema.cs</span>
<div class="code-block">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));
}
}
}</div>
</div>
<!-- Section 5: DI Helper Extension -->
<div class="section">
<div class="section-header">
<div class="section-number">۵</div>
<div class="section-title">
<h3>ثبت وابستگی‌ها (Dependency Injection)</h3>
</div>
</div>
<span class="file-path">Modules/xBotService/DI/XDIHelperExtension.cs</span>
<div class="code-block">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
{
/// &lt;summary&gt;
/// Register XBotService
/// &lt;/summary&gt;
public static void AddXBotService(
this IServiceCollection services,
XRepositoryType repositoryType,
ServiceLifetime lifeTime = ServiceLifetime.Scoped
)
{
AddBotService(
contextType: null,
services: services,
lifeTime: lifeTime,
repositoryType: repositoryType
);
}
/// &lt;summary&gt;
/// Register XBotService with Context
/// &lt;/summary&gt;
public static void AddXBotService&lt;TContext&gt;(
this IServiceCollection services,
XRepositoryType repositoryType,
ServiceLifetime lifeTime = ServiceLifetime.Scoped
) where TContext : XDbContext
{
AddBotService(
services: services,
lifeTime: lifeTime,
contextType: typeof(TContext),
repositoryType: repositoryType
);
}
/// &lt;summary&gt;
/// Use XBotService Middleware
/// &lt;/summary&gt;
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} =&gt; {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&lt;XRepositoryDescriptor&gt;(
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 ...");
}
}
}</div>
</div>
<!-- Section 6: Helpers -->
<div class="section">
<div class="section-header">
<div class="section-number">۶</div>
<div class="section-title">
<h3>کمک‌کننده‌های توصیف‌گر (Descriptor Helpers)</h3>
</div>
</div>
<span class="file-path">Modules/xBotService/Helpers/XBotServiceHelper.cs</span>
<div class="code-block">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&lt;
XBotUser,
int,
IXBotUserKeyGenerator,
XBotUserKeyGenerator,
IXBotUserRepositoryEvents,
XBotUserRepositoryEvents,
XBotUserGraphType,
IntGraphType,
XBotUserGraphQuery,
IXBotUserGraphQLTypeHelper,
XBotUserGraphQLTypeHelper,
XBotUserGraphSchema,
IXBotUserRepository,
XBotUserEFRepository&lt;TContext&gt;,
TContext
&gt; GetXBotUserEFRepositoryDescriptor&lt;TContext&gt;()
where TContext : XDbContext
{
var result = new XEFRepositoryDescriptor&lt;
XBotUser,
int,
IXBotUserKeyGenerator,
XBotUserKeyGenerator,
IXBotUserRepositoryEvents,
XBotUserRepositoryEvents,
XBotUserGraphType,
IntGraphType,
XBotUserGraphQuery,
IXBotUserGraphQLTypeHelper,
XBotUserGraphQLTypeHelper,
XBotUserGraphSchema,
IXBotUserRepository,
XBotUserEFRepository&lt;TContext&gt;,
TContext
&gt;();
return result;
}
}
}</div>
</div>
<div class="success-box">
<strong>نتیجه‌گیری:</strong><br>
با افزودن فایل‌های فوق، ماژول <code>xBotService</code> اکنون دارای چرخه حیات کامل (CRUD)، پشتیبانی از GraphQL، Event Handling و قابلیت تزریق وابستگی خودکار مطابق با استانداردهای <code>xDataService</code> است. این ساختار امکان توسعه سریع‌تر و نگهداری آسان‌تر را فراهم می‌آورد.
</div>
</div>
<footer class="document-footer">
<div class="footer-brand">طراحی و توسعه: هادی خزاعی اصل</div>
<div>شرکت رایان پرداز کاوش &nbsp;|&nbsp; واحد معماری نرم‌افزار</div>
</footer>
</div>
</body>
</html>