Files
xDataService/Extensions/XRepositoryDescriptorExtensions.cs
T
2026-05-15 02:51:02 +03:30

299 lines
9.1 KiB
C#

using xCommons.Extensions;
using xDataService.Db;
using xDataService.Interfaces;
using xDataService.Models;
using xDataService.Providers;
using xModels.Base;
namespace xDataService.Extensions
{
public static class XRepositoryDescriptorExtensions
{
//
#region Hasers ...
/// <summary>
/// Validate a Descriptor ...
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static bool IsValid(this XRepositoryDescriptor source)
{
var result =
!source.IsNull() &&
!source.Key.IsNull() &&
!source.Entity.IsNull();
//
return result;
}
/// <summary>
/// Check a Descriptor Has Events Service or not ...
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static bool HasEvents(this XRepositoryDescriptor source)
{
//
var result =
source.IsValid() &&
!source.EventsService.IsNull() &&
!source.EventsImplementation.IsNull();
//
return result;
}
/// <summary>
/// Check a Descriptor Has EF Core Context or not ...
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static bool HasContext(this XRepositoryDescriptor source)
{
//
var result =
source.IsValid() &&
!source.Context.IsNull();
//
return result;
}
/// <summary>
/// Check a Descriptor Has GraphQL Support or not ...
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static bool HasGraphSupport(this XRepositoryDescriptor source)
{
//
var result =
source.IsValid() &&
source.HasRepository() &&
!source.GraphType.IsNull() &&
!source.GraphQuery.IsNull() &&
!source.GraphSchema.IsNull() &&
!source.GraphTypeKey.IsNull() &&
!source.GraphTypeHelperService.IsNull() &&
!source.GraphTypeHelperImplementation.IsNull();
//
return result;
}
/// <summary>
/// Check a Descriptor Has Key Generator Service or not ...
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static bool HasKeyGenerator(this XRepositoryDescriptor source)
{
//
var result =
source.IsValid() &&
!source.KeyGeneratorService.IsNull() &&
!source.KeyGeneratorImplementation.IsNull();
//
return result;
}
/// <summary>
/// Check a Descriptor Has Repository Service or not ...
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static bool HasRepository(this XRepositoryDescriptor source)
{
//
var result =
source.IsValid() &&
!source.RepositoryService.IsNull() &&
!source.RepositoryImplementation.IsNull();
//
return result;
}
/// <summary>
/// Check a Descriptor Has Seeder or not ...
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static bool HasSeeder(this XRepositoryDescriptor source)
{
//
var result =
source.IsValid() &&
source.HasRepository() &&
!source.SeederService.IsNull() &&
!source.SeederImplementation.IsNull();
//
return result;
}
#endregion
//
#region Builders ...
public static XRepositoryDescriptor AddEntity<TEntity, TKey>(
this XRepositoryDescriptor source
)
where TEntity : XBaseEntity<TKey>
{
//
if (source.IsNull())
{
source = new XRepositoryDescriptor();
}
//
source.Key = typeof(TKey);
source.Entity = typeof(TEntity);
//
return source;
}
public static XRepositoryDescriptor AddEvents<TEntity, TKey, TEventsService, TEventsImplementation>(
this XRepositoryDescriptor source
)
where TEntity : XBaseEntity<TKey>
where TEventsService : IXBaseRepositoryEvents<TEntity>
where TEventsImplementation : XBaseRepositoryEvents<TEntity>
{
//
if (source.IsNull())
{
//
source = new XRepositoryDescriptor();
source = source.AddEntity<TEntity, TKey>();
}
//
source.EventsService = typeof(TEventsService);
source.EventsImplementation = typeof(TEventsImplementation);
//
return source;
}
public static XRepositoryDescriptor AddKeyGenerator<TEntity, TKey, TKeyGeneratorService, TKeyGeneratorImplementation>(
this XRepositoryDescriptor source
)
where TEntity : XBaseEntity<TKey>
where TKeyGeneratorService : IXKeyGenerator<TEntity, TKey>
where TKeyGeneratorImplementation : XKeyGenerator<TEntity, TKey>
{
//
if (source.IsNull())
{
//
source = new XRepositoryDescriptor();
source = source.AddEntity<TEntity, TKey>();
}
//
source.KeyGeneratorService = typeof(TKeyGeneratorService);
source.KeyGeneratorImplementation = typeof(TKeyGeneratorImplementation);
//
return source;
}
public static XRepositoryDescriptor AddSeeder<TEntity, TKey, TSeederService, TSeederImplementation>(
this XRepositoryDescriptor source
)
where TEntity : XBaseEntity<TKey>
where TSeederService : IXBaseDbSeeder<TEntity, TKey>
where TSeederImplementation : XBaseDbSeeder<TEntity, TKey>
{
//
if (source.IsNull())
{
//
source = new XRepositoryDescriptor();
source = source.AddEntity<TEntity, TKey>();
}
//
source.SeederService = typeof(TSeederService);
source.SeederImplementation = typeof(TSeederImplementation);
//
return source;
}
public static XRepositoryDescriptor AddEFRepository<TEntity, TKey, TContext, TRepositoryService, TRepositoryImplementation>(
this XRepositoryDescriptor source
)
where TContext : XDbContext
where TEntity : XBaseEntity<TKey>
where TRepositoryService : IXBaseRepository<TEntity, TKey>
where TRepositoryImplementation : XBaseEFRepository<TEntity, TKey, TContext>
{
//
if (source.IsNull())
{
//
source = new XRepositoryDescriptor();
source = source.AddEntity<TEntity, TKey>();
}
//
source.RepositoryService = typeof(TRepositoryService);
source.RepositoryImplementation = typeof(TRepositoryImplementation);
//
return source;
}
public static XRepositoryDescriptor AddMongoRepository<TEntity, TKey, TRepositoryService, TRepositoryImplementation>(
this XRepositoryDescriptor source
)
where TEntity : XBaseEntity<TKey>
where TRepositoryService : IXBaseRepository<TEntity, TKey>
where TRepositoryImplementation : XBaseMongoRepository<TEntity, TKey>
{
//
if (source.IsNull())
{
//
source = new XRepositoryDescriptor();
source = source.AddEntity<TEntity, TKey>();
}
//
source.RepositoryService = typeof(TRepositoryService);
source.RepositoryImplementation = typeof(TRepositoryImplementation);
//
return source;
}
public static XRepositoryDescriptor AddInMemoryRepository<TEntity, TKey, TRepositoryService, TRepositoryImplementation>(
this XRepositoryDescriptor source
)
where TEntity : XBaseEntity<TKey>
where TRepositoryService : IXBaseRepository<TEntity, TKey>
where TRepositoryImplementation : XBaseInMemoryRepository<TEntity, TKey>
{
//
if (source.IsNull())
{
//
source = new XRepositoryDescriptor();
source = source.AddEntity<TEntity, TKey>();
}
//
source.RepositoryService = typeof(TRepositoryService);
source.RepositoryImplementation = typeof(TRepositoryImplementation);
//
return source;
}
#endregion
}
}