From 2b17be057516678dea1187891c310d5563b1a189 Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Sat, 8 Aug 2026 18:07:55 +0330 Subject: [PATCH] Add Repository Descriptor Builder ... --- Builders/XRepositoryDescriptorBuilder.cs | 196 +++++++++++++++++++++++ 1 file changed, 196 insertions(+) create mode 100644 Builders/XRepositoryDescriptorBuilder.cs diff --git a/Builders/XRepositoryDescriptorBuilder.cs b/Builders/XRepositoryDescriptorBuilder.cs new file mode 100644 index 0000000..fadd8f4 --- /dev/null +++ b/Builders/XRepositoryDescriptorBuilder.cs @@ -0,0 +1,196 @@ +using GraphQL.Types; +using xDataService.Db; +using xDataService.GraphQL; +using xDataService.Interfaces; +using xDataService.Models; +using xDataService.Providers; +using xModels.Base; + +namespace xDataService.Builders +{ + /// + /// Fluent Builder for constructing XRepositoryDescriptor instances. + /// Replaces the telescoping generic constructors (12-17 type parameters) + /// with a readable, chainable API. + /// + /// The entity type + /// The entity key type + /// + /// var descriptor = XRepositoryDescriptor.For<XAiMessage, Guid>() + /// .WithKeyGenerator<IXAiMessageKeyGenerator, XAiMessageKeyGenerator>() + /// .WithEvents<IXAiMessageRepositoryEvents, XAiMessageRepositoryEvents>() + /// .WithGraphQL<XAiMessageGraphType, GuidGraphType, XAiMessageGraphQuery, + /// IXAiMessageGraphQLTypeHelper, XAiMessageGraphQLTypeHelper, XAiMessageGraphSchema>() + /// .WithEFRepository<XAiApiDbContext, IXAiMessageRepository, XAiMessageEFRepository<XAiApiDbContext>>() + /// .WithSeeder<IXAiMessageSeeder, XAiMessageSeeder>() + /// .Build(); + /// + public sealed class XRepositoryDescriptorBuilder + where TEntity : XBaseEntity + { + private readonly XRepositoryDescriptor _descriptor; + + private XRepositoryDescriptorBuilder() + { + _descriptor = new XRepositoryDescriptor + { + Entity = typeof(TEntity), + Key = typeof(TKey) + }; + } + + /// + /// Creates a new builder instance for the specified entity and key types. + /// + public static XRepositoryDescriptorBuilder Create() + { + return new XRepositoryDescriptorBuilder(); + } + + // + #region KeyGenerator + /// + /// Configures the Key Generator service for the entity. + /// + /// The key generator interface + /// The key generator implementation + public XRepositoryDescriptorBuilder WithKeyGenerator() + where TKeyGeneratorService : IXKeyGenerator + where TKeyGeneratorImplementation : XKeyGenerator + { + _descriptor.KeyGeneratorService = typeof(TKeyGeneratorService); + _descriptor.KeyGeneratorImplementation = typeof(TKeyGeneratorImplementation); + return this; + } + #endregion + + // + #region Events + /// + /// Configures the Repository Events service for the entity. + /// + /// The events interface + /// The events implementation + public XRepositoryDescriptorBuilder WithEvents() + where TEventsService : IXBaseRepositoryEvents + where TEventsImplementation : XBaseRepositoryEvents + { + _descriptor.EventsService = typeof(TEventsService); + _descriptor.EventsImplementation = typeof(TEventsImplementation); + return this; + } + #endregion + + // + #region GraphQL + /// + /// Configures the full GraphQL stack for the entity. + /// + /// The GraphQL object type + /// The GraphQL key type + /// The GraphQL query type + /// The GraphQL type helper interface + /// The GraphQL type helper implementation + /// The GraphQL schema + public XRepositoryDescriptorBuilder WithGraphQL< + TGraph, + TGraphKey, + TGraphQuery, + TGraphTypeHelperService, + TGraphTypeHelperImplementation, + TGraphSchema>() + where TGraphSchema : Schema + where TGraphKey : IGraphType + where TGraph : XBaseGraphObjectType + where TGraphTypeHelperService : IXBaseGraphQLTypeHelper + where TGraphQuery : XBaseGraphQLQuery + where TGraphTypeHelperImplementation : XBaseGraphQLTypeHelper + { + _descriptor.GraphType = typeof(TGraph); + _descriptor.GraphTypeKey = typeof(TGraphKey); + _descriptor.GraphQuery = typeof(TGraphQuery); + _descriptor.GraphSchema = typeof(TGraphSchema); + _descriptor.GraphTypeHelperService = typeof(TGraphTypeHelperService); + _descriptor.GraphTypeHelperImplementation = typeof(TGraphTypeHelperImplementation); + return this; + } + #endregion + + // + #region Repository + /// + /// Configures an Entity Framework Core repository for the entity. + /// + /// The EF DbContext + /// The repository interface + /// The repository implementation + public XRepositoryDescriptorBuilder WithEFRepository() + where TContext : XDbContext + where TRepositoryService : IXBaseRepository + where TRepositoryImplementation : XBaseEFRepository + { + _descriptor.Context = typeof(TContext); + _descriptor.RepositoryService = typeof(TRepositoryService); + _descriptor.RepositoryImplementation = typeof(TRepositoryImplementation); + return this; + } + + /// + /// Configures a MongoDB repository for the entity. + /// + /// The repository interface + /// The repository implementation + public XRepositoryDescriptorBuilder WithMongoRepository() + where TRepositoryService : IXBaseRepository + where TRepositoryImplementation : XBaseMongoRepository + { + _descriptor.RepositoryService = typeof(TRepositoryService); + _descriptor.RepositoryImplementation = typeof(TRepositoryImplementation); + return this; + } + + /// + /// Configures an In-Memory repository for the entity. + /// + /// The repository interface + /// The repository implementation + public XRepositoryDescriptorBuilder WithInMemoryRepository() + where TRepositoryService : IXBaseRepository + where TRepositoryImplementation : XBaseInMemoryRepository + { + _descriptor.RepositoryService = typeof(TRepositoryService); + _descriptor.RepositoryImplementation = typeof(TRepositoryImplementation); + return this; + } + #endregion + + // + #region Seeder + /// + /// Configures the database seeder for the entity. + /// + /// The seeder interface + /// The seeder implementation + public XRepositoryDescriptorBuilder WithSeeder() + where TSeederService : IXBaseDbSeeder + where TSeederImplementation : XBaseDbSeeder + { + _descriptor.SeederService = typeof(TSeederService); + _descriptor.SeederImplementation = typeof(TSeederImplementation); + return this; + } + #endregion + + // + #region Build + /// + /// Builds and returns the configured XRepositoryDescriptor. + /// + /// The fully configured descriptor + public XRepositoryDescriptor Build() + { + return _descriptor; + } + #endregion + } +} \ No newline at end of file