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 ...
///
/// Validate a Descriptor ...
///
///
///
public static bool IsValid(this XRepositoryDescriptor source)
{
var result =
!source.IsNull() &&
!source.Key.IsNull() &&
!source.Entity.IsNull();
//
return result;
}
///
/// Check a Descriptor Has Events Service or not ...
///
///
///
public static bool HasEvents(this XRepositoryDescriptor source)
{
//
var result =
source.IsValid() &&
!source.EventsService.IsNull() &&
!source.EventsImplementation.IsNull();
//
return result;
}
///
/// Check a Descriptor Has EF Core Context or not ...
///
///
///
public static bool HasContext(this XRepositoryDescriptor source)
{
//
var result =
source.IsValid() &&
!source.Context.IsNull();
//
return result;
}
///
/// Check a Descriptor Has GraphQL Support or not ...
///
///
///
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;
}
///
/// Check a Descriptor Has Key Generator Service or not ...
///
///
///
public static bool HasKeyGenerator(this XRepositoryDescriptor source)
{
//
var result =
source.IsValid() &&
!source.KeyGeneratorService.IsNull() &&
!source.KeyGeneratorImplementation.IsNull();
//
return result;
}
///
/// Check a Descriptor Has Repository Service or not ...
///
///
///
public static bool HasRepository(this XRepositoryDescriptor source)
{
//
var result =
source.IsValid() &&
!source.RepositoryService.IsNull() &&
!source.RepositoryImplementation.IsNull();
//
return result;
}
///
/// Check a Descriptor Has Seeder or not ...
///
///
///
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(
this XRepositoryDescriptor source
)
where TEntity : XBaseEntity
{
//
if (source.IsNull())
{
source = new XRepositoryDescriptor();
}
//
source.Key = typeof(TKey);
source.Entity = typeof(TEntity);
//
return source;
}
public static XRepositoryDescriptor AddEvents(
this XRepositoryDescriptor source
)
where TEntity : XBaseEntity
where TEventsService : IXBaseRepositoryEvents
where TEventsImplementation : XBaseRepositoryEvents
{
//
if (source.IsNull())
{
//
source = new XRepositoryDescriptor();
source = source.AddEntity();
}
//
source.EventsService = typeof(TEventsService);
source.EventsImplementation = typeof(TEventsImplementation);
//
return source;
}
public static XRepositoryDescriptor AddKeyGenerator(
this XRepositoryDescriptor source
)
where TEntity : XBaseEntity
where TKeyGeneratorService : IXKeyGenerator
where TKeyGeneratorImplementation : XKeyGenerator
{
//
if (source.IsNull())
{
//
source = new XRepositoryDescriptor();
source = source.AddEntity();
}
//
source.KeyGeneratorService = typeof(TKeyGeneratorService);
source.KeyGeneratorImplementation = typeof(TKeyGeneratorImplementation);
//
return source;
}
public static XRepositoryDescriptor AddSeeder(
this XRepositoryDescriptor source
)
where TEntity : XBaseEntity
where TSeederService : IXBaseDbSeeder
where TSeederImplementation : XBaseDbSeeder
{
//
if (source.IsNull())
{
//
source = new XRepositoryDescriptor();
source = source.AddEntity();
}
//
source.SeederService = typeof(TSeederService);
source.SeederImplementation = typeof(TSeederImplementation);
//
return source;
}
public static XRepositoryDescriptor AddEFRepository(
this XRepositoryDescriptor source
)
where TContext : XDbContext
where TEntity : XBaseEntity
where TRepositoryService : IXBaseRepository
where TRepositoryImplementation : XBaseEFRepository
{
//
if (source.IsNull())
{
//
source = new XRepositoryDescriptor();
source = source.AddEntity();
}
//
source.RepositoryService = typeof(TRepositoryService);
source.RepositoryImplementation = typeof(TRepositoryImplementation);
//
return source;
}
public static XRepositoryDescriptor AddMongoRepository(
this XRepositoryDescriptor source
)
where TEntity : XBaseEntity
where TRepositoryService : IXBaseRepository
where TRepositoryImplementation : XBaseMongoRepository
{
//
if (source.IsNull())
{
//
source = new XRepositoryDescriptor();
source = source.AddEntity();
}
//
source.RepositoryService = typeof(TRepositoryService);
source.RepositoryImplementation = typeof(TRepositoryImplementation);
//
return source;
}
public static XRepositoryDescriptor AddInMemoryRepository(
this XRepositoryDescriptor source
)
where TEntity : XBaseEntity
where TRepositoryService : IXBaseRepository
where TRepositoryImplementation : XBaseInMemoryRepository
{
//
if (source.IsNull())
{
//
source = new XRepositoryDescriptor();
source = source.AddEntity();
}
//
source.RepositoryService = typeof(TRepositoryService);
source.RepositoryImplementation = typeof(TRepositoryImplementation);
//
return source;
}
#endregion
}
}