Files
xDataService/Builders/XRepositoryDescriptorBuilder.cs
T

196 lines
8.8 KiB
C#

using GraphQL.Types;
using xDataService.Db;
using xDataService.GraphQL;
using xDataService.Interfaces;
using xDataService.Models;
using xDataService.Providers;
using xModels.Base;
namespace xDataService.Builders
{
/// <summary>
/// Fluent Builder for constructing XRepositoryDescriptor instances.
/// Replaces the telescoping generic constructors (12-17 type parameters)
/// with a readable, chainable API.
/// </summary>
/// <typeparam name="TEntity">The entity type</typeparam>
/// <typeparam name="TKey">The entity key type</typeparam>
/// <example>
/// var descriptor = XRepositoryDescriptor.For&lt;XAiMessage, Guid&gt;()
/// .WithKeyGenerator&lt;IXAiMessageKeyGenerator, XAiMessageKeyGenerator&gt;()
/// .WithEvents&lt;IXAiMessageRepositoryEvents, XAiMessageRepositoryEvents&gt;()
/// .WithGraphQL&lt;XAiMessageGraphType, GuidGraphType, XAiMessageGraphQuery,
/// IXAiMessageGraphQLTypeHelper, XAiMessageGraphQLTypeHelper, XAiMessageGraphSchema&gt;()
/// .WithEFRepository&lt;XAiApiDbContext, IXAiMessageRepository, XAiMessageEFRepository&lt;XAiApiDbContext&gt;&gt;()
/// .WithSeeder&lt;IXAiMessageSeeder, XAiMessageSeeder&gt;()
/// .Build();
/// </example>
public sealed class XRepositoryDescriptorBuilder<TEntity, TKey>
where TEntity : XBaseEntity<TKey>
{
private readonly XRepositoryDescriptor _descriptor;
private XRepositoryDescriptorBuilder()
{
_descriptor = new XRepositoryDescriptor
{
Entity = typeof(TEntity),
Key = typeof(TKey)
};
}
/// <summary>
/// Creates a new builder instance for the specified entity and key types.
/// </summary>
public static XRepositoryDescriptorBuilder<TEntity, TKey> Create()
{
return new XRepositoryDescriptorBuilder<TEntity, TKey>();
}
//
#region KeyGenerator
/// <summary>
/// Configures the Key Generator service for the entity.
/// </summary>
/// <typeparam name="TKeyGeneratorService">The key generator interface</typeparam>
/// <typeparam name="TKeyGeneratorImplementation">The key generator implementation</typeparam>
public XRepositoryDescriptorBuilder<TEntity, TKey> WithKeyGenerator<TKeyGeneratorService, TKeyGeneratorImplementation>()
where TKeyGeneratorService : IXKeyGenerator<TEntity, TKey>
where TKeyGeneratorImplementation : XKeyGenerator<TEntity, TKey>
{
_descriptor.KeyGeneratorService = typeof(TKeyGeneratorService);
_descriptor.KeyGeneratorImplementation = typeof(TKeyGeneratorImplementation);
return this;
}
#endregion
//
#region Events
/// <summary>
/// Configures the Repository Events service for the entity.
/// </summary>
/// <typeparam name="TEventsService">The events interface</typeparam>
/// <typeparam name="TEventsImplementation">The events implementation</typeparam>
public XRepositoryDescriptorBuilder<TEntity, TKey> WithEvents<TEventsService, TEventsImplementation>()
where TEventsService : IXBaseRepositoryEvents<TEntity>
where TEventsImplementation : XBaseRepositoryEvents<TEntity>
{
_descriptor.EventsService = typeof(TEventsService);
_descriptor.EventsImplementation = typeof(TEventsImplementation);
return this;
}
#endregion
//
#region GraphQL
/// <summary>
/// Configures the full GraphQL stack for the entity.
/// </summary>
/// <typeparam name="TGraph">The GraphQL object type</typeparam>
/// <typeparam name="TGraphKey">The GraphQL key type</typeparam>
/// <typeparam name="TGraphQuery">The GraphQL query type</typeparam>
/// <typeparam name="TGraphTypeHelperService">The GraphQL type helper interface</typeparam>
/// <typeparam name="TGraphTypeHelperImplementation">The GraphQL type helper implementation</typeparam>
/// <typeparam name="TGraphSchema">The GraphQL schema</typeparam>
public XRepositoryDescriptorBuilder<TEntity, TKey> WithGraphQL<
TGraph,
TGraphKey,
TGraphQuery,
TGraphTypeHelperService,
TGraphTypeHelperImplementation,
TGraphSchema>()
where TGraphSchema : Schema
where TGraphKey : IGraphType
where TGraph : XBaseGraphObjectType<TEntity, TKey>
where TGraphTypeHelperService : IXBaseGraphQLTypeHelper<TEntity, TKey>
where TGraphQuery : XBaseGraphQLQuery<TEntity, TKey, TGraph, TGraphKey>
where TGraphTypeHelperImplementation : XBaseGraphQLTypeHelper<TEntity, TKey>
{
_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
/// <summary>
/// Configures an Entity Framework Core repository for the entity.
/// </summary>
/// <typeparam name="TContext">The EF DbContext</typeparam>
/// <typeparam name="TRepositoryService">The repository interface</typeparam>
/// <typeparam name="TRepositoryImplementation">The repository implementation</typeparam>
public XRepositoryDescriptorBuilder<TEntity, TKey> WithEFRepository<TContext, TRepositoryService, TRepositoryImplementation>()
where TContext : XDbContext
where TRepositoryService : IXBaseRepository<TEntity, TKey>
where TRepositoryImplementation : XBaseEFRepository<TEntity, TKey, TContext>
{
_descriptor.Context = typeof(TContext);
_descriptor.RepositoryService = typeof(TRepositoryService);
_descriptor.RepositoryImplementation = typeof(TRepositoryImplementation);
return this;
}
/// <summary>
/// Configures a MongoDB repository for the entity.
/// </summary>
/// <typeparam name="TRepositoryService">The repository interface</typeparam>
/// <typeparam name="TRepositoryImplementation">The repository implementation</typeparam>
public XRepositoryDescriptorBuilder<TEntity, TKey> WithMongoRepository<TRepositoryService, TRepositoryImplementation>()
where TRepositoryService : IXBaseRepository<TEntity, TKey>
where TRepositoryImplementation : XBaseMongoRepository<TEntity, TKey>
{
_descriptor.RepositoryService = typeof(TRepositoryService);
_descriptor.RepositoryImplementation = typeof(TRepositoryImplementation);
return this;
}
/// <summary>
/// Configures an In-Memory repository for the entity.
/// </summary>
/// <typeparam name="TRepositoryService">The repository interface</typeparam>
/// <typeparam name="TRepositoryImplementation">The repository implementation</typeparam>
public XRepositoryDescriptorBuilder<TEntity, TKey> WithInMemoryRepository<TRepositoryService, TRepositoryImplementation>()
where TRepositoryService : IXBaseRepository<TEntity, TKey>
where TRepositoryImplementation : XBaseInMemoryRepository<TEntity, TKey>
{
_descriptor.RepositoryService = typeof(TRepositoryService);
_descriptor.RepositoryImplementation = typeof(TRepositoryImplementation);
return this;
}
#endregion
//
#region Seeder
/// <summary>
/// Configures the database seeder for the entity.
/// </summary>
/// <typeparam name="TSeederService">The seeder interface</typeparam>
/// <typeparam name="TSeederImplementation">The seeder implementation</typeparam>
public XRepositoryDescriptorBuilder<TEntity, TKey> WithSeeder<TSeederService, TSeederImplementation>()
where TSeederService : IXBaseDbSeeder<TEntity, TKey>
where TSeederImplementation : XBaseDbSeeder<TEntity, TKey>
{
_descriptor.SeederService = typeof(TSeederService);
_descriptor.SeederImplementation = typeof(TSeederImplementation);
return this;
}
#endregion
//
#region Build
/// <summary>
/// Builds and returns the configured XRepositoryDescriptor.
/// </summary>
/// <returns>The fully configured descriptor</returns>
public XRepositoryDescriptor Build()
{
return _descriptor;
}
#endregion
}
}