Files
xDataService/Helpers/XDataServiceHelper.cs
T
2026-05-09 22:21:40 +03:30

1083 lines
47 KiB
C#

using System;
using GraphQL.Server;
using GraphQL.Types;
using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using MySql.EntityFrameworkCore.Extensions;
using xCommons.Extensions;
using xDataService.Constants;
using xDataService.Db;
using xDataService.EFRepositories;
using xDataService.Events;
using xDataService.Extensions;
using xDataService.GraphQL;
using xDataService.InMemRepositories;
using xDataService.Interfaces;
using xDataService.Models;
using xDataService.MongoRepositories;
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="provider"></param>
/// <param name="connectionString"></param>
/// <param name="lifetime"></param>
/// <param name="optionsBuilder"></param>
/// <typeparam name="TContext"></typeparam>
public static void AddXDbContext<TContext>(
IServiceCollection source,
XDbProviders provider,
string connectionString,
ServiceLifetime lifetime = ServiceLifetime.Scoped,
Action<dynamic> optionsBuilder = null
)
where TContext : XDbContext
{
//
if (provider == XDbProviders.None)
{
//
// Log("Invalid Db Provider ...");
XException.InvalidArgs.Throw();
}
//
switch (provider)
{
//
// MySQL ...
case XDbProviders.MySQL:
//
source.AddEntityFrameworkMySQL();
source.AddDbContext<TContext>(cfg =>
{
cfg.UseMySQL(connectionString, optionsBuilder);
}, lifetime);
break;
//
// SQLite ...
case XDbProviders.SQLite:
//
source.AddEntityFrameworkSqlite();
source.AddDbContext<TContext>(cfg =>
{
cfg.UseSqlite(connectionString, optionsBuilder);
}, lifetime);
break;
//
// SQLServer ...
case XDbProviders.SQLServer:
//
source.AddEntityFrameworkSqlServer();
source.AddDbContext<TContext>(cfg =>
{
cfg.UseSqlServer(connectionString, optionsBuilder);
}, lifetime);
break;
//
case XDbProviders.MongoDB:
// There is notRequired to Register DBConext forMongo DB ...
break;
}
//
var isValid =
provider == XDbProviders.MySQL ||
provider == XDbProviders.SQLite ||
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="TGraphQueryService"></typeparam>
/// <typeparam name="TGraphQueryImplementation"></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, TGraphQueryService, TGraphQueryImplementation, TGraphTypeHelperService, TGraphTypeHelperImplementation, TGraphSchema>(
IServiceCollection source,
XRepositoryDescriptor<TEntity, TKey, TKeyGeneratorService, TKeyGeneratorImplementation, TEventsService, TEventsImplementation, TGraph, TGraphKey, TGraphQueryService, TGraphQueryImplementation, 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 TGraphTypeHelperImplementation : XBaseGraphQLTypeHelper<TEntity, TKey>
where TGraphQueryService : IXBaseGraphQLQuery<TEntity, TKey, TGraph, TGraphKey>
where TGraphQueryImplementation : XBaseGraphQLQuery<TEntity, TKey, TGraph, TGraphKey>
{
//
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, TGraphQueryService, TGraphQueryImplementation, 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="TGraphQueryService"></typeparam>
/// <typeparam name="TGraphQueryImplementation"></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, TGraphQueryService, TGraphQueryImplementation, TGraphTypeHelperService, TGraphTypeHelperImplementation, TGraphSchema, TRepositoryService, TRepositoryImplementation>(
IServiceCollection source,
XInMemoryRepositoryDescriptor<TEntity, TKey, TKeyGeneratorService, TKeyGeneratorImplementation, TEventsService, TEventsImplementation, TGraph, TGraphKey, TGraphQueryService, TGraphQueryImplementation, 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 TRepositoryImplementation : XBaseInMemoryRepositoy<TEntity, TKey>
where TGraphTypeHelperImplementation : XBaseGraphQLTypeHelper<TEntity, TKey>
where TGraphQueryService : IXBaseGraphQLQuery<TEntity, TKey, TGraph, TGraphKey>
where TGraphQueryImplementation : XBaseGraphQLQuery<TEntity, TKey, TGraph, TGraphKey>
{
//
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, TGraphQueryService, TGraphQueryImplementation, 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="TGraphQueryService"></typeparam>
/// <typeparam name="TGraphQueryImplementation"></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, TGraphQueryService, TGraphQueryImplementation, TGraphTypeHelperService, TGraphTypeHelperImplementation, TGraphSchema, TRepositoryService, TRepositoryImplementation>(
IServiceCollection source,
XMongoRepositoryDescriptor<TEntity, TKey, TKeyGeneratorService, TKeyGeneratorImplementation, TEventsService, TEventsImplementation, TGraph, TGraphKey, TGraphQueryService, TGraphQueryImplementation, 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 TRepositoryImplementation : XBaseMongoRepository<TEntity, TKey>
where TGraphTypeHelperImplementation : XBaseGraphQLTypeHelper<TEntity, TKey>
where TGraphQueryService : IXBaseGraphQLQuery<TEntity, TKey, TGraph, TGraphKey>
where TGraphQueryImplementation : XBaseGraphQLQuery<TEntity, TKey, TGraph, TGraphKey>
{
//
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, TGraphQueryService, TGraphQueryImplementation, 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="TGraphQueryService"></typeparam>
/// <typeparam name="TGraphQueryImplementation"></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, TGraphQueryService, TGraphQueryImplementation, TGraphTypeHelperService, TGraphTypeHelperImplementation, TGraphSchema, TRepositoryService, TRepositoryImplementation, TContext>(
IServiceCollection source,
XEFRepositoryDescriptor<TEntity, TKey, TKeyGeneratorService, TKeyGeneratorImplementation, TEventsService, TEventsImplementation, TGraph, TGraphKey, TGraphQueryService, TGraphQueryImplementation, 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 TRepositoryImplementation : XBaseEFRepository<TEntity, TKey, TContext>
where TGraphTypeHelperImplementation : XBaseGraphQLTypeHelper<TEntity, TKey>
where TGraphQueryService : IXBaseGraphQLQuery<TEntity, TKey, TGraph, TGraphKey>
where TGraphQueryImplementation : XBaseGraphQLQuery<TEntity, TKey, TGraph, TGraphKey>
{
//
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, TGraphQueryService, TGraphQueryImplementation, 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="TGraphQueryService"></typeparam>
/// <typeparam name="TGraphQueryImplementation"></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 UseXRepository<TEntity, TKey, TKeyGeneratorService, TKeyGeneratorImplementation, TEventsService, TEventsImplementation, TGraph, TGraphKey, TGraphQueryService, TGraphQueryImplementation, TGraphTypeHelperService, TGraphTypeHelperImplementation, TGraphSchema, TRepositoryService, TRepositoryImplementation>(
IApplicationBuilder source,
XInMemoryRepositoryDescriptor<TEntity, TKey, TKeyGeneratorService, TKeyGeneratorImplementation, TEventsService, TEventsImplementation, TGraph, TGraphKey, TGraphQueryService, TGraphQueryImplementation, TGraphTypeHelperService, TGraphTypeHelperImplementation, TGraphSchema, TRepositoryService, TRepositoryImplementation> repositoryDescriptor
)
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 TRepositoryImplementation : XBaseInMemoryRepositoy<TEntity, TKey>
where TGraphTypeHelperImplementation : XBaseGraphQLTypeHelper<TEntity, TKey>
where TGraphQueryService : IXBaseGraphQLQuery<TEntity, TKey, TGraph, TGraphKey>
where TGraphQueryImplementation : XBaseGraphQLQuery<TEntity, TKey, TGraph, TGraphKey>
{
//
var isValid = repositoryDescriptor.IsValid();
if (!isValid)
{
return;
}
//
// GraphQL ...
isValid = repositoryDescriptor.HasGraphSupport();
if (isValid)
{
//
UseXGraphQL<TEntity, TKey, TGraph, TGraphKey, TGraphQueryService, TGraphQueryImplementation, TGraphTypeHelperService, TGraphTypeHelperImplementation, TGraphSchema>(
source,
repositoryDescriptor
);
}
}
/// <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="TGraphQueryService"></typeparam>
/// <typeparam name="TGraphQueryImplementation"></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 UseXRepository<TEntity, TKey, TKeyGeneratorService, TKeyGeneratorImplementation, TEventsService, TEventsImplementation, TGraph, TGraphKey, TGraphQueryService, TGraphQueryImplementation, TGraphTypeHelperService, TGraphTypeHelperImplementation, TGraphSchema, TRepositoryService, TRepositoryImplementation>(
IApplicationBuilder source,
XMongoRepositoryDescriptor<TEntity, TKey, TKeyGeneratorService, TKeyGeneratorImplementation, TEventsService, TEventsImplementation, TGraph, TGraphKey, TGraphQueryService, TGraphQueryImplementation, TGraphTypeHelperService, TGraphTypeHelperImplementation, TGraphSchema, TRepositoryService, TRepositoryImplementation> repositoryDescriptor
)
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 TRepositoryImplementation : XBaseMongoRepository<TEntity, TKey>
where TGraphTypeHelperImplementation : XBaseGraphQLTypeHelper<TEntity, TKey>
where TGraphQueryService : IXBaseGraphQLQuery<TEntity, TKey, TGraph, TGraphKey>
where TGraphQueryImplementation : XBaseGraphQLQuery<TEntity, TKey, TGraph, TGraphKey>
{
//
var isValid = repositoryDescriptor.IsValid();
if (!isValid)
{
return;
}
//
// GraphQL ...
isValid = repositoryDescriptor.HasGraphSupport();
if (isValid)
{
//
UseXGraphQL<TEntity, TKey, TGraph, TGraphKey, TGraphQueryService, TGraphQueryImplementation, TGraphTypeHelperService, TGraphTypeHelperImplementation, TGraphSchema>(
source,
repositoryDescriptor
);
}
}
/// <summary>
/// Register Repository ...
/// </summary>
/// <param name="source"></param>
/// <param name="repositoryDescriptor"></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="TGraphQueryService"></typeparam>
/// <typeparam name="TGraphQueryImplementation"></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 UseXRepository<TEntity, TKey, TKeyGeneratorService, TKeyGeneratorImplementation, TEventsService, TEventsImplementation, TGraph, TGraphKey, TGraphQueryService, TGraphQueryImplementation, TGraphTypeHelperService, TGraphTypeHelperImplementation, TGraphSchema, TRepositoryService, TRepositoryImplementation, TContext>(
IApplicationBuilder source,
XEFRepositoryDescriptor<TEntity, TKey, TKeyGeneratorService, TKeyGeneratorImplementation, TEventsService, TEventsImplementation, TGraph, TGraphKey, TGraphQueryService, TGraphQueryImplementation, TGraphTypeHelperService, TGraphTypeHelperImplementation, TGraphSchema, TRepositoryService, TRepositoryImplementation, TContext> repositoryDescriptor
)
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 TRepositoryImplementation : XBaseEFRepository<TEntity, TKey, TContext>
where TGraphTypeHelperImplementation : XBaseGraphQLTypeHelper<TEntity, TKey>
where TGraphQueryService : IXBaseGraphQLQuery<TEntity, TKey, TGraph, TGraphKey>
where TGraphQueryImplementation : XBaseGraphQLQuery<TEntity, TKey, TGraph, TGraphKey>
{
//
var isValid = repositoryDescriptor.IsValid();
if (!isValid)
{
return;
}
//
// GraphQL ...
isValid = repositoryDescriptor.HasGraphSupport();
if (isValid)
{
//
UseXGraphQL<TEntity, TKey, TGraph, TGraphKey, TGraphQueryService, TGraphQueryImplementation, TGraphTypeHelperService, TGraphTypeHelperImplementation, TGraphSchema>(
source,
repositoryDescriptor
);
}
}
#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="TGraphQueryService"></typeparam>
/// <typeparam name="TGraphQueryImplementation"></typeparam>
/// <typeparam name="TGraphTypeHelperService"></typeparam>
/// <typeparam name="TGraphTypeHelperImplementation"></typeparam>
/// <typeparam name="TGraphSchema"></typeparam>
/// <returns></returns>
public static void AddXGraphQL<TEntity, TKey, TGraph, TGraphKey, TGraphQueryService, TGraphQueryImplementation, 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 TGraphTypeHelperImplementation : XBaseGraphQLTypeHelper<TEntity, TKey>
where TGraphQueryService : IXBaseGraphQLQuery<TEntity, TKey, TGraph, TGraphKey>
where TGraphQueryImplementation : XBaseGraphQLQuery<TEntity, TKey, TGraph, TGraphKey>
{
//
// Validate ...
var isValid =
!source.IsNull() &&
!repositoryDescriptor.IsNull() &&
!repositoryDescriptor.GraphType.IsNull() &&
!repositoryDescriptor.GraphSchema.IsNull() &&
!repositoryDescriptor.GraphTypeKey.IsNull() &&
!repositoryDescriptor.GraphQueryService.IsNull() &&
!repositoryDescriptor.GraphTypeHelperService.IsNull() &&
!repositoryDescriptor.GraphQueryImplementation.IsNull() &&
!repositoryDescriptor.GraphTypeHelperImplementation.IsNull();
if (!isValid)
{
return;
}
//
// Register Graph Type ...
var builder = source.GetRegisteredService<IGraphQLBuilder>();
if (builder.IsNull())
{
//
Action<GraphQLOptions> options = x => { };
builder = source.AddGraphQL(options)
.AddNewtonsoftJson(deserializerSettings => { }, serializerSettings => { })
.AddWebSockets()
.AddDataLoader()
.AddGraphTypes();
}
builder.AddGraphTypes(repositoryDescriptor.GraphType);
//
// Register Graph Schema ...
source.Add(
new ServiceDescriptor(
repositoryDescriptor.GraphSchema,
repositoryDescriptor.GraphSchema,
lifetime
)
);
//
// Register Graph Query ...
source.Add(
new ServiceDescriptor(
repositoryDescriptor.GraphQueryService,
repositoryDescriptor.GraphQueryImplementation,
lifetime
)
);
//
// Register Graph Type Helper ...
source.Add(
new ServiceDescriptor(
repositoryDescriptor.GraphTypeHelperService,
repositoryDescriptor.GraphTypeHelperImplementation,
lifetime: ServiceLifetime.Singleton
)
);
}
/// <summary>
/// Use GraphQL Middleware ...
/// </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="TGraphQueryService"></typeparam>
/// <typeparam name="TGraphQueryImplementation"></typeparam>
/// <typeparam name="TGraphTypeHelperService"></typeparam>
/// <typeparam name="TGraphTypeHelperImplementation"></typeparam>
/// <typeparam name="TGraphSchema"></typeparam>
/// <returns></returns>
public static void UseXGraphQL<TEntity, TKey, TGraph, TGraphKey, TGraphQueryService, TGraphQueryImplementation, TGraphTypeHelperService, TGraphTypeHelperImplementation, TGraphSchema>(
IApplicationBuilder source,
XRepositoryDescriptor<TEntity, TKey> repositoryDescriptor
)
where TGraphSchema : Schema
where TGraphKey : IGraphType
where TEntity : XBaseEntity<TKey>
where TGraph : XBaseGraphObjectType<TEntity, TKey>
where TGraphTypeHelperService : IXBaseGraphQLTypeHelper<TEntity, TKey>
where TGraphTypeHelperImplementation : XBaseGraphQLTypeHelper<TEntity, TKey>
where TGraphQueryService : IXBaseGraphQLQuery<TEntity, TKey, TGraph, TGraphKey>
where TGraphQueryImplementation : XBaseGraphQLQuery<TEntity, TKey, TGraph, TGraphKey>
{
//
// Validate ...
var isValid =
!source.IsNull() &&
!repositoryDescriptor.IsNull() &&
!repositoryDescriptor.GraphType.IsNull() &&
!repositoryDescriptor.GraphSchema.IsNull() &&
!repositoryDescriptor.GraphTypeKey.IsNull() &&
!repositoryDescriptor.GraphQueryService.IsNull() &&
!repositoryDescriptor.GraphTypeHelperService.IsNull() &&
!repositoryDescriptor.GraphQueryImplementation.IsNull() &&
!repositoryDescriptor.GraphTypeHelperImplementation.IsNull();
if (!isValid)
{
return;
}
//
// Create an Scope for Services Acces ...
using (var scope = source.ApplicationServices.CreateScope())
{
//
// Inject GraphQL Type Helper ...
var helper = scope.ServiceProvider.GetService<TGraphTypeHelperService>();
//
source.UseGraphQL<TGraphSchema>(helper.GetGraphQLPath());
source.UseGraphQLWebSockets<TGraphSchema>();
}
}
#endregion
}
}