last works on Implementing XBotUser Entity and Repository Implementation based on xDataService ...

This commit is contained in:
2026-11-11 15:47:44 +03:30
parent 609402b04c
commit 1e2eb030e8
23 changed files with 726 additions and 2 deletions
@@ -10,7 +10,7 @@ namespace xBotService.Configuration.Entities
{ {
public override void Configure(EntityTypeBuilder<XBotUser> builder) public override void Configure(EntityTypeBuilder<XBotUser> builder)
{ {
builder.ToTable(XBotServiceConstants.XBotUsersTable); builder.ToTable(XBotServiceConstants.XBotUserTable);
} }
} }
} }
+159
View File
@@ -0,0 +1,159 @@
using System;
using xDataService.Db;
using xDataService.DI;
using xCommons.Helpers;
using xCommons.Extensions;
using xDataService.Models;
using xExceptions.Constants;
using xBotService.Constants;
using xDataService.Constants;
using XBotUserService.Helpers;
using xHttpService.Interfaces;
using xBotService.Providers.Dtos;
using Microsoft.AspNetCore.Builder;
using xBotService.Interfaces.Entities.Dtos;
using Microsoft.Extensions.DependencyInjection;
namespace xBotService.DI
{
public static class XDIHelperExtension
{
/// <summary>
/// Register XBotService
/// </summary>
public static void AddXBotService(
this IServiceCollection services,
XRepositoryType repositoryType,
ServiceLifetime lifeTime = ServiceLifetime.Scoped
)
{
AddBotService(
contextType: null,
services: services,
lifeTime: lifeTime,
repositoryType: repositoryType
);
}
/// <summary>
/// Register XBotService with Context
/// </summary>
public static void AddXBotService<TContext>(
this IServiceCollection services,
XRepositoryType repositoryType,
ServiceLifetime lifeTime = ServiceLifetime.Scoped
) where TContext : XDbContext
{
AddBotService(
services: services,
lifeTime: lifeTime,
contextType: typeof(TContext),
repositoryType: repositoryType
);
}
/// <summary>
/// Use XBotService Middleware
/// </summary>
public static void UseXBotService(
this IApplicationBuilder app,
bool isDevelopmentEnvironment = false
)
{
// Using XBot Repository Descriptor
if (!descriptor.IsNull())
{
app.UseXRepository(
descriptor: descriptor,
isDevelopmentEnvironment: isDevelopmentEnvironment
);
}
}
//
#region Private ...
private static XRepositoryDescriptor descriptor = null;
private static string XLogTag = XBotServiceConstants.XBotServiceDILogTag;
private static void Log(string message)
{
Console.WriteLine($"{XLogTag} => {message}");
}
private static void AddBotService(
IServiceCollection services,
XRepositoryType repositoryType,
Type contextType = null,
ServiceLifetime lifeTime = ServiceLifetime.Scoped
)
{
//
var exception = XException.InvalidArgs.ToException();
//
// Validate Services
if (services.IsNull())
{
//
Log("Service Registration failed, services not provided ...");
throw exception;
}
//
// Check Requirements ...
var httpService = services.GetRegisteredService<IXHttpProvider>();
if (httpService.IsNull())
{
//
Log("Service Registration failed, HttpService not provided ...");
throw exception;
}
//
// Validate Repository Type
var isRepositoryTypeValid =
(repositoryType == XRepositoryType.EF && !contextType.IsNull()) ||
((repositoryType == XRepositoryType.Mongo || repositoryType == XRepositoryType.InMemory) && contextType.IsNull());
if (!isRepositoryTypeValid)
{
Log("Service Registration failed, Invalid Repository Type and Context Type ...");
throw exception;
}
//
// Make Instance of Repository Descriptor
switch (repositoryType)
{
case XRepositoryType.EF:
descriptor = typeof(XBotServiceHelper)
.InvokeGenericMethod<XRepositoryDescriptor>(
args: null,
runtimeType: contextType,
methodName: XBotServiceConstants.GetXBotUserEFRepositoryDescriptor
);
break;
case XRepositoryType.Mongo:
// descriptor = XBotServiceHelper.GetXBotUserMongoRepositoryDescriptor();
Log("MongoDB support not implemented yet for XBotService.");
break;
case XRepositoryType.InMemory:
// descriptor = XBotServiceHelper.GetXBotUserInMemoryRepositoryDescriptor();
Log("InMemory support not implemented yet for XBotService.");
break;
}
//
// Register XBotUser Repository Descriptor
if (!descriptor.IsNull())
{
services.AddXRepository(lifeTime: lifeTime, descriptor: descriptor);
}
//
// Register Dto Services
services.Add(new ServiceDescriptor(typeof(IXBotUserRepositoryService), typeof(XBotUserRepositoryService), lifeTime));
Log("XBotService Registration Succeed ...");
}
#endregion
}
}
+278
View File
@@ -0,0 +1,278 @@
using GraphQL.Types;
using xDataService.Db;
using xDataService.Models;
using xDataService.Providers;
using xBotService.Models.Entities;
using xBotService.Providers.GraphQL;
using xBotService.Providers.Entities;
using xBotService.Interfaces.Entities;
namespace XBotUserService.Helpers
{
public static class XBotServiceHelper
{
//
#region EF Repository Descriptor Provider(s) ...
public static XEFRepositoryDescriptor<
XBotUser,
int,
IXBotUserKeyGenerator,
XBotUserKeyGenerator,
IXBotUserRepositoryEvents,
XBotUserRepositoryEvents,
XBotUserGraphType,
IntGraphType,
XBotUserGraphQuery,
IXBotUserGraphQLTypeHelper,
XBotUserGraphQLTypeHelper,
XBotUserGraphSchema,
IXBotUserRepository,
XBotUserEFRepository<TContext>,
TContext
> GetXBotUserEFRepositoryDescriptor<TContext>()
where TContext : XDbContext
{
//
var result = new XEFRepositoryDescriptor<
XBotUser,
int,
IXBotUserKeyGenerator,
XBotUserKeyGenerator,
IXBotUserRepositoryEvents,
XBotUserRepositoryEvents,
XBotUserGraphType,
IntGraphType,
XBotUserGraphQuery,
IXBotUserGraphQLTypeHelper,
XBotUserGraphQLTypeHelper,
XBotUserGraphSchema,
IXBotUserRepository,
XBotUserEFRepository<TContext>,
TContext
>();
//
return result;
}
public static XEFRepositoryDescriptor<
XBotUser,
int,
IXBotUserKeyGenerator,
XBotUserKeyGenerator,
IXBotUserRepositoryEvents,
XBotUserRepositoryEvents,
XBotUserGraphType,
IntGraphType,
XBotUserGraphQuery,
IXBotUserGraphQLTypeHelper,
XBotUserGraphQLTypeHelper,
XBotUserGraphSchema,
IXBotUserRepository,
XBotUserEFRepository<TContext>,
TContext,
IXBotUserSeeder,
TSeederImplementation
> GetXBotUserEFRepositoryDescriptor<TContext, TSeederImplementation>()
where TContext : XDbContext
where TSeederImplementation : XBaseDbSeeder<XBotUser, int>
{
//
var result = new XEFRepositoryDescriptor<
XBotUser,
int,
IXBotUserKeyGenerator,
XBotUserKeyGenerator,
IXBotUserRepositoryEvents,
XBotUserRepositoryEvents,
XBotUserGraphType,
IntGraphType,
XBotUserGraphQuery,
IXBotUserGraphQLTypeHelper,
XBotUserGraphQLTypeHelper,
XBotUserGraphSchema,
IXBotUserRepository,
XBotUserEFRepository<TContext>,
TContext,
IXBotUserSeeder,
TSeederImplementation
>();
//
return result;
}
#endregion
//
#region Mongo Repository Descriptor Provider(s) ...
public static XMongoRepositoryDescriptor<
XBotUser,
int,
IXBotUserKeyGenerator,
XBotUserKeyGenerator,
IXBotUserRepositoryEvents,
XBotUserRepositoryEvents,
XBotUserGraphType,
IntGraphType,
XBotUserGraphQuery,
IXBotUserGraphQLTypeHelper,
XBotUserGraphQLTypeHelper,
XBotUserGraphSchema,
IXBotUserRepository,
XBotUserMongoRepository
> GetXBotUserMongoRepositoryDescriptor()
{
//
var result = new XMongoRepositoryDescriptor<
XBotUser,
int,
IXBotUserKeyGenerator,
XBotUserKeyGenerator,
IXBotUserRepositoryEvents,
XBotUserRepositoryEvents,
XBotUserGraphType,
IntGraphType,
XBotUserGraphQuery,
IXBotUserGraphQLTypeHelper,
XBotUserGraphQLTypeHelper,
XBotUserGraphSchema,
IXBotUserRepository,
XBotUserMongoRepository
>();
//
return result;
}
public static XMongoRepositoryDescriptor<
XBotUser,
int,
IXBotUserKeyGenerator,
XBotUserKeyGenerator,
IXBotUserRepositoryEvents,
XBotUserRepositoryEvents,
XBotUserGraphType,
IntGraphType,
XBotUserGraphQuery,
IXBotUserGraphQLTypeHelper,
XBotUserGraphQLTypeHelper,
XBotUserGraphSchema,
IXBotUserRepository,
XBotUserMongoRepository,
IXBotUserSeeder,
TSeederImplementation
> GetXBotUserMongoRepositoryDescriptor<TSeederImplementation>()
where TSeederImplementation : XBaseDbSeeder<XBotUser, int>
{
//
var result = new XMongoRepositoryDescriptor<
XBotUser,
int,
IXBotUserKeyGenerator,
XBotUserKeyGenerator,
IXBotUserRepositoryEvents,
XBotUserRepositoryEvents,
XBotUserGraphType,
IntGraphType,
XBotUserGraphQuery,
IXBotUserGraphQLTypeHelper,
XBotUserGraphQLTypeHelper,
XBotUserGraphSchema,
IXBotUserRepository,
XBotUserMongoRepository,
IXBotUserSeeder,
TSeederImplementation
>();
//
return result;
}
#endregion
//
#region InMemory Repository Descriptor Provider(s) ...
public static XInMemoryRepositoryDescriptor<
XBotUser,
int,
IXBotUserKeyGenerator,
XBotUserKeyGenerator,
IXBotUserRepositoryEvents,
XBotUserRepositoryEvents,
XBotUserGraphType,
IntGraphType,
XBotUserGraphQuery,
IXBotUserGraphQLTypeHelper,
XBotUserGraphQLTypeHelper,
XBotUserGraphSchema,
IXBotUserRepository,
XBotUserInMemoryRepository
> GetXBotUserInMemoryRepositoryDescriptor()
{
//
var result = new XInMemoryRepositoryDescriptor<
XBotUser,
int,
IXBotUserKeyGenerator,
XBotUserKeyGenerator,
IXBotUserRepositoryEvents,
XBotUserRepositoryEvents,
XBotUserGraphType,
IntGraphType,
XBotUserGraphQuery,
IXBotUserGraphQLTypeHelper,
XBotUserGraphQLTypeHelper,
XBotUserGraphSchema,
IXBotUserRepository,
XBotUserInMemoryRepository
>();
//
return result;
}
public static XInMemoryRepositoryDescriptor<
XBotUser,
int,
IXBotUserKeyGenerator,
XBotUserKeyGenerator,
IXBotUserRepositoryEvents,
XBotUserRepositoryEvents,
XBotUserGraphType,
IntGraphType,
XBotUserGraphQuery,
IXBotUserGraphQLTypeHelper,
XBotUserGraphQLTypeHelper,
XBotUserGraphSchema,
IXBotUserRepository,
XBotUserInMemoryRepository,
IXBotUserSeeder,
TSeederImplementation
> GetXBotUserInMemoryRepositoryDescriptor<TSeederImplementation>()
where TSeederImplementation : XBaseDbSeeder<XBotUser, int>
{
//
var result = new XInMemoryRepositoryDescriptor<
XBotUser,
int,
IXBotUserKeyGenerator,
XBotUserKeyGenerator,
IXBotUserRepositoryEvents,
XBotUserRepositoryEvents,
XBotUserGraphType,
IntGraphType,
XBotUserGraphQuery,
IXBotUserGraphQLTypeHelper,
XBotUserGraphQLTypeHelper,
XBotUserGraphSchema,
IXBotUserRepository,
XBotUserInMemoryRepository,
IXBotUserSeeder,
TSeederImplementation
>();
//
return result;
}
#endregion
}
}
+9
View File
@@ -0,0 +1,9 @@
using xBotService.Models.Dtos;
using xPushService.Interfaces;
using xBotService.Models.Entities;
namespace xBotService.Interfaces.Dtos
{
public interface IXBotUserDtoHub : IXBaseDtoHub<XBotUser, XBotUserDto, int>
{ }
}
@@ -0,0 +1,10 @@
using xBotService.Models.Dtos;
using xPushService.Interfaces;
using xBotService.Models.Entities;
using xBotService.Providers.Dtos;
namespace xBotService.Interfaces.Dtos
{
public interface IXBotUserServiceProvider : IXBaseDtoProvider<XBotUser, XBotUserDto, int, XBotUserDtoHub>
{ }
}
@@ -0,0 +1,8 @@
using xPushService.Interfaces;
using xBotService.Models.Entities;
namespace xBotService.Interfaces.Entities
{
public interface IXBotUserEntityHub : IXBaseEntityHub<XBotUser, int>
{ }
}
@@ -0,0 +1,8 @@
using xDataService.Interfaces;
using xBotService.Models.Entities;
namespace xBotService.Interfaces.Entities
{
public interface IXBotUserGraphQLTypeHelper : IXBaseGraphQLTypeHelper<XBotUser, int>
{ }
}
@@ -0,0 +1,9 @@
using xPushService.Interfaces;
using xBotService.Models.Entities;
using xBotService.Providers.Entities;
namespace xBotService.Interfaces.Entities
{
public interface IXBotUserRepositoryProvider : IXBaseEntityProvider<XBotUser, int, XBotUserEntityHub>
{ }
}
+8
View File
@@ -0,0 +1,8 @@
using xDataService.Interfaces;
using xBotService.Models.Entities;
namespace xBotService.Interfaces.Entities
{
public interface IXBotUserSeeder: IXBaseDbSeeder<XBotUser, int>
{ }
}
+16
View File
@@ -0,0 +1,16 @@
using xPushService.Base;
using xBotService.Models.Dtos;
using xBotService.Interfaces.Dtos;
using xBotService.Models.Entities;
using Microsoft.Extensions.Logging;
namespace xBotService.Providers.Dtos
{
public class XBotUserDtoHub : XBaseDtoHub<XBotUser, XBotUserDto, int>, IXBotUserDtoHub
{
public XBotUserDtoHub(
ILogger<XBotUserDtoHub> logger
) : base(logger)
{ }
}
}
+27
View File
@@ -0,0 +1,27 @@
using xPushService.Base;
using xBotService.Models.Dtos;
using xDataService.Configuration;
using xBotService.Interfaces.Dtos;
using xBotService.Models.Entities;
using xIdentityService.Interfaces;
using Microsoft.AspNetCore.SignalR;
using xBotService.Interfaces.Entities.Dtos;
namespace xBotService.Providers.Dtos
{
public class XBotUserServiceProvider : XBaseDtoProvider<XBotUser, XBotUserDto, int, XBotUserDtoHub>, IXBotUserServiceProvider
{
public XBotUserServiceProvider(
IHubContext<XBotUserDtoHub> hub,
XDataServiceConfiguration dataConfiguration,
IXBotUserRepositoryService service,
IXIdentityProvider identityProvider = null
) : base(
hub,
dataConfiguration,
service,
identityProvider
)
{ }
}
}
+15
View File
@@ -0,0 +1,15 @@
using xPushService.Base;
using xBotService.Models.Entities;
using Microsoft.Extensions.Logging;
using xBotService.Interfaces.Entities;
namespace xBotService.Providers.Entities
{
public class XBotUserEntityHub : XBaseEntityHub<XBotUser, int>, IXBotUserEntityHub
{
public XBotUserEntityHub(
ILogger<XBotUserEntityHub> logger
) : base(logger)
{ }
}
}
@@ -0,0 +1,21 @@
using xDataService.Providers;
using xDataService.Configuration;
using xBotService.Models.Entities;
using xBotService.Interfaces.Entities;
namespace xBotService.Providers.Entities
{
public class XBotUserInMemoryRepository : XBaseInMemoryRepository<XBotUser, int>, IXBotUserRepository
{
public XBotUserInMemoryRepository(
XDataServiceConfiguration configuration,
IXBotUserKeyGenerator keyGenerator = null,
IXBotUserRepositoryEvents baseRepositoryEvents = null
) : base(
configuration,
keyGenerator,
baseRepositoryEvents
)
{ }
}
}
@@ -0,0 +1,25 @@
using xDataService.Providers;
using xDataService.Configuration;
using xBotService.Models.Entities;
using xBotService.Interfaces.Entities;
namespace xBotService.Providers.Entities
{
public class XBotUserMongoRepository : XBaseMongoRepository<XBotUser, int>, IXBotUserRepository
{
public XBotUserMongoRepository(
XDataBaseConfiguration dbConfiguration,
XDataServiceConfiguration configuration,
string collectionName = null,
IXBotUserKeyGenerator keyGenerator = null,
IXBotUserRepositoryEvents baseRepositoryEvents = null
) : base(
dbConfiguration,
configuration,
collectionName,
keyGenerator,
baseRepositoryEvents
)
{ }
}
}
+23
View File
@@ -0,0 +1,23 @@
using xDataService.Providers;
using xDataService.Configuration;
using xBotService.Models.Entities;
using xBotService.Interfaces.Entities;
namespace xBotService.Providers.Entities
{
public abstract class XBotUserSeederBase : XBaseDbSeeder<XBotUser, int>, IXBotUserSeeder
{
protected XBotUserSeederBase(
string entity,
string database,
IXBotUserRepository repository,
XDataServiceConfiguration dataServiceConfiguration
) : base(
entity,
database,
repository,
dataServiceConfiguration
)
{ }
}
}
@@ -0,0 +1,28 @@
using xDataService.GraphQL;
using xDataService.Configuration;
using xBotService.Models.Entities;
using xBotService.Interfaces.Entities;
using xBotService.Constants;
namespace xBotService.Providers.GraphQL
{
public class XBotUserGraphQLTypeHelper : XBaseGraphQLTypeHelper<XBotUser, int>, IXBotUserGraphQLTypeHelper
{
public XBotUserGraphQLTypeHelper(
XDataServiceConfiguration configuration
)
: base(configuration)
{
}
public override string GetInQueryCollectionName()
{
return XBotServiceConstants.XBotUserCollectionName;
}
public override string GetInQuerySingleName()
{
return XBotServiceConstants.XBotUserSingleName;
}
}
}
+18
View File
@@ -0,0 +1,18 @@
using GraphQL.Types;
using xDataService.GraphQL;
using xDataService.Configuration;
using xBotService.Models.Entities;
using xBotService.Interfaces.Entities;
namespace xBotService.Providers.GraphQL
{
public class XBotUserGraphQuery : XBaseGraphQLQuery<XBotUser, int, XBotUserGraphType, IntGraphType>
{
public XBotUserGraphQuery(
XDataServiceConfiguration configuration,
IXBotUserRepository repository,
IXBotUserGraphQLTypeHelper helper
) : base(configuration, repository, helper)
{ }
}
}
+13
View File
@@ -0,0 +1,13 @@
using System;
using GraphQL.Types;
namespace xBotService.Providers.GraphQL
{
public class XBotUserGraphSchema : Schema
{
public XBotUserGraphSchema(IServiceProvider services) : base(services)
{
Query = (XBotUserGraphQuery)services.GetService(typeof(XBotUserGraphQuery));
}
}
}
+14
View File
@@ -0,0 +1,14 @@
using xDataService.GraphQL;
using xBotService.Models.Entities;
namespace xBotService.Providers.GraphQL
{
public class XBotUserGraphType : XBaseGraphObjectType<XBotUser, int>
{
public XBotUserGraphType() : base()
{
Field(x => x.UserId);
Field(x => x.BotUserName);
}
}
}
+34
View File
@@ -0,0 +1,34 @@
using xDataService.Providers;
using xDataService.Interfaces;
using xBotService.Models.Entities;
using Microsoft.EntityFrameworkCore;
using xBotService.Configuration.Entities;
namespace xBotService.Providers
{
public class XBotUserEntityRegisterar : XBaseEntityRegisterar, IXEntityRegisterar
{
public XBotUserEntityRegisterar()
: base(nameof(XBotUserEntityRegisterar))
{
//
// Add XBotUser Entity ...
AddEntity<XBotUser, int>();
}
/// <summary>
/// Configure Entities ...
/// </summary>
/// <param name="modelBuilder"></param>
public override void ConfigureEntities(ModelBuilder modelBuilder)
{
//
// Configure Entity Using Provided ...
modelBuilder
.ApplyConfigurationsFromAssembly(typeof(XBotUserEntityConfiguration).Assembly);
//
base.ConfigureEntities(modelBuilder);
}
}
}
+1 -1
View File
@@ -1,4 +1,4 @@
# xLoggingService # xBotService
provide all commonly used actions/extensions and classes which required to Bot Management Feature in xDashboard project. provide all commonly used actions/extensions and classes which required to Bot Management Feature in xDashboard project.
+1
View File
@@ -30,6 +30,7 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\xHttpService\xHttpService.csproj" /> <ProjectReference Include="..\xHttpService\xHttpService.csproj" />
<ProjectReference Include="..\xDataService\xDataService.csproj" /> <ProjectReference Include="..\xDataService\xDataService.csproj" />
<ProjectReference Include="..\xPushService\xPushService.csproj" />
</ItemGroup> </ItemGroup>
<!-- Dependencies --> <!-- Dependencies -->