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
{
///
/// Register DbContext ...
///
///
///
///
///
public static void AddXDbContext(
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(cfg =>
{
//
cfg.UseMySQL(
databaseDescriptor.ConnectionString,
databaseDescriptor.OptionsBuilder
);
}, lifetime);
break;
//
// SQLite ...
case XDbProviders.SQLite:
//
source.AddEntityFrameworkSqlite();
source.AddDbContext(cfg =>
{
//
cfg.UseSqlite(
databaseDescriptor.ConnectionString,
databaseDescriptor.OptionsBuilder
);
}, lifetime);
break;
//
// Oracle ...
case XDbProviders.Oracle:
//
source.AddEntityFrameworkOracle();
source.AddDbContext(cfg =>
{
//
cfg.UseOracle(
databaseDescriptor.ConnectionString,
databaseDescriptor.OptionsBuilder
);
}, lifetime);
break;
//
// SQLServer ...
case XDbProviders.SQLServer:
//
source.AddEntityFrameworkSqlServer();
source.AddDbContext(cfg =>
{
//
cfg.UseSqlServer(
databaseDescriptor.ConnectionString,
databaseDescriptor.OptionsBuilder
);
}, lifetime);
break;
//
// Mongo DB ...
case XDbProviders.MongoDB:
//
#region XBaseEntity Mappers ...
//
// Map ID as BsonId here ...
BsonClassMap.RegisterClassMap>(cm =>
{
//
cm.AutoMap();
cm.MapIdMember(c => c.Id);
});
BsonClassMap.RegisterClassMap>(cm =>
{
//
cm.AutoMap();
cm.MapIdMember(c => c.Id);
});
BsonClassMap.RegisterClassMap>(cm =>
{
//
cm.AutoMap();
cm.MapIdMember(c => c.Id);
});
BsonClassMap.RegisterClassMap>(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(
source,
lifetime
);
}
}
//
#region Repository ...
///
/// Register Repository ...
///
///
///
///
///
///
///
public static void AddXRepository(
IServiceCollection source,
XRepositoryDescriptor repositoryDescriptor,
ServiceLifetime lifetime = ServiceLifetime.Scoped
)
where TEntity : XBaseEntity
{
//
var isValid = repositoryDescriptor.IsValid();
if (!isValid)
{
return;
}
}
///
/// Register Repository ...
///
///
///
///
///
///
///
///
///
public static void AddXRepository<
TEntity,
TKey,
TKeyGeneratorService,
TKeyGeneratorImplementation
>(
IServiceCollection source,
XRepositoryDescriptor repositoryDescriptor,
ServiceLifetime lifetime = ServiceLifetime.Scoped
)
where TEntity : XBaseEntity
where TKeyGeneratorService : IXKeyGenerator
where TKeyGeneratorImplementation : XKeyGenerator
{
//
var isValid = repositoryDescriptor.IsValid();
if (!isValid)
{
return;
}
//
// Register KeyGenerator ...
isValid = repositoryDescriptor.HasKeyGenerator();
if (isValid)
{
//
AddXKeyGenerator(
source,
repositoryDescriptor,
lifetime
);
}
}
///
/// Register Repository ...
///
///
///
///
///
///
///
///
///
///
///
public static void AddXRepository<
TEntity,
TKey,
TKeyGeneratorService,
TKeyGeneratorImplementation,
TEventsService,
TEventsImplementation
>(
IServiceCollection source,
XRepositoryDescriptor repositoryDescriptor,
ServiceLifetime lifetime = ServiceLifetime.Scoped
)
where TEntity : XBaseEntity
where TEventsService : IXBaseRepositoryEvents
where TKeyGeneratorService : IXKeyGenerator
where TEventsImplementation : XBaseRepositoryEvents
where TKeyGeneratorImplementation : XKeyGenerator
{
//
var isValid = repositoryDescriptor.IsValid();
if (!isValid)
{
return;
}
//
// Register KeyGenerator ...
isValid = repositoryDescriptor.HasKeyGenerator();
if (isValid)
{
//
AddXKeyGenerator(
source,
repositoryDescriptor,
lifetime
);
}
//
// Register Events ...
isValid = repositoryDescriptor.HasEvents();
if (isValid)
{
//
AddXEvents(
source,
repositoryDescriptor,
lifetime
);
}
}
///
/// Register Repository ...
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
public static void AddXRepository<
TEntity,
TKey,
TKeyGeneratorService,
TKeyGeneratorImplementation,
TEventsService,
TEventsImplementation,
TGraph,
TGraphKey,
TGraphQuery,
TGraphTypeHelperService,
TGraphTypeHelperImplementation,
TGraphSchema
>(
IServiceCollection source,
XRepositoryDescriptor repositoryDescriptor,
ServiceLifetime lifetime = ServiceLifetime.Scoped
)
where TGraphSchema : Schema
where TGraphKey : IGraphType
where TEntity : XBaseEntity
where TGraph : XBaseGraphObjectType
where TEventsService : IXBaseRepositoryEvents
where TKeyGeneratorService : IXKeyGenerator
where TEventsImplementation : XBaseRepositoryEvents
where TKeyGeneratorImplementation : XKeyGenerator
where TGraphTypeHelperService : IXBaseGraphQLTypeHelper
where TGraphQuery : XBaseGraphQLQuery
where TGraphTypeHelperImplementation : XBaseGraphQLTypeHelper
{
//
var isValid = repositoryDescriptor.IsValid();
if (!isValid)
{
return;
}
//
// Register KeyGenerator ...
isValid = repositoryDescriptor.HasKeyGenerator();
if (isValid)
{
//
AddXKeyGenerator(
source,
repositoryDescriptor,
lifetime
);
}
//
// Register Events ...
isValid = repositoryDescriptor.HasEvents();
if (isValid)
{
//
AddXEvents(
source,
repositoryDescriptor,
lifetime
);
}
//
// Register Graph ...
isValid = repositoryDescriptor.HasGraphSupport();
if (isValid)
{
//
AddXGraphQL(
source,
repositoryDescriptor,
lifetime
);
}
}
///
/// Register Repository ...
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
public static void AddXRepository<
TEntity,
TKey,
TKeyGeneratorService,
TKeyGeneratorImplementation,
TEventsService,
TEventsImplementation,
TGraph,
TGraphKey,
TGraphQuery,
TGraphTypeHelperService,
TGraphTypeHelperImplementation,
TGraphSchema,
TRepositoryService,
TRepositoryImplementation
>(
IServiceCollection source,
XInMemoryRepositoryDescriptor repositoryDescriptor,
ServiceLifetime lifetime = ServiceLifetime.Scoped
)
where TGraphSchema : Schema
where TGraphKey : IGraphType
where TEntity : XBaseEntity
where TGraph : XBaseGraphObjectType
where TEventsService : IXBaseRepositoryEvents
where TKeyGeneratorService : IXKeyGenerator
where TRepositoryService : IXBaseRepository
where TEventsImplementation : XBaseRepositoryEvents
where TKeyGeneratorImplementation : XKeyGenerator
where TGraphTypeHelperService : IXBaseGraphQLTypeHelper
where TGraphQuery : XBaseGraphQLQuery
where TRepositoryImplementation : XBaseInMemoryRepository
where TGraphTypeHelperImplementation : XBaseGraphQLTypeHelper
{
//
var isValid = repositoryDescriptor.IsValid();
if (!isValid)
{
return;
}
//
// Register KeyGenerator ...
isValid = repositoryDescriptor.HasKeyGenerator();
if (isValid)
{
//
AddXKeyGenerator(
source,
repositoryDescriptor,
lifetime
);
}
//
// Register Events ...
isValid = repositoryDescriptor.HasEvents();
if (isValid)
{
//
AddXEvents(
source,
repositoryDescriptor,
lifetime
);
}
//
// Register Graph ...
isValid = repositoryDescriptor.HasGraphSupport();
if (isValid)
{
//
AddXGraphQL(
source,
repositoryDescriptor,
lifetime
);
}
//
// Register Repository ...
isValid = repositoryDescriptor.HasRepository();
if (isValid)
{
//
source.Add(
new ServiceDescriptor(
repositoryDescriptor.RepositoryService,
repositoryDescriptor.RepositoryImplementation,
lifetime
)
);
}
}
///
/// Register Repository ...
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
public static void AddXRepository<
TEntity,
TKey,
TKeyGeneratorService,
TKeyGeneratorImplementation,
TEventsService,
TEventsImplementation,
TGraph,
TGraphKey,
TGraphQuery,
TGraphTypeHelperService,
TGraphTypeHelperImplementation,
TGraphSchema,
TRepositoryService,
TRepositoryImplementation,
TSeederService,
TSeederImplementation
>(
IServiceCollection source,
XInMemoryRepositoryDescriptor repositoryDescriptor,
ServiceLifetime lifetime = ServiceLifetime.Scoped
)
where TGraphSchema : Schema
where TGraphKey : IGraphType
where TEntity : XBaseEntity
where TSeederService : IXBaseDbSeeder
where TSeederImplementation : XBaseDbSeeder
where TGraph : XBaseGraphObjectType
where TEventsService : IXBaseRepositoryEvents
where TKeyGeneratorService : IXKeyGenerator
where TRepositoryService : IXBaseRepository
where TEventsImplementation : XBaseRepositoryEvents
where TKeyGeneratorImplementation : XKeyGenerator
where TGraphTypeHelperService : IXBaseGraphQLTypeHelper
where TGraphQuery : XBaseGraphQLQuery
where TRepositoryImplementation : XBaseInMemoryRepository
where TGraphTypeHelperImplementation : XBaseGraphQLTypeHelper
{
//
var isValid = repositoryDescriptor.IsValid();
if (!isValid)
{
return;
}
//
// Register KeyGenerator ...
isValid = repositoryDescriptor.HasKeyGenerator();
if (isValid)
{
//
AddXKeyGenerator(
source,
repositoryDescriptor,
lifetime
);
}
//
// Register Events ...
isValid = repositoryDescriptor.HasEvents();
if (isValid)
{
//
AddXEvents(
source,
repositoryDescriptor,
lifetime
);
}
//
// Register Graph ...
isValid = repositoryDescriptor.HasGraphSupport();
if (isValid)
{
//
AddXGraphQL(
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
)
);
}
}
///
/// Register Repository ...
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
public static void AddXRepository<
TEntity,
TKey,
TKeyGeneratorService,
TKeyGeneratorImplementation,
TEventsService,
TEventsImplementation,
TGraph,
TGraphKey,
TGraphQuery,
TGraphTypeHelperService,
TGraphTypeHelperImplementation,
TGraphSchema,
TRepositoryService,
TRepositoryImplementation
>(
IServiceCollection source,
XMongoRepositoryDescriptor repositoryDescriptor,
ServiceLifetime lifetime = ServiceLifetime.Scoped
)
where TGraphSchema : Schema
where TGraphKey : IGraphType
where TEntity : XBaseEntity
where TGraph : XBaseGraphObjectType
where TEventsService : IXBaseRepositoryEvents
where TKeyGeneratorService : IXKeyGenerator
where TRepositoryService : IXBaseRepository
where TEventsImplementation : XBaseRepositoryEvents
where TKeyGeneratorImplementation : XKeyGenerator
where TRepositoryImplementation : XBaseMongoRepository
where TGraphTypeHelperService : IXBaseGraphQLTypeHelper
where TGraphQuery : XBaseGraphQLQuery
where TGraphTypeHelperImplementation : XBaseGraphQLTypeHelper
{
//
var isValid = repositoryDescriptor.IsValid();
if (!isValid)
{
return;
}
//
// Register KeyGenerator ...
isValid = repositoryDescriptor.HasKeyGenerator();
if (isValid)
{
//
AddXKeyGenerator(
source,
repositoryDescriptor,
lifetime
);
}
//
// Register Events ...
isValid = repositoryDescriptor.HasEvents();
if (isValid)
{
//
AddXEvents(
source,
repositoryDescriptor,
lifetime
);
}
//
// Register Graph ...
isValid = repositoryDescriptor.HasGraphSupport();
if (isValid)
{
//
AddXGraphQL(
source,
repositoryDescriptor,
lifetime
);
}
//
// Register Repository ...
isValid = repositoryDescriptor.HasRepository();
if (isValid)
{
//
source.Add(
new ServiceDescriptor(
repositoryDescriptor.RepositoryService,
repositoryDescriptor.RepositoryImplementation,
lifetime
)
);
}
}
///
/// Register Repository ...
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
public static void AddXRepository<
TEntity,
TKey,
TKeyGeneratorService,
TKeyGeneratorImplementation,
TEventsService,
TEventsImplementation,
TGraph,
TGraphKey,
TGraphQuery,
TGraphTypeHelperService,
TGraphTypeHelperImplementation,
TGraphSchema,
TRepositoryService,
TRepositoryImplementation,
TSeederService,
TSeederImplementation
>(
IServiceCollection source,
XMongoRepositoryDescriptor repositoryDescriptor,
ServiceLifetime lifetime = ServiceLifetime.Scoped
)
where TGraphSchema : Schema
where TGraphKey : IGraphType
where TEntity : XBaseEntity
where TGraph : XBaseGraphObjectType
where TSeederService : IXBaseDbSeeder
where TEventsService : IXBaseRepositoryEvents
where TKeyGeneratorService : IXKeyGenerator
where TRepositoryService : IXBaseRepository
where TSeederImplementation : XBaseDbSeeder
where TEventsImplementation : XBaseRepositoryEvents
where TKeyGeneratorImplementation : XKeyGenerator
where TRepositoryImplementation : XBaseMongoRepository
where TGraphTypeHelperService : IXBaseGraphQLTypeHelper
where TGraphQuery : XBaseGraphQLQuery
where TGraphTypeHelperImplementation : XBaseGraphQLTypeHelper
{
//
var isValid = repositoryDescriptor.IsValid();
if (!isValid)
{
return;
}
//
// Register KeyGenerator ...
isValid = repositoryDescriptor.HasKeyGenerator();
if (isValid)
{
//
AddXKeyGenerator(
source,
repositoryDescriptor,
lifetime
);
}
//
// Register Events ...
isValid = repositoryDescriptor.HasEvents();
if (isValid)
{
//
AddXEvents(
source,
repositoryDescriptor,
lifetime
);
}
//
// Register Graph ...
isValid = repositoryDescriptor.HasGraphSupport();
if (isValid)
{
//
AddXGraphQL(
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
)
);
}
}
///
/// Register Repository ...
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
public static void AddXRepository<
TEntity,
TKey,
TKeyGeneratorService,
TKeyGeneratorImplementation,
TEventsService,
TEventsImplementation,
TGraph,
TGraphKey,
TGraphQuery,
TGraphTypeHelperService,
TGraphTypeHelperImplementation,
TGraphSchema,
TRepositoryService,
TRepositoryImplementation,
TContext
>(
IServiceCollection source,
XEFRepositoryDescriptor repositoryDescriptor,
ServiceLifetime lifetime = ServiceLifetime.Scoped
)
where TGraphSchema : Schema
where TContext : XDbContext
where TGraphKey : IGraphType
where TEntity : XBaseEntity
where TGraph : XBaseGraphObjectType
where TEventsService : IXBaseRepositoryEvents
where TKeyGeneratorService : IXKeyGenerator
where TRepositoryService : IXBaseRepository
where TEventsImplementation : XBaseRepositoryEvents
where TKeyGeneratorImplementation : XKeyGenerator
where TGraphTypeHelperService : IXBaseGraphQLTypeHelper
where TGraphQuery : XBaseGraphQLQuery
where TRepositoryImplementation : XBaseEFRepository
where TGraphTypeHelperImplementation : XBaseGraphQLTypeHelper
{
//
var isValid = repositoryDescriptor.IsValid();
if (!isValid)
{
return;
}
//
// Register KeyGenerator ...
isValid = repositoryDescriptor.HasKeyGenerator();
if (isValid)
{
//
AddXKeyGenerator(
source,
repositoryDescriptor,
lifetime
);
}
//
// Register Events ...
isValid = repositoryDescriptor.HasEvents();
if (isValid)
{
//
AddXEvents(
source,
repositoryDescriptor,
lifetime
);
}
//
// Register Graph ...
isValid = repositoryDescriptor.HasGraphSupport();
if (isValid)
{
//
AddXGraphQL(
source,
repositoryDescriptor,
lifetime
);
}
//
// Register Repository ...
isValid = repositoryDescriptor.HasRepository();
if (isValid)
{
//
source.Add(
new ServiceDescriptor(
repositoryDescriptor.RepositoryService,
repositoryDescriptor.RepositoryImplementation,
lifetime
)
);
}
}
///
/// Register Repository ...
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
public static void AddXRepository<
TEntity,
TKey,
TKeyGeneratorService,
TKeyGeneratorImplementation,
TEventsService,
TEventsImplementation,
TGraph,
TGraphKey,
TGraphQuery,
TGraphTypeHelperService,
TGraphTypeHelperImplementation,
TGraphSchema,
TRepositoryService,
TRepositoryImplementation,
TContext,
TSeederService,
TSeederImplementation
>(
IServiceCollection source,
XEFRepositoryDescriptor repositoryDescriptor,
ServiceLifetime lifetime = ServiceLifetime.Scoped
)
where TGraphSchema : Schema
where TContext : XDbContext
where TGraphKey : IGraphType
where TEntity : XBaseEntity
where TGraph : XBaseGraphObjectType
where TSeederService : IXBaseDbSeeder
where TEventsService : IXBaseRepositoryEvents
where TSeederImplementation : XBaseDbSeeder
where TKeyGeneratorService : IXKeyGenerator
where TRepositoryService : IXBaseRepository
where TEventsImplementation : XBaseRepositoryEvents
where TKeyGeneratorImplementation : XKeyGenerator
where TGraphTypeHelperService : IXBaseGraphQLTypeHelper
where TGraphQuery : XBaseGraphQLQuery
where TRepositoryImplementation : XBaseEFRepository
where TGraphTypeHelperImplementation : XBaseGraphQLTypeHelper
{
//
var isValid = repositoryDescriptor.IsValid();
if (!isValid)
{
return;
}
//
// Register KeyGenerator ...
isValid = repositoryDescriptor.HasKeyGenerator();
if (isValid)
{
//
AddXKeyGenerator(
source,
repositoryDescriptor,
lifetime
);
}
//
// Register Events ...
isValid = repositoryDescriptor.HasEvents();
if (isValid)
{
//
AddXEvents(
source,
repositoryDescriptor,
lifetime
);
}
//
// Register Graph ...
isValid = repositoryDescriptor.HasGraphSupport();
if (isValid)
{
//
AddXGraphQL(
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
)
);
}
}
///
/// Use GraphQL ...
///
///
///
///
///
///
///
///
///
///
///
///
///
///
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
where TGraph : XBaseGraphObjectType
where TGraphTypeHelperService : IXBaseGraphQLTypeHelper
where TGraphQuery : XBaseGraphQLQuery
where TGraphTypeHelperImplementation : XBaseGraphQLTypeHelper
{
//
source.UseGraphQL(graphPath);
source.UseGraphQLWebSockets();
//
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 ...
///
/// Extract Assemblies which Contains Entity ...
///
///
public static List ExtractEntityAssemblies()
{
//
var result = new List();
//
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;
}
///
/// Register All Entities using Model Builder ...
///
///
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 ...
///
/// Register Unit of Works ...
///
///
///
///
public static void AddXUnitOfWorks(
IServiceCollection source,
ServiceLifetime lifetime = ServiceLifetime.Scoped
)
where TContext : XDbContext
{
//
source.Add(
new ServiceDescriptor(
typeof(IXUnitOfWorks),
typeof(XUnitOfWorks),
lifetime
)
);
}
///
/// Register Key Generator Service ...
///
///
///
///
///
///
///
///
///
public static void AddXKeyGenerator<
TEntity,
TKey,
TKeyGeneratorService,
TKeyGeneratorImplementation
>(
IServiceCollection source,
XRepositoryDescriptor repositoryDescriptor,
ServiceLifetime lifetime = ServiceLifetime.Scoped
)
where TEntity : XBaseEntity
where TKeyGeneratorService : IXKeyGenerator
where TKeyGeneratorImplementation : XKeyGenerator
{
//
// 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
)
);
}
///
/// Register Events Service ...
///
///
///
///
///
///
///
///
///
public static void AddXEvents<
TEntity,
TKey,
TEventsService,
TEventsImplementation
>(
IServiceCollection source,
XRepositoryDescriptor repositoryDescriptor,
ServiceLifetime lifetime = ServiceLifetime.Scoped
)
where TEntity : XBaseEntity
where TEventsService : IXBaseRepositoryEvents
where TEventsImplementation : XBaseRepositoryEvents
{
//
// 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
)
);
}
///
/// Register GraphQL Service ...
///
///
///
///
///
///
///
///
///
///
///
///
///
public static void AddXGraphQL<
TEntity,
TKey,
TGraph,
TGraphKey,
TGraphQuery,
TGraphTypeHelperService,
TGraphTypeHelperImplementation,
TGraphSchema
>(
IServiceCollection source,
XRepositoryDescriptor repositoryDescriptor,
ServiceLifetime lifetime = ServiceLifetime.Scoped
)
where TGraphSchema : Schema
where TGraphKey : IGraphType
where TEntity : XBaseEntity
where TGraph : XBaseGraphObjectType
where TGraphTypeHelperService : IXBaseGraphQLTypeHelper
where TGraphQuery : XBaseGraphQLQuery
where TGraphTypeHelperImplementation : XBaseGraphQLTypeHelper
{
//
// 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),
implementationType: typeof(XBaseGraphQLEventType)
)
);
//
// 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();
//
// Register Graph Type Schema ...
var builder = source.GetRegisteredService();
if (builder.IsNull())
{
//
Action 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
}
}