تحلیل معماری:
با بررسی دقیق
xTagService، الگوی حاکم بر سیستم شامل موارد زیر است:
- استفاده از XBaseIntIDEntity برای موجودیتها.
- پیکربندی جداگانه EF در کلاسهای Configuration.
- استفاده از XRepositoryDescriptor برای ثبت سرویسها در DI.
- پشتیبانی کامل از GraphQL و SignalR Hubs.
- جداسازی لایههای Dto و Entity با استفاده از AutoMapper.
کدهای زیر تمامی شکافهای موجود در
xBotService را پر کرده و آن را به یک ماژول کامل تبدیل میکنند.
Modules/xBotService/Models/Entities/XBotUser.cs
using xModels.Base;
namespace xBotService.Models.Entities
{
/// <summary>
/// Bot User / System User Relation ...
/// </summary>
public class XBotUser : XBaseIntIDEntity
{
/// <summary>
/// User Id ...
/// </summary>
public string UserId { get; set; }
/// <summary>
/// Chat Bot User Name ...
/// </summary>
public string BotUserName { get; set; }
}
}
Modules/xBotService/Models/Dtos/XBotUserDto.cs
using xModels.Dtos;
namespace xBotService.Models.Dtos
{
/// <summary>
/// Bot User / System User Relation Dto ...
/// </summary>
public class XBotUserDto : XBaseIntIDEntityDto
{
/// <summary>
/// User Id ...
/// </summary>
public string UserId { get; set; }
/// <summary>
/// Chat Bot User Name ...
/// </summary>
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<XBotUser, int>
{
public override void Configure(EntityTypeBuilder<XBotUser> builder)
{
builder.ToTable("BotUsers");
// Example Indexes
builder.HasIndex(x => x.UserId);
builder.HasIndex(x => x.BotUserName);
}
}
}
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<XBotUser, int>
{
}
}
Modules/xBotService/Interfaces/Entities/IXBotUserKeyGenerator.cs
using xDataService.Interfaces;
using xBotService.Models.Entities;
namespace xBotService.Interfaces.Entities
{
public interface IXBotUserKeyGenerator : IXKeyGenerator<XBotUser, int>
{
}
}
Modules/xBotService/Interfaces/Entities/IXBotUserRepositoryEvents.cs
using xDataService.Interfaces;
using xBotService.Models.Entities;
namespace xBotService.Interfaces.Entities
{
public interface IXBotUserRepositoryEvents : IXBaseRepositoryEvents<XBotUser>
{
}
}
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<XBotUser, XBotUserDto, int>
{
}
}
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<TContext> : XBaseEFRepository<XBotUser, int, TContext>, IXBotUserRepository
where TContext : XDbContext
{
public XBotUserEFRepository(
IXUnitOfWorks<TContext> 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<XBotUser>, 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<XBotUser>, 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<XBotUser, XBotUserDto, int>, IXBotUserRepositoryService
{
public XBotUserRepositoryService(
IXBotUserRepository repository,
IEnumerable<Profile> mapperProfiles = null
) : base(repository, mapperProfiles)
{
}
}
}
Modules/xBotService/Providers/GraphQL/XBotUserGraphType.cs
using xDataService.GraphQL;
using xBotService.Models.Entities;
namespace xBotService.Providers.GraphQL
{
public class XBotUserGraphType : XBaseGraphObjectType<XBotUser, int>
{
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<XBotUser, int>, 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<XBotUser, int, XBotUserGraphType, IntGraphType>
{
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));
}
}
}
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
{
/// <summary>
/// Register XBotService
/// </summary>
public static void AddXBotService(
this IServiceCollection services,
XRepositoryType repositoryType,
ServiceLifetime lifeTime = ServiceLifetime.Scoped
)
{
AddBotService(
contextType: null,
services: services,
lifeTime: lifeTime,
repositoryType: repositoryType
);
}
/// <summary>
/// Register XBotService with Context
/// </summary>
public static void AddXBotService<TContext>(
this IServiceCollection services,
XRepositoryType repositoryType,
ServiceLifetime lifeTime = ServiceLifetime.Scoped
) where TContext : XDbContext
{
AddBotService(
services: services,
lifeTime: lifeTime,
contextType: typeof(TContext),
repositoryType: repositoryType
);
}
/// <summary>
/// Use XBotService Middleware
/// </summary>
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<XRepositoryDescriptor>(
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 ...");
}
}
}
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>,
TContext
> GetXBotUserEFRepositoryDescriptor<TContext>()
where TContext : XDbContext
{
var result = new XEFRepositoryDescriptor<
XBotUser,
int,
IXBotUserKeyGenerator,
XBotUserKeyGenerator,
IXBotUserRepositoryEvents,
XBotUserRepositoryEvents,
XBotUserGraphType,
IntGraphType,
XBotUserGraphQuery,
IXBotUserGraphQLTypeHelper,
XBotUserGraphQLTypeHelper,
XBotUserGraphSchema,
IXBotUserRepository,
XBotUserEFRepository<TContext>,
TContext
>();
return result;
}
}
}
نتیجهگیری:
با افزودن فایلهای فوق، ماژول xBotService اکنون دارای چرخه حیات کامل (CRUD)، پشتیبانی از GraphQL، Event Handling و قابلیت تزریق وابستگی خودکار مطابق با استانداردهای xDataService است. این ساختار امکان توسعه سریعتر و نگهداری آسانتر را فراهم میآورد.