using xModels.Base;
using GraphQL.Types;
using xDataService.Db;
using xDataService.Models;
using xDataService.GraphQL;
using xDataService.Providers;
using xDataService.Interfaces;
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
}
}