Files
xDataService/Helpers/XDataServiceHelper.cs
T

1736 lines
66 KiB
C#

using System;
using GraphQL.Server;
using GraphQL.Server.Ui.Playground;
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.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="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;
//
// 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.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>
/// 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 UseXRepository<
TEntity,
TKey,
TKeyGeneratorService,
TKeyGeneratorImplementation,
TEventsService,
TEventsImplementation,
TGraph,
TGraphKey,
TGraphQuery,
TGraphTypeHelperService,
TGraphTypeHelperImplementation,
TGraphSchema,
TRepositoryService,
TRepositoryImplementation
>(
IApplicationBuilder source,
XInMemoryRepositoryDescriptor<TEntity, TKey, TKeyGeneratorService, TKeyGeneratorImplementation, TEventsService, TEventsImplementation, TGraph, TGraphKey, TGraphQuery, 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 : XBaseInMemoryRepository<TEntity, TKey>
where TGraphQuery : XBaseGraphQLQuery<TEntity, TKey, TGraph, TGraphKey>
where TGraphTypeHelperImplementation : XBaseGraphQLTypeHelper<TEntity, TKey>
{
//
var isValid = repositoryDescriptor.IsValid();
if (!isValid)
{
return;
}
//
// GraphQL ...
isValid = repositoryDescriptor.HasGraphSupport();
if (isValid)
{
//
UseXGraphQL<TEntity, TKey, TGraph, TGraphKey, TGraphQuery, 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="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 UseXRepository<
TEntity,
TKey,
TKeyGeneratorService,
TKeyGeneratorImplementation,
TEventsService,
TEventsImplementation,
TGraph,
TGraphKey,
TGraphQuery,
TGraphTypeHelperService,
TGraphTypeHelperImplementation,
TGraphSchema,
TRepositoryService,
TRepositoryImplementation
>(
IApplicationBuilder source,
XMongoRepositoryDescriptor<TEntity, TKey, TKeyGeneratorService, TKeyGeneratorImplementation, TEventsService, TEventsImplementation, TGraph, TGraphKey, TGraphQuery, 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 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;
}
//
// GraphQL ...
isValid = repositoryDescriptor.HasGraphSupport();
if (isValid)
{
//
UseXGraphQL<TEntity, TKey, TGraph, TGraphKey, TGraphQuery, 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="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 UseXRepository<
// TEntity,
// TKey,
// TKeyGeneratorService,
// TKeyGeneratorImplementation,
// TEventsService,
// TEventsImplementation,
// TGraph,
// TGraphKey,
// TGraphQuery,
// TGraphTypeHelperService,
// TGraphTypeHelperImplementation,
// TGraphSchema,
// TRepositoryService,
// TRepositoryImplementation,
// TContext
// >(
// IApplicationBuilder source,
// XEFRepositoryDescriptor<TEntity, TKey, TKeyGeneratorService, TKeyGeneratorImplementation, TEventsService, TEventsImplementation, TGraph, TGraphKey, TGraphQuery, 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 TGraphQuery : XBaseGraphQLQuery<TEntity, TKey, TGraph, TGraphKey>
// where TRepositoryImplementation : XBaseEFRepository<TEntity, TKey, TContext>
// where TGraphTypeHelperImplementation : XBaseGraphQLTypeHelper<TEntity, TKey>
// {
// //
// var isValid = repositoryDescriptor.IsValid();
// if (!isValid)
// {
// return;
// }
// //
// // GraphQL ...
// isValid = repositoryDescriptor.HasGraphSupport();
// if (isValid)
// {
// //
// UseXGraphQL<TEntity, TKey, TGraph, TGraphKey, TGraphQuery, TGraphTypeHelperService, TGraphTypeHelperImplementation, TGraphSchema>(
// source,
// repositoryDescriptor
// );
// }
// }
public static void UseXGraphQL<
TEntity,
TKey,
TGraph,
TGraphKey,
TGraphQuery,
TGraphTypeHelperService,
TGraphTypeHelperImplementation,
TGraphSchema
>(
IApplicationBuilder source,
string graphPath,
bool withPlayGround = false,
GraphQLPlaygroundOptions options = null
)
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(options);
}
}
#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 Schema ...
source.Add(
new ServiceDescriptor(
repositoryDescriptor.GraphSchema,
repositoryDescriptor.GraphSchema,
lifetime
)
);
//
// Register Graph Query ...
source.Add(
new ServiceDescriptor(
repositoryDescriptor.GraphQuery,
repositoryDescriptor.GraphQuery,
lifetime
)
);
//
// Register Graph Type Helper ...
source.Add(
new ServiceDescriptor(
repositoryDescriptor.GraphTypeHelperService,
repositoryDescriptor.GraphTypeHelperImplementation,
lifetime: ServiceLifetime.Singleton
)
);
//
// Register Graph Type ...
source.Add(
new ServiceDescriptor(
repositoryDescriptor.GraphType,
repositoryDescriptor.GraphType,
ServiceLifetime.Singleton
)
);
//
// Register Graph Events ...
source.Add(
new ServiceDescriptor(
typeof(XBaseGraphQLEventType<TEntity, TKey, TGraph>),
typeof(XBaseGraphQLEventType<TEntity, TKey, TGraph>),
ServiceLifetime.Singleton
)
);
//
// Register Graph Type Schema ...
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.GraphSchema);
}
/// <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="TGraphQuery"></typeparam>
/// <typeparam name="TGraphTypeHelperService"></typeparam>
/// <typeparam name="TGraphTypeHelperImplementation"></typeparam>
/// <typeparam name="TGraphSchema"></typeparam>
/// <returns></returns>
public static void UseXGraphQL<
TEntity,
TKey,
TGraph,
TGraphKey,
TGraphQuery,
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 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;
}
//
// 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
}
}