Files
xDataService/Helpers/XDataServiceHelper.cs
2026-06-01 15:49:03 +03:30

1556 lines
58 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using GraphQL;
using GraphQL.Server;
using GraphQL.Server.Ui.Playground;
using GraphQL.Server.Ui.Voyager;
using GraphQL.Types;
using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Conventions;
using MySql.EntityFrameworkCore.Extensions;
using xCommons.Extensions;
using xDataService.Constants;
using xDataService.Db;
using xDataService.Extensions;
using xDataService.GraphQL;
using xDataService.Interfaces;
using xDataService.Models;
using xDataService.Providers;
using xExceptions.Constants;
using xModels.Base;
namespace xDataService.Helpers
{
public static class XDataServiceHelper
{
/// <summary>
/// Register DbContext ...
/// </summary>
/// <param name="source"></param>
/// <param name="databaseDescriptor"></param>
/// <param name="lifetime"></param>
/// <typeparam name="TContext"></typeparam>
public static void AddXDbContext<TContext>(
IServiceCollection source,
XDatabaseDescriptor databaseDescriptor,
ServiceLifetime lifetime = ServiceLifetime.Scoped
)
where TContext : XDbContext
{
//
var isValid =
databaseDescriptor.IsValid() &&
databaseDescriptor.HasProvider();
if (!isValid)
{
XException.InvalidConfiguration.Throw();
}
//
switch (databaseDescriptor.Provider)
{
//
// MySQL ...
case XDbProviders.MySQL:
//
source.AddEntityFrameworkMySQL();
source.AddDbContext<TContext>(cfg =>
{
//
cfg.UseMySQL(
databaseDescriptor.ConnectionString,
databaseDescriptor.OptionsBuilder
);
}, lifetime);
break;
//
// SQLite ...
case XDbProviders.SQLite:
//
source.AddEntityFrameworkSqlite();
source.AddDbContext<TContext>(cfg =>
{
//
cfg.UseSqlite(
databaseDescriptor.ConnectionString,
databaseDescriptor.OptionsBuilder
);
}, lifetime);
break;
//
// Oracle ...
case XDbProviders.Oracle:
//
source.AddEntityFrameworkOracle();
source.AddDbContext<TContext>(cfg =>
{
//
cfg.UseOracle(
databaseDescriptor.ConnectionString,
databaseDescriptor.OptionsBuilder
);
}, lifetime);
break;
//
// SQLServer ...
case XDbProviders.SQLServer:
//
source.AddEntityFrameworkSqlServer();
source.AddDbContext<TContext>(cfg =>
{
//
cfg.UseSqlServer(
databaseDescriptor.ConnectionString,
databaseDescriptor.OptionsBuilder
);
}, lifetime);
break;
//
// Mongo DB ...
case XDbProviders.MongoDB:
//
#region XBaseEntity Mappers ...
//
// Map ID as BsonId here ...
BsonClassMap.RegisterClassMap<XBaseEntity<Guid>>(cm =>
{
//
cm.AutoMap();
cm.MapIdMember(c => c.Id);
});
BsonClassMap.RegisterClassMap<XBaseEntity<int>>(cm =>
{
//
cm.AutoMap();
cm.MapIdMember(c => c.Id);
});
BsonClassMap.RegisterClassMap<XBaseEntity<long>>(cm =>
{
//
cm.AutoMap();
cm.MapIdMember(c => c.Id);
});
BsonClassMap.RegisterClassMap<XBaseEntity<string>>(cm =>
{
//
cm.AutoMap();
cm.MapIdMember(c => c.Id);
});
#endregion
//
// Check Has Convention Packs ...
isValid = databaseDescriptor.HasConventionPacks();
if (isValid)
{
//
ConventionRegistry.Register(
nameof(
databaseDescriptor.ConventionPacks
),
databaseDescriptor.ConventionPacks,
type => !type.FullName
.IsNullOrEmpty()
);
}
break;
}
//
isValid =
databaseDescriptor.Provider == XDbProviders.MySQL ||
databaseDescriptor.Provider == XDbProviders.SQLite ||
databaseDescriptor.Provider == XDbProviders.Oracle ||
databaseDescriptor.Provider == XDbProviders.SQLServer;
if (isValid)
{
//
AddXUnitOfWorks<TContext>(
source,
lifetime
);
}
}
//
#region Repository ...
/// <summary>
/// Register Repository ...
/// </summary>
/// <param name="source"></param>
/// <param name="repositoryDescriptor"></param>
/// <param name="lifetime"></param>
/// <typeparam name="TEntity"></typeparam>
/// <typeparam name="TKey"></typeparam>
/// <returns></returns>
public static void AddXRepository<TEntity, TKey>(
IServiceCollection source,
XRepositoryDescriptor<TEntity, TKey> repositoryDescriptor,
ServiceLifetime lifetime = ServiceLifetime.Scoped
)
where TEntity : XBaseEntity<TKey>
{
//
var isValid = repositoryDescriptor.IsValid();
if (!isValid)
{
return;
}
}
/// <summary>
/// Register Repository ...
/// </summary>
/// <param name="source"></param>
/// <param name="repositoryDescriptor"></param>
/// <param name="lifetime"></param>
/// <typeparam name="TEntity"></typeparam>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TKeyGeneratorService"></typeparam>
/// <typeparam name="TKeyGeneratorImplementation"></typeparam>
/// <returns></returns>
public static void AddXRepository<
TEntity,
TKey,
TKeyGeneratorService,
TKeyGeneratorImplementation
>(
IServiceCollection source,
XRepositoryDescriptor<TEntity, TKey, TKeyGeneratorService, TKeyGeneratorImplementation> repositoryDescriptor,
ServiceLifetime lifetime = ServiceLifetime.Scoped
)
where TEntity : XBaseEntity<TKey>
where TKeyGeneratorService : IXKeyGenerator<TEntity, TKey>
where TKeyGeneratorImplementation : XKeyGenerator<TEntity, TKey>
{
//
var isValid = repositoryDescriptor.IsValid();
if (!isValid)
{
return;
}
//
// Register KeyGenerator ...
isValid = repositoryDescriptor.HasKeyGenerator();
if (isValid)
{
//
AddXKeyGenerator<TEntity, TKey, TKeyGeneratorService, TKeyGeneratorImplementation>(
source,
repositoryDescriptor,
lifetime
);
}
}
/// <summary>
/// Register Repository ...
/// </summary>
/// <param name="source"></param>
/// <param name="repositoryDescriptor"></param>
/// <param name="lifetime"></param>
/// <typeparam name="TEntity"></typeparam>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TKeyGeneratorService"></typeparam>
/// <typeparam name="TKeyGeneratorImplementation"></typeparam>
/// <typeparam name="TEventsService"></typeparam>
/// <typeparam name="TEventsImplementation"></typeparam>
/// <returns></returns>
public static void AddXRepository<
TEntity,
TKey,
TKeyGeneratorService,
TKeyGeneratorImplementation,
TEventsService,
TEventsImplementation
>(
IServiceCollection source,
XRepositoryDescriptor<TEntity, TKey, TKeyGeneratorService, TKeyGeneratorImplementation, TEventsService, TEventsImplementation> repositoryDescriptor,
ServiceLifetime lifetime = ServiceLifetime.Scoped
)
where TEntity : XBaseEntity<TKey>
where TEventsService : IXBaseRepositoryEvents<TEntity>
where TKeyGeneratorService : IXKeyGenerator<TEntity, TKey>
where TEventsImplementation : XBaseRepositoryEvents<TEntity>
where TKeyGeneratorImplementation : XKeyGenerator<TEntity, TKey>
{
//
var isValid = repositoryDescriptor.IsValid();
if (!isValid)
{
return;
}
//
// Register KeyGenerator ...
isValid = repositoryDescriptor.HasKeyGenerator();
if (isValid)
{
//
AddXKeyGenerator<TEntity, TKey, TKeyGeneratorService, TKeyGeneratorImplementation>(
source,
repositoryDescriptor,
lifetime
);
}
//
// Register Events ...
isValid = repositoryDescriptor.HasEvents();
if (isValid)
{
//
AddXEvents<TEntity, TKey, TEventsService, TEventsImplementation>(
source,
repositoryDescriptor,
lifetime
);
}
}
/// <summary>
/// Register Repository ...
/// </summary>
/// <param name="source"></param>
/// <param name="repositoryDescriptor"></param>
/// <param name="lifetime"></param>
/// <typeparam name="TEntity"></typeparam>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TKeyGeneratorService"></typeparam>
/// <typeparam name="TKeyGeneratorImplementation"></typeparam>
/// <typeparam name="TEventsService"></typeparam>
/// <typeparam name="TEventsImplementation"></typeparam>
/// <typeparam name="TGraph"></typeparam>
/// <typeparam name="TGraphKey"></typeparam>
/// <typeparam name="TGraphQuery"></typeparam>
/// <typeparam name="TGraphTypeHelperService"></typeparam>
/// <typeparam name="TGraphTypeHelperImplementation"></typeparam>
/// <typeparam name="TGraphSchema"></typeparam>
/// <returns></returns>
public static void AddXRepository<
TEntity,
TKey,
TKeyGeneratorService,
TKeyGeneratorImplementation,
TEventsService,
TEventsImplementation,
TGraph,
TGraphKey,
TGraphQuery,
TGraphTypeHelperService,
TGraphTypeHelperImplementation,
TGraphSchema
>(
IServiceCollection source,
XRepositoryDescriptor<TEntity, TKey, TKeyGeneratorService, TKeyGeneratorImplementation, TEventsService, TEventsImplementation, TGraph, TGraphKey, TGraphQuery, TGraphTypeHelperService, TGraphTypeHelperImplementation, TGraphSchema> repositoryDescriptor,
ServiceLifetime lifetime = ServiceLifetime.Scoped
)
where TGraphSchema : Schema
where TGraphKey : IGraphType
where TEntity : XBaseEntity<TKey>
where TGraph : XBaseGraphObjectType<TEntity, TKey>
where TEventsService : IXBaseRepositoryEvents<TEntity>
where TKeyGeneratorService : IXKeyGenerator<TEntity, TKey>
where TEventsImplementation : XBaseRepositoryEvents<TEntity>
where TKeyGeneratorImplementation : XKeyGenerator<TEntity, TKey>
where TGraphTypeHelperService : IXBaseGraphQLTypeHelper<TEntity, TKey>
where TGraphQuery : XBaseGraphQLQuery<TEntity, TKey, TGraph, TGraphKey>
where TGraphTypeHelperImplementation : XBaseGraphQLTypeHelper<TEntity, TKey>
{
//
var isValid = repositoryDescriptor.IsValid();
if (!isValid)
{
return;
}
//
// Register KeyGenerator ...
isValid = repositoryDescriptor.HasKeyGenerator();
if (isValid)
{
//
AddXKeyGenerator<TEntity, TKey, TKeyGeneratorService, TKeyGeneratorImplementation>(
source,
repositoryDescriptor,
lifetime
);
}
//
// Register Events ...
isValid = repositoryDescriptor.HasEvents();
if (isValid)
{
//
AddXEvents<TEntity, TKey, TEventsService, TEventsImplementation>(
source,
repositoryDescriptor,
lifetime
);
}
//
// Register Graph ...
isValid = repositoryDescriptor.HasGraphSupport();
if (isValid)
{
//
AddXGraphQL<TEntity, TKey, TGraph, TGraphKey, TGraphQuery, TGraphTypeHelperService, TGraphTypeHelperImplementation, TGraphSchema>(
source,
repositoryDescriptor,
lifetime
);
}
}
/// <summary>
/// Register Repository ...
/// </summary>
/// <param name="source"></param>
/// <param name="repositoryDescriptor"></param>
/// <param name="lifetime"></param>
/// <typeparam name="TEntity"></typeparam>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TKeyGeneratorService"></typeparam>
/// <typeparam name="TKeyGeneratorImplementation"></typeparam>
/// <typeparam name="TEventsService"></typeparam>
/// <typeparam name="TEventsImplementation"></typeparam>
/// <typeparam name="TGraph"></typeparam>
/// <typeparam name="TGraphKey"></typeparam>
/// <typeparam name="TGraphQuery"></typeparam>
/// <typeparam name="TGraphTypeHelperService"></typeparam>
/// <typeparam name="TGraphTypeHelperImplementation"></typeparam>
/// <typeparam name="TGraphSchema"></typeparam>
/// <typeparam name="TRepositoryService"></typeparam>
/// <typeparam name="TRepositoryImplementation"></typeparam>
/// <returns></returns>
public static void AddXRepository<
TEntity,
TKey,
TKeyGeneratorService,
TKeyGeneratorImplementation,
TEventsService,
TEventsImplementation,
TGraph,
TGraphKey,
TGraphQuery,
TGraphTypeHelperService,
TGraphTypeHelperImplementation,
TGraphSchema,
TRepositoryService,
TRepositoryImplementation
>(
IServiceCollection source,
XInMemoryRepositoryDescriptor<TEntity, TKey, TKeyGeneratorService, TKeyGeneratorImplementation, TEventsService, TEventsImplementation, TGraph, TGraphKey, TGraphQuery, TGraphTypeHelperService, TGraphTypeHelperImplementation, TGraphSchema, TRepositoryService, TRepositoryImplementation> repositoryDescriptor,
ServiceLifetime lifetime = ServiceLifetime.Scoped
)
where TGraphSchema : Schema
where TGraphKey : IGraphType
where TEntity : XBaseEntity<TKey>
where TGraph : XBaseGraphObjectType<TEntity, TKey>
where TEventsService : IXBaseRepositoryEvents<TEntity>
where TKeyGeneratorService : IXKeyGenerator<TEntity, TKey>
where TRepositoryService : IXBaseRepository<TEntity, TKey>
where TEventsImplementation : XBaseRepositoryEvents<TEntity>
where TKeyGeneratorImplementation : XKeyGenerator<TEntity, TKey>
where TGraphTypeHelperService : IXBaseGraphQLTypeHelper<TEntity, TKey>
where TGraphQuery : XBaseGraphQLQuery<TEntity, TKey, TGraph, TGraphKey>
where TRepositoryImplementation : XBaseInMemoryRepository<TEntity, TKey>
where TGraphTypeHelperImplementation : XBaseGraphQLTypeHelper<TEntity, TKey>
{
//
var isValid = repositoryDescriptor.IsValid();
if (!isValid)
{
return;
}
//
// Register KeyGenerator ...
isValid = repositoryDescriptor.HasKeyGenerator();
if (isValid)
{
//
AddXKeyGenerator<TEntity, TKey, TKeyGeneratorService, TKeyGeneratorImplementation>(
source,
repositoryDescriptor,
lifetime
);
}
//
// Register Events ...
isValid = repositoryDescriptor.HasEvents();
if (isValid)
{
//
AddXEvents<TEntity, TKey, TEventsService, TEventsImplementation>(
source,
repositoryDescriptor,
lifetime
);
}
//
// Register Graph ...
isValid = repositoryDescriptor.HasGraphSupport();
if (isValid)
{
//
AddXGraphQL<TEntity, TKey, TGraph, TGraphKey, TGraphQuery, TGraphTypeHelperService, TGraphTypeHelperImplementation, TGraphSchema>(
source,
repositoryDescriptor,
lifetime
);
}
//
// Register Repository ...
isValid = repositoryDescriptor.HasRepository();
if (isValid)
{
//
source.Add(
new ServiceDescriptor(
repositoryDescriptor.RepositoryService,
repositoryDescriptor.RepositoryImplementation,
lifetime
)
);
}
}
/// <summary>
/// Register Repository ...
/// </summary>
/// <param name="source"></param>
/// <param name="repositoryDescriptor"></param>
/// <param name="lifetime"></param>
/// <typeparam name="TEntity"></typeparam>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TKeyGeneratorService"></typeparam>
/// <typeparam name="TKeyGeneratorImplementation"></typeparam>
/// <typeparam name="TEventsService"></typeparam>
/// <typeparam name="TEventsImplementation"></typeparam>
/// <typeparam name="TGraph"></typeparam>
/// <typeparam name="TGraphKey"></typeparam>
/// <typeparam name="TGraphQuery"></typeparam>
/// <typeparam name="TGraphTypeHelperService"></typeparam>
/// <typeparam name="TGraphTypeHelperImplementation"></typeparam>
/// <typeparam name="TGraphSchema"></typeparam>
/// <typeparam name="TRepositoryService"></typeparam>
/// <typeparam name="TRepositoryImplementation"></typeparam>
/// <typeparam name="TSeederService"></typeparam>
/// <typeparam name="TSeederImplementation"></typeparam>
/// <returns></returns>
public static void AddXRepository<
TEntity,
TKey,
TKeyGeneratorService,
TKeyGeneratorImplementation,
TEventsService,
TEventsImplementation,
TGraph,
TGraphKey,
TGraphQuery,
TGraphTypeHelperService,
TGraphTypeHelperImplementation,
TGraphSchema,
TRepositoryService,
TRepositoryImplementation,
TSeederService,
TSeederImplementation
>(
IServiceCollection source,
XInMemoryRepositoryDescriptor<TEntity, TKey, TKeyGeneratorService, TKeyGeneratorImplementation, TEventsService, TEventsImplementation, TGraph, TGraphKey, TGraphQuery, TGraphTypeHelperService, TGraphTypeHelperImplementation, TGraphSchema, TRepositoryService, TRepositoryImplementation, TSeederService, TSeederImplementation> repositoryDescriptor,
ServiceLifetime lifetime = ServiceLifetime.Scoped
)
where TGraphSchema : Schema
where TGraphKey : IGraphType
where TEntity : XBaseEntity<TKey>
where TSeederService : IXBaseDbSeeder<TEntity, TKey>
where TSeederImplementation : XBaseDbSeeder<TEntity, TKey>
where TGraph : XBaseGraphObjectType<TEntity, TKey>
where TEventsService : IXBaseRepositoryEvents<TEntity>
where TKeyGeneratorService : IXKeyGenerator<TEntity, TKey>
where TRepositoryService : IXBaseRepository<TEntity, TKey>
where TEventsImplementation : XBaseRepositoryEvents<TEntity>
where TKeyGeneratorImplementation : XKeyGenerator<TEntity, TKey>
where TGraphTypeHelperService : IXBaseGraphQLTypeHelper<TEntity, TKey>
where TGraphQuery : XBaseGraphQLQuery<TEntity, TKey, TGraph, TGraphKey>
where TRepositoryImplementation : XBaseInMemoryRepository<TEntity, TKey>
where TGraphTypeHelperImplementation : XBaseGraphQLTypeHelper<TEntity, TKey>
{
//
var isValid = repositoryDescriptor.IsValid();
if (!isValid)
{
return;
}
//
// Register KeyGenerator ...
isValid = repositoryDescriptor.HasKeyGenerator();
if (isValid)
{
//
AddXKeyGenerator<TEntity, TKey, TKeyGeneratorService, TKeyGeneratorImplementation>(
source,
repositoryDescriptor,
lifetime
);
}
//
// Register Events ...
isValid = repositoryDescriptor.HasEvents();
if (isValid)
{
//
AddXEvents<TEntity, TKey, TEventsService, TEventsImplementation>(
source,
repositoryDescriptor,
lifetime
);
}
//
// Register Graph ...
isValid = repositoryDescriptor.HasGraphSupport();
if (isValid)
{
//
AddXGraphQL<TEntity, TKey, TGraph, TGraphKey, TGraphQuery, TGraphTypeHelperService, TGraphTypeHelperImplementation, TGraphSchema>(
source,
repositoryDescriptor,
lifetime
);
}
//
// Register Repository ...
isValid = repositoryDescriptor.HasRepository();
if (isValid)
{
//
source.Add(
new ServiceDescriptor(
repositoryDescriptor.RepositoryService,
repositoryDescriptor.RepositoryImplementation,
lifetime
)
);
}
//
// DbSeeder ...
isValid = repositoryDescriptor.HasSeeder();
if (isValid)
{
//
source.Add(
new ServiceDescriptor(
repositoryDescriptor.SeederService,
repositoryDescriptor.SeederImplementation,
lifetime
)
);
}
}
/// <summary>
/// Register Repository ...
/// </summary>
/// <param name="source"></param>
/// <param name="repositoryDescriptor"></param>
/// <param name="lifetime"></param>
/// <typeparam name="TEntity"></typeparam>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TKeyGeneratorService"></typeparam>
/// <typeparam name="TKeyGeneratorImplementation"></typeparam>
/// <typeparam name="TEventsService"></typeparam>
/// <typeparam name="TEventsImplementation"></typeparam>
/// <typeparam name="TGraph"></typeparam>
/// <typeparam name="TGraphKey"></typeparam>
/// <typeparam name="TGraphQuery"></typeparam>
/// <typeparam name="TGraphTypeHelperService"></typeparam>
/// <typeparam name="TGraphTypeHelperImplementation"></typeparam>
/// <typeparam name="TGraphSchema"></typeparam>
/// <typeparam name="TRepositoryService"></typeparam>
/// <typeparam name="TRepositoryImplementation"></typeparam>
/// <returns></returns>
public static void AddXRepository<
TEntity,
TKey,
TKeyGeneratorService,
TKeyGeneratorImplementation,
TEventsService,
TEventsImplementation,
TGraph,
TGraphKey,
TGraphQuery,
TGraphTypeHelperService,
TGraphTypeHelperImplementation,
TGraphSchema,
TRepositoryService,
TRepositoryImplementation
>(
IServiceCollection source,
XMongoRepositoryDescriptor<TEntity, TKey, TKeyGeneratorService, TKeyGeneratorImplementation, TEventsService, TEventsImplementation, TGraph, TGraphKey, TGraphQuery, TGraphTypeHelperService, TGraphTypeHelperImplementation, TGraphSchema, TRepositoryService, TRepositoryImplementation> repositoryDescriptor,
ServiceLifetime lifetime = ServiceLifetime.Scoped
)
where TGraphSchema : Schema
where TGraphKey : IGraphType
where TEntity : XBaseEntity<TKey>
where TGraph : XBaseGraphObjectType<TEntity, TKey>
where TEventsService : IXBaseRepositoryEvents<TEntity>
where TKeyGeneratorService : IXKeyGenerator<TEntity, TKey>
where TRepositoryService : IXBaseRepository<TEntity, TKey>
where TEventsImplementation : XBaseRepositoryEvents<TEntity>
where TKeyGeneratorImplementation : XKeyGenerator<TEntity, TKey>
where TRepositoryImplementation : XBaseMongoRepository<TEntity, TKey>
where TGraphTypeHelperService : IXBaseGraphQLTypeHelper<TEntity, TKey>
where TGraphQuery : XBaseGraphQLQuery<TEntity, TKey, TGraph, TGraphKey>
where TGraphTypeHelperImplementation : XBaseGraphQLTypeHelper<TEntity, TKey>
{
//
var isValid = repositoryDescriptor.IsValid();
if (!isValid)
{
return;
}
//
// Register KeyGenerator ...
isValid = repositoryDescriptor.HasKeyGenerator();
if (isValid)
{
//
AddXKeyGenerator<TEntity, TKey, TKeyGeneratorService, TKeyGeneratorImplementation>(
source,
repositoryDescriptor,
lifetime
);
}
//
// Register Events ...
isValid = repositoryDescriptor.HasEvents();
if (isValid)
{
//
AddXEvents<TEntity, TKey, TEventsService, TEventsImplementation>(
source,
repositoryDescriptor,
lifetime
);
}
//
// Register Graph ...
isValid = repositoryDescriptor.HasGraphSupport();
if (isValid)
{
//
AddXGraphQL<TEntity, TKey, TGraph, TGraphKey, TGraphQuery, TGraphTypeHelperService, TGraphTypeHelperImplementation, TGraphSchema>(
source,
repositoryDescriptor,
lifetime
);
}
//
// Register Repository ...
isValid = repositoryDescriptor.HasRepository();
if (isValid)
{
//
source.Add(
new ServiceDescriptor(
repositoryDescriptor.RepositoryService,
repositoryDescriptor.RepositoryImplementation,
lifetime
)
);
}
}
/// <summary>
/// Register Repository ...
/// </summary>
/// <param name="source"></param>
/// <param name="repositoryDescriptor"></param>
/// <param name="lifetime"></param>
/// <typeparam name="TEntity"></typeparam>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TKeyGeneratorService"></typeparam>
/// <typeparam name="TKeyGeneratorImplementation"></typeparam>
/// <typeparam name="TEventsService"></typeparam>
/// <typeparam name="TEventsImplementation"></typeparam>
/// <typeparam name="TGraph"></typeparam>
/// <typeparam name="TGraphKey"></typeparam>
/// <typeparam name="TGraphQuery"></typeparam>
/// <typeparam name="TGraphTypeHelperService"></typeparam>
/// <typeparam name="TGraphTypeHelperImplementation"></typeparam>
/// <typeparam name="TGraphSchema"></typeparam>
/// <typeparam name="TRepositoryService"></typeparam>
/// <typeparam name="TRepositoryImplementation"></typeparam>
/// <typeparam name="TSeederService"></typeparam>
/// <typeparam name="TSeederImplementation"></typeparam>
/// <returns></returns>
public static void AddXRepository<
TEntity,
TKey,
TKeyGeneratorService,
TKeyGeneratorImplementation,
TEventsService,
TEventsImplementation,
TGraph,
TGraphKey,
TGraphQuery,
TGraphTypeHelperService,
TGraphTypeHelperImplementation,
TGraphSchema,
TRepositoryService,
TRepositoryImplementation,
TSeederService,
TSeederImplementation
>(
IServiceCollection source,
XMongoRepositoryDescriptor<TEntity, TKey, TKeyGeneratorService, TKeyGeneratorImplementation, TEventsService, TEventsImplementation, TGraph, TGraphKey, TGraphQuery, TGraphTypeHelperService, TGraphTypeHelperImplementation, TGraphSchema, TRepositoryService, TRepositoryImplementation, TSeederService, TSeederImplementation> repositoryDescriptor,
ServiceLifetime lifetime = ServiceLifetime.Scoped
)
where TGraphSchema : Schema
where TGraphKey : IGraphType
where TEntity : XBaseEntity<TKey>
where TGraph : XBaseGraphObjectType<TEntity, TKey>
where TSeederService : IXBaseDbSeeder<TEntity, TKey>
where TEventsService : IXBaseRepositoryEvents<TEntity>
where TKeyGeneratorService : IXKeyGenerator<TEntity, TKey>
where TRepositoryService : IXBaseRepository<TEntity, TKey>
where TSeederImplementation : XBaseDbSeeder<TEntity, TKey>
where TEventsImplementation : XBaseRepositoryEvents<TEntity>
where TKeyGeneratorImplementation : XKeyGenerator<TEntity, TKey>
where TRepositoryImplementation : XBaseMongoRepository<TEntity, TKey>
where TGraphTypeHelperService : IXBaseGraphQLTypeHelper<TEntity, TKey>
where TGraphQuery : XBaseGraphQLQuery<TEntity, TKey, TGraph, TGraphKey>
where TGraphTypeHelperImplementation : XBaseGraphQLTypeHelper<TEntity, TKey>
{
//
var isValid = repositoryDescriptor.IsValid();
if (!isValid)
{
return;
}
//
// Register KeyGenerator ...
isValid = repositoryDescriptor.HasKeyGenerator();
if (isValid)
{
//
AddXKeyGenerator<TEntity, TKey, TKeyGeneratorService, TKeyGeneratorImplementation>(
source,
repositoryDescriptor,
lifetime
);
}
//
// Register Events ...
isValid = repositoryDescriptor.HasEvents();
if (isValid)
{
//
AddXEvents<TEntity, TKey, TEventsService, TEventsImplementation>(
source,
repositoryDescriptor,
lifetime
);
}
//
// Register Graph ...
isValid = repositoryDescriptor.HasGraphSupport();
if (isValid)
{
//
AddXGraphQL<TEntity, TKey, TGraph, TGraphKey, TGraphQuery, TGraphTypeHelperService, TGraphTypeHelperImplementation, TGraphSchema>(
source,
repositoryDescriptor,
lifetime
);
}
//
// Register Repository ...
isValid = repositoryDescriptor.HasRepository();
if (isValid)
{
//
source.Add(
new ServiceDescriptor(
repositoryDescriptor.RepositoryService,
repositoryDescriptor.RepositoryImplementation,
lifetime
)
);
}
//
// DbSeeder ...
isValid = repositoryDescriptor.HasSeeder();
if (isValid)
{
//
source.Add(
new ServiceDescriptor(
repositoryDescriptor.SeederService,
repositoryDescriptor.SeederImplementation,
lifetime
)
);
}
}
/// <summary>
/// Register Repository ...
/// </summary>
/// <param name="source"></param>
/// <param name="repositoryDescriptor"></param>
/// <param name="lifetime"></param>
/// <typeparam name="TEntity"></typeparam>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TKeyGeneratorService"></typeparam>
/// <typeparam name="TKeyGeneratorImplementation"></typeparam>
/// <typeparam name="TEventsService"></typeparam>
/// <typeparam name="TEventsImplementation"></typeparam>
/// <typeparam name="TGraph"></typeparam>
/// <typeparam name="TGraphKey"></typeparam>
/// <typeparam name="TGraphQuery"></typeparam>
/// <typeparam name="TGraphTypeHelperService"></typeparam>
/// <typeparam name="TGraphTypeHelperImplementation"></typeparam>
/// <typeparam name="TGraphSchema"></typeparam>
/// <typeparam name="TRepositoryService"></typeparam>
/// <typeparam name="TRepositoryImplementation"></typeparam>
/// <typeparam name="TContext"></typeparam>
/// <returns></returns>
public static void AddXRepository<
TEntity,
TKey,
TKeyGeneratorService,
TKeyGeneratorImplementation,
TEventsService,
TEventsImplementation,
TGraph,
TGraphKey,
TGraphQuery,
TGraphTypeHelperService,
TGraphTypeHelperImplementation,
TGraphSchema,
TRepositoryService,
TRepositoryImplementation,
TContext
>(
IServiceCollection source,
XEFRepositoryDescriptor<TEntity, TKey, TKeyGeneratorService, TKeyGeneratorImplementation, TEventsService, TEventsImplementation, TGraph, TGraphKey, TGraphQuery, TGraphTypeHelperService, TGraphTypeHelperImplementation, TGraphSchema, TRepositoryService, TRepositoryImplementation, TContext> repositoryDescriptor,
ServiceLifetime lifetime = ServiceLifetime.Scoped
)
where TGraphSchema : Schema
where TContext : XDbContext
where TGraphKey : IGraphType
where TEntity : XBaseEntity<TKey>
where TGraph : XBaseGraphObjectType<TEntity, TKey>
where TEventsService : IXBaseRepositoryEvents<TEntity>
where TKeyGeneratorService : IXKeyGenerator<TEntity, TKey>
where TRepositoryService : IXBaseRepository<TEntity, TKey>
where TEventsImplementation : XBaseRepositoryEvents<TEntity>
where TKeyGeneratorImplementation : XKeyGenerator<TEntity, TKey>
where TGraphTypeHelperService : IXBaseGraphQLTypeHelper<TEntity, TKey>
where TGraphQuery : XBaseGraphQLQuery<TEntity, TKey, TGraph, TGraphKey>
where TRepositoryImplementation : XBaseEFRepository<TEntity, TKey, TContext>
where TGraphTypeHelperImplementation : XBaseGraphQLTypeHelper<TEntity, TKey>
{
//
var isValid = repositoryDescriptor.IsValid();
if (!isValid)
{
return;
}
//
// Register KeyGenerator ...
isValid = repositoryDescriptor.HasKeyGenerator();
if (isValid)
{
//
AddXKeyGenerator<TEntity, TKey, TKeyGeneratorService, TKeyGeneratorImplementation>(
source,
repositoryDescriptor,
lifetime
);
}
//
// Register Events ...
isValid = repositoryDescriptor.HasEvents();
if (isValid)
{
//
AddXEvents<TEntity, TKey, TEventsService, TEventsImplementation>(
source,
repositoryDescriptor,
lifetime
);
}
//
// Register Graph ...
isValid = repositoryDescriptor.HasGraphSupport();
if (isValid)
{
//
AddXGraphQL<TEntity, TKey, TGraph, TGraphKey, TGraphQuery, TGraphTypeHelperService, TGraphTypeHelperImplementation, TGraphSchema>(
source,
repositoryDescriptor,
lifetime
);
}
//
// Register Repository ...
isValid = repositoryDescriptor.HasRepository();
if (isValid)
{
//
source.Add(
new ServiceDescriptor(
repositoryDescriptor.RepositoryService,
repositoryDescriptor.RepositoryImplementation,
lifetime
)
);
}
}
/// <summary>
/// Register Repository ...
/// </summary>
/// <param name="source"></param>
/// <param name="repositoryDescriptor"></param>
/// <param name="lifetime"></param>
/// <typeparam name="TEntity"></typeparam>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TKeyGeneratorService"></typeparam>
/// <typeparam name="TKeyGeneratorImplementation"></typeparam>
/// <typeparam name="TEventsService"></typeparam>
/// <typeparam name="TEventsImplementation"></typeparam>
/// <typeparam name="TGraph"></typeparam>
/// <typeparam name="TGraphKey"></typeparam>
/// <typeparam name="TGraphQuery"></typeparam>
/// <typeparam name="TGraphTypeHelperService"></typeparam>
/// <typeparam name="TGraphTypeHelperImplementation"></typeparam>
/// <typeparam name="TGraphSchema"></typeparam>
/// <typeparam name="TRepositoryService"></typeparam>
/// <typeparam name="TRepositoryImplementation"></typeparam>
/// <typeparam name="TContext"></typeparam>
/// <typeparam name="TSeederService"></typeparam>
/// <typeparam name="TSeederImplementation"></typeparam>
/// <returns></returns>
public static void AddXRepository<
TEntity,
TKey,
TKeyGeneratorService,
TKeyGeneratorImplementation,
TEventsService,
TEventsImplementation,
TGraph,
TGraphKey,
TGraphQuery,
TGraphTypeHelperService,
TGraphTypeHelperImplementation,
TGraphSchema,
TRepositoryService,
TRepositoryImplementation,
TContext,
TSeederService,
TSeederImplementation
>(
IServiceCollection source,
XEFRepositoryDescriptor<TEntity, TKey, TKeyGeneratorService, TKeyGeneratorImplementation, TEventsService, TEventsImplementation, TGraph, TGraphKey, TGraphQuery, TGraphTypeHelperService, TGraphTypeHelperImplementation, TGraphSchema, TRepositoryService, TRepositoryImplementation, TContext, TSeederService, TSeederImplementation> repositoryDescriptor,
ServiceLifetime lifetime = ServiceLifetime.Scoped
)
where TGraphSchema : Schema
where TContext : XDbContext
where TGraphKey : IGraphType
where TEntity : XBaseEntity<TKey>
where TGraph : XBaseGraphObjectType<TEntity, TKey>
where TSeederService : IXBaseDbSeeder<TEntity, TKey>
where TEventsService : IXBaseRepositoryEvents<TEntity>
where TSeederImplementation : XBaseDbSeeder<TEntity, TKey>
where TKeyGeneratorService : IXKeyGenerator<TEntity, TKey>
where TRepositoryService : IXBaseRepository<TEntity, TKey>
where TEventsImplementation : XBaseRepositoryEvents<TEntity>
where TKeyGeneratorImplementation : XKeyGenerator<TEntity, TKey>
where TGraphTypeHelperService : IXBaseGraphQLTypeHelper<TEntity, TKey>
where TGraphQuery : XBaseGraphQLQuery<TEntity, TKey, TGraph, TGraphKey>
where TRepositoryImplementation : XBaseEFRepository<TEntity, TKey, TContext>
where TGraphTypeHelperImplementation : XBaseGraphQLTypeHelper<TEntity, TKey>
{
//
var isValid = repositoryDescriptor.IsValid();
if (!isValid)
{
return;
}
//
// Register KeyGenerator ...
isValid = repositoryDescriptor.HasKeyGenerator();
if (isValid)
{
//
AddXKeyGenerator<TEntity, TKey, TKeyGeneratorService, TKeyGeneratorImplementation>(
source,
repositoryDescriptor,
lifetime
);
}
//
// Register Events ...
isValid = repositoryDescriptor.HasEvents();
if (isValid)
{
//
AddXEvents<TEntity, TKey, TEventsService, TEventsImplementation>(
source,
repositoryDescriptor,
lifetime
);
}
//
// Register Graph ...
isValid = repositoryDescriptor.HasGraphSupport();
if (isValid)
{
//
AddXGraphQL<TEntity, TKey, TGraph, TGraphKey, TGraphQuery, TGraphTypeHelperService, TGraphTypeHelperImplementation, TGraphSchema>(
source,
repositoryDescriptor,
lifetime
);
}
//
// Register Repository ...
isValid = repositoryDescriptor.HasRepository();
if (isValid)
{
//
source.Add(
new ServiceDescriptor(
repositoryDescriptor.RepositoryService,
repositoryDescriptor.RepositoryImplementation,
lifetime
)
);
}
//
// DbSeeder ...
isValid = repositoryDescriptor.HasSeeder();
if (isValid)
{
//
source.Add(
new ServiceDescriptor(
repositoryDescriptor.SeederService,
repositoryDescriptor.SeederImplementation,
lifetime
)
);
}
}
/// <summary>
/// Use GraphQL ...
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TGraph"></typeparam>
/// <typeparam name="TGraphKey"></typeparam>
/// <typeparam name="TGraphQuery"></typeparam>
/// <typeparam name="TGraphTypeHelperService"></typeparam>
/// <typeparam name="TGraphTypeHelperImplementation"></typeparam>
/// <typeparam name="TGraphSchema"></typeparam>
/// <param name="source"></param>
/// <param name="graphBasePath"></param>
/// <param name="graphPath"></param>
/// <param name="withPlayGround"></param>
/// <param name="options"></param>
public static void UseXGraphQL<
TEntity,
TKey,
TGraph,
TGraphKey,
TGraphQuery,
TGraphTypeHelperService,
TGraphTypeHelperImplementation,
TGraphSchema
>(
IApplicationBuilder source,
string graphBasePath,
string graphPath,
bool withPlayGround = false
)
where TGraphSchema : Schema
where TGraphKey : IGraphType
where TEntity : XBaseEntity<TKey>
where TGraph : XBaseGraphObjectType<TEntity, TKey>
where TGraphTypeHelperService : IXBaseGraphQLTypeHelper<TEntity, TKey>
where TGraphQuery : XBaseGraphQLQuery<TEntity, TKey, TGraph, TGraphKey>
where TGraphTypeHelperImplementation : XBaseGraphQLTypeHelper<TEntity, TKey>
{
//
source.UseGraphQL<TGraphSchema>(graphPath);
source.UseGraphQLWebSockets<TGraphSchema>();
//
if (withPlayGround)
{
//
source.UseGraphQLPlayground(new GraphQLPlaygroundOptions
{
Path = "/ui/playground",
GraphQLEndPoint = graphBasePath
});
//
source.UseGraphQLVoyager(new GraphQLVoyagerOptions
{
Path = "/ui/voyager",
GraphQLEndPoint = graphBasePath
});
}
}
#endregion
//
#region Entity Assembly Detector ...
/// <summary>
/// Extract Assemblies which Contains Entity ...
/// </summary>
/// <returns></returns>
public static List<Assembly> ExtractEntityAssemblies()
{
//
var result = new List<Assembly>();
//
var allAssemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (var assembly in allAssemblies)
{
//
try
{
//
bool hasEntity = assembly
.GetExportedTypes()
.Any(t => t.IsXEntity());
if (hasEntity)
{
result.Add(assembly);
}
}
catch { }
}
//
return result;
}
/// <summary>
/// Register All Entities using Model Builder ...
/// </summary>
/// <param name="modelBuilder"></param>
public static void RegisterAllEntities(this ModelBuilder modelBuilder)
{
//
// Extract Entity Container Assemblies ...
var entityAssemblies = ExtractEntityAssemblies();
foreach(var assembly in entityAssemblies)
{
modelBuilder.ApplyConfigurationsFromAssembly(assembly);
}
}
#endregion
//
#region Private ...
/// <summary>
/// Register Unit of Works ...
/// </summary>
/// <param name="source"></param>
/// <param name="lifetime"></param>
/// <typeparam name="TContext"></typeparam>
public static void AddXUnitOfWorks<TContext>(
IServiceCollection source,
ServiceLifetime lifetime = ServiceLifetime.Scoped
)
where TContext : XDbContext
{
//
source.Add(
new ServiceDescriptor(
typeof(IXUnitOfWorks<TContext>),
typeof(XUnitOfWorks<TContext>),
lifetime
)
);
}
/// <summary>
/// Register Key Generator Service ...
/// </summary>
/// <param name="source"></param>
/// <param name="repositoryDescriptor"></param>
/// <param name="lifetime"></param>
/// <typeparam name="TEntity"></typeparam>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TKeyGeneratorService"></typeparam>
/// <typeparam name="TKeyGeneratorImplementation"></typeparam>
/// <returns></returns>
public static void AddXKeyGenerator<
TEntity,
TKey,
TKeyGeneratorService,
TKeyGeneratorImplementation
>(
IServiceCollection source,
XRepositoryDescriptor<TEntity, TKey> repositoryDescriptor,
ServiceLifetime lifetime = ServiceLifetime.Scoped
)
where TEntity : XBaseEntity<TKey>
where TKeyGeneratorService : IXKeyGenerator<TEntity, TKey>
where TKeyGeneratorImplementation : XKeyGenerator<TEntity, TKey>
{
//
// Validate ...
var isValid =
!source.IsNull() &&
!repositoryDescriptor.IsNull() &&
!repositoryDescriptor.KeyGeneratorService.IsNull() &&
!repositoryDescriptor.KeyGeneratorImplementation.IsNull();
if (!isValid)
{
return;
}
//
// Register ...
source.Add(
new ServiceDescriptor(
repositoryDescriptor.KeyGeneratorService,
repositoryDescriptor.KeyGeneratorImplementation,
lifetime
)
);
}
/// <summary>
/// Register Events Service ...
/// </summary>
/// <param name="source"></param>
/// <param name="repositoryDescriptor"></param>
/// <param name="lifetime"></param>
/// <typeparam name="TEntity"></typeparam>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TEventsService"></typeparam>
/// <typeparam name="TEventsImplementation"></typeparam>
/// <returns></returns>
public static void AddXEvents<
TEntity,
TKey,
TEventsService,
TEventsImplementation
>(
IServiceCollection source,
XRepositoryDescriptor<TEntity, TKey> repositoryDescriptor,
ServiceLifetime lifetime = ServiceLifetime.Scoped
)
where TEntity : XBaseEntity<TKey>
where TEventsService : IXBaseRepositoryEvents<TEntity>
where TEventsImplementation : XBaseRepositoryEvents<TEntity>
{
//
// Validate ...
var isValid =
!source.IsNull() &&
!repositoryDescriptor.IsNull() &&
!repositoryDescriptor.EventsService.IsNull() &&
!repositoryDescriptor.EventsImplementation.IsNull();
if (!isValid)
{
return;
}
//
// Register ...
source.Add(
new ServiceDescriptor(
repositoryDescriptor.EventsService,
repositoryDescriptor.EventsImplementation,
lifetime
)
);
}
/// <summary>
/// Register GraphQL Service ...
/// </summary>
/// <param name="source"></param>
/// <param name="repositoryDescriptor"></param>
/// <param name="lifetime"></param>
/// <typeparam name="TEntity"></typeparam>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TGraph"></typeparam>
/// <typeparam name="TGraphKey"></typeparam>
/// <typeparam name="TGraphQuery"></typeparam>
/// <typeparam name="TGraphTypeHelperService"></typeparam>
/// <typeparam name="TGraphTypeHelperImplementation"></typeparam>
/// <typeparam name="TGraphSchema"></typeparam>
/// <returns></returns>
public static void AddXGraphQL<
TEntity,
TKey,
TGraph,
TGraphKey,
TGraphQuery,
TGraphTypeHelperService,
TGraphTypeHelperImplementation,
TGraphSchema
>(
IServiceCollection source,
XRepositoryDescriptor<TEntity, TKey> repositoryDescriptor,
ServiceLifetime lifetime = ServiceLifetime.Scoped
)
where TGraphSchema : Schema
where TGraphKey : IGraphType
where TEntity : XBaseEntity<TKey>
where TGraph : XBaseGraphObjectType<TEntity, TKey>
where TGraphTypeHelperService : IXBaseGraphQLTypeHelper<TEntity, TKey>
where TGraphQuery : XBaseGraphQLQuery<TEntity, TKey, TGraph, TGraphKey>
where TGraphTypeHelperImplementation : XBaseGraphQLTypeHelper<TEntity, TKey>
{
//
// Validate ...
var isValid =
!source.IsNull() &&
!repositoryDescriptor.IsNull() &&
!repositoryDescriptor.GraphType.IsNull() &&
!repositoryDescriptor.GraphQuery.IsNull() &&
!repositoryDescriptor.GraphSchema.IsNull() &&
!repositoryDescriptor.GraphTypeKey.IsNull() &&
!repositoryDescriptor.GraphTypeHelperService.IsNull() &&
!repositoryDescriptor.GraphTypeHelperImplementation.IsNull();
if (!isValid)
{
return;
}
//
// Register Graph Type ...
source.Add(
new ServiceDescriptor(
lifetime: lifetime, // ServiceLifetime.Singleton,
serviceType: repositoryDescriptor.GraphType,
implementationType: repositoryDescriptor.GraphType
)
);
//
// Register Graph Events ...
source.Add(
new ServiceDescriptor(
lifetime: lifetime, // ServiceLifetime.Singleton,
serviceType: typeof(XBaseGraphQLEventType<TEntity, TKey, TGraph>),
implementationType: typeof(XBaseGraphQLEventType<TEntity, TKey, TGraph>)
)
);
//
// Register Graph Type Helper ...
source.Add(
new ServiceDescriptor(
lifetime: ServiceLifetime.Singleton,
serviceType: repositoryDescriptor.GraphTypeHelperService,
implementationType: repositoryDescriptor.GraphTypeHelperImplementation
)
);
//
// Register Graph Query ...
source.Add(
new ServiceDescriptor(
lifetime: lifetime,
serviceType: repositoryDescriptor.GraphQuery,
implementationType: repositoryDescriptor.GraphQuery
)
);
//
// Register Graph Schema ...
source.Add(
new ServiceDescriptor(
lifetime: lifetime,
serviceType: repositoryDescriptor.GraphSchema,
implementationType: repositoryDescriptor.GraphSchema
)
);
//
source.AddScoped<IDocumentExecuter, DocumentExecuter>();
//
// Register Graph Type Schema ...
var builder = source.GetRegisteredService<IGraphQLBuilder>();
if (builder.IsNull())
{
//
Action<GraphQLOptions> options = x =>
{
//
x.EnableMetrics = true;
x.UnhandledExceptionDelegate = context =>
{
Console.WriteLine("XGraphQL Error: " + context.OriginalException.Message);
};
};
builder = source.AddGraphQL(options)
.AddNewtonsoftJson(deserializerSettings => { }, serializerSettings => { })
.AddWebSockets()
.AddDataLoader()
.AddGraphTypes(lifetime);
}
builder.AddGraphTypes(
serviceLifetime: lifetime,
typeFromAssembly: repositoryDescriptor.GraphSchema
);
}
#endregion
}
}