199 lines
5.8 KiB
C#
199 lines
5.8 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.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));
|
|
#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));
|
|
break;
|
|
|
|
//
|
|
case XDbProviders.MongoDB:
|
|
//
|
|
// XTest ...
|
|
services.Add(new ServiceDescriptor(typeof(IXTestRepository), typeof(XTestMongoRepository), 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>>();
|
|
}
|
|
|
|
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
|
|
}
|
|
} |