241 lines
8.0 KiB
C#
241 lines
8.0 KiB
C#
using System;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using MongoDB.Bson.Serialization.Conventions;
|
|
using xAiApi.AI.DataHelper.Events;
|
|
using xAiApi.AI.Interfaces.Entities;
|
|
using xAiApi.AI.Models.Entities;
|
|
using xAiApi.Data.Events;
|
|
using xAiApi.Data.Extensions;
|
|
using xAiApi.Data.Interfaces;
|
|
using xAiApi.Data.Models.Entities;
|
|
using xAiApi.Data.Repositories.Ef;
|
|
using xAiApi.Data.Repositories.Mongo;
|
|
using xAiApi.Data.Seeder;
|
|
using xCommons.Extensions;
|
|
using xDataService.Configuration;
|
|
using xDataService.Constants;
|
|
using xDataService.Interfaces;
|
|
using xDataService.Providers;
|
|
using xExceptions.Constants;
|
|
|
|
namespace xAiApi.Data.Helpers
|
|
{
|
|
public class XAiApiDataProviderHelper : IXDataServiceHelper
|
|
{
|
|
//
|
|
private readonly ILogger<XAiApiDataProviderHelper> logger;
|
|
|
|
//
|
|
public XAiApiDataProviderHelper(ILogger<XAiApiDataProviderHelper> logger)
|
|
{
|
|
this.logger = logger;
|
|
}
|
|
|
|
//
|
|
#region Actions ...
|
|
public void AddDbContext(
|
|
IServiceCollection services,
|
|
string connectionString,
|
|
XDbProviders dbProvider,
|
|
ServiceLifetime lifetime,
|
|
Action<dynamic> optionsBuilder = null
|
|
)
|
|
{
|
|
//
|
|
logger.LogInformation("Start AddDbContext ...");
|
|
|
|
//
|
|
switch (dbProvider)
|
|
{
|
|
//
|
|
case XDbProviders.MySQL:
|
|
//
|
|
services.AddDbContext<XAiApiDbContext>(cfg =>
|
|
{
|
|
cfg.UseMySQL(connectionString, optionsBuilder);
|
|
}, lifetime);
|
|
break;
|
|
|
|
//
|
|
case XDbProviders.SQLite:
|
|
//
|
|
services.AddDbContext<XAiApiDbContext>(cfg =>
|
|
{
|
|
cfg.UseSqlite(connectionString, optionsBuilder);
|
|
}, lifetime);
|
|
break;
|
|
|
|
//
|
|
case XDbProviders.SQLServer:
|
|
//
|
|
services.AddDbContext<XAiApiDbContext>(cfg =>
|
|
{
|
|
cfg.UseSqlServer(connectionString, optionsBuilder);
|
|
}, lifetime);
|
|
break;
|
|
|
|
//
|
|
case XDbProviders.MongoDB:
|
|
break;
|
|
|
|
//
|
|
default:
|
|
Console.WriteLine("Unsuported Data Provider Type ...");
|
|
XException.NotAllowed.Throw();
|
|
break;
|
|
}
|
|
|
|
//
|
|
logger.LogInformation("End AddDbContext ...");
|
|
}
|
|
|
|
public void AddRepositories(
|
|
IServiceCollection services,
|
|
ServiceLifetime lifetime
|
|
)
|
|
{
|
|
//
|
|
logger.LogInformation("Start AddRepositories ...");
|
|
|
|
//
|
|
var dataServiceConfiguration = services.GetRegisteredService<XDataServiceConfiguration>();
|
|
|
|
//
|
|
#region Events ...
|
|
//
|
|
// XTest ...
|
|
services.Add(new ServiceDescriptor(typeof(IXTestEvents), typeof(XTestEvents), ServiceLifetime.Singleton));
|
|
|
|
//
|
|
// XAiProject ...
|
|
services.Add(new ServiceDescriptor(typeof(IXAiProjectEvents), typeof(XAiProjectEvents), ServiceLifetime.Singleton));
|
|
|
|
//
|
|
// XAiMessage ...
|
|
services.Add(new ServiceDescriptor(typeof(IXAiMessageEvents), typeof(XAiMessageEvents), ServiceLifetime.Singleton));
|
|
|
|
//
|
|
// XAiConversation ...
|
|
services.Add(new ServiceDescriptor(typeof(IXAiConversationEvents), typeof(XAiConversationEvents), ServiceLifetime.Singleton));
|
|
#endregion
|
|
|
|
//
|
|
#region Repositories ...
|
|
//
|
|
// Adding DbContext if EFCore ...
|
|
switch (dataServiceConfiguration.Provider)
|
|
{
|
|
//
|
|
case XDbProviders.MySQL:
|
|
case XDbProviders.SQLite:
|
|
case XDbProviders.SQLServer:
|
|
//
|
|
// XTest ...
|
|
services.Add(new ServiceDescriptor(typeof(IXTestRepository), typeof(XTestEfRepository<XAiApiDbContext>), lifetime));
|
|
|
|
//
|
|
// XAiProject ...
|
|
services.Add(new ServiceDescriptor(typeof(IXAiProjectRepository), typeof(XAiProjectEfRepository<XAiApiDbContext>), lifetime));
|
|
|
|
//
|
|
// XAiMessage ...
|
|
services.Add(new ServiceDescriptor(typeof(IXAiMessageRepository), typeof(XAiMessageEfRepository<XAiApiDbContext>), lifetime));
|
|
|
|
//
|
|
// XAiConversation ...
|
|
services.Add(new ServiceDescriptor(typeof(IXAiConversationRepository), typeof(XAiConversationEfRepository<XAiApiDbContext>), lifetime));
|
|
break;
|
|
|
|
//
|
|
case XDbProviders.MongoDB:
|
|
//
|
|
// XTest ...
|
|
services.Add(new ServiceDescriptor(typeof(IXTestRepository), typeof(XTestMongoRepository), lifetime));
|
|
|
|
//
|
|
// XAiProject ...
|
|
services.Add(new ServiceDescriptor(typeof(IXAiProjectRepository), typeof(XAiProjectMongoRepository), lifetime));
|
|
|
|
//
|
|
// XAiMessage ...
|
|
services.Add(new ServiceDescriptor(typeof(IXAiMessageRepository), typeof(XAiMessageMongoRepository), lifetime));
|
|
|
|
//
|
|
// XAiConversation ...
|
|
services.Add(new ServiceDescriptor(typeof(IXAiConversationRepository), typeof(XAiConversationMongoRepository), lifetime));
|
|
break;
|
|
|
|
//
|
|
default:
|
|
Console.WriteLine("Unsuported Data Provider Type ...");
|
|
XException.NotAllowed.Throw();
|
|
break;
|
|
}
|
|
#endregion
|
|
|
|
//
|
|
logger.LogInformation("End AddRepositories ...");
|
|
}
|
|
|
|
public void AddKeyGenerators(IServiceCollection services)
|
|
{
|
|
//
|
|
services.AddSingleton<IXKeyGenerator<XTest, int>, XIntKeyGenerator<XTest>>();
|
|
services.AddSingleton<IXKeyGenerator<XAiProject, Guid>, XGuidKeyGenerator<XAiProject>>();
|
|
services.AddSingleton<IXKeyGenerator<XAiMessage, Guid>, XGuidKeyGenerator<XAiMessage>>();
|
|
services.AddSingleton<IXKeyGenerator<XAiConversation, Guid>, XGuidKeyGenerator<XAiConversation>>();
|
|
}
|
|
|
|
public void AddDbSeederConfiguration(
|
|
IServiceCollection services,
|
|
IConfiguration configuration
|
|
)
|
|
{
|
|
//
|
|
logger.LogInformation("Start AddDbSeederConfiguration ...");
|
|
|
|
//
|
|
// Retrieve Config from appSettings ...
|
|
var dbSeederConfig = configuration.GetXDbSeederConfiguration();
|
|
if (!dbSeederConfig.IsNull())
|
|
{
|
|
services.AddXDbSeederConfiguration(dbSeederConfig);
|
|
}
|
|
|
|
//
|
|
logger.LogInformation("End AddDbSeederConfiguration ...");
|
|
}
|
|
|
|
public void AddDbSeeder<TDbSeeder>(
|
|
IServiceCollection services,
|
|
IConfiguration config,
|
|
ServiceLifetime lifetime
|
|
) where TDbSeeder : IXDbSeeder
|
|
{
|
|
//
|
|
logger.LogInformation("Start AddDbSeeder ...");
|
|
|
|
//
|
|
// Adding DbSeeder ...
|
|
services.Add(new ServiceDescriptor(typeof(IXDbSeeder), typeof(XAiApiDbSeeder), lifetime));
|
|
|
|
//
|
|
logger.LogInformation("End AddDbSeeder ...");
|
|
}
|
|
|
|
public ConventionPack MongoConventionPacks()
|
|
{
|
|
return null;
|
|
}
|
|
|
|
public void RegisterMongoExtras() { }
|
|
#endregion
|
|
|
|
//
|
|
#region Private ...
|
|
#endregion
|
|
}
|
|
} |