This commit is contained in:
2026-05-08 04:30:04 +03:30
parent afe5c74d09
commit 34afd95640
20 changed files with 1022 additions and 713 deletions
+79
View File
@@ -0,0 +1,79 @@
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using xCommons.Extensions;
using xDataService.Constants;
using xDataService.Db;
using xExceptions.Constants;
namespace xDataService.Helpers
{
public static class XDataServiceHelper
{
/// <summary>
/// Register DbContext ...
/// </summary>
/// <param name="source"></param>
/// <param name="provider"></param>
/// <param name="connectionString"></param>
/// <param name="lifetime"></param>
/// <param name="optionsBuilder"></param>
/// <typeparam name="TContext"></typeparam>
public static void AddXDbContext<TContext>(
IServiceCollection source,
XDbProviders provider,
string connectionString,
ServiceLifetime lifetime = ServiceLifetime.Scoped,
Action<dynamic> optionsBuilder = null
)
where TContext : XDbContext
{
//
if (provider == XDbProviders.None)
{
//
// Log("Invalid Db Provider ...");
XException.InvalidArgs.Throw();
}
//
switch (provider)
{
//
// MySQL ...
case XDbProviders.MySQL:
//
source.AddDbContext<TContext>(cfg =>
{
cfg.UseMySQL(connectionString, optionsBuilder);
}, lifetime);
break;
//
// SQLite ...
case XDbProviders.SQLite:
//
source.AddDbContext<TContext>(cfg =>
{
cfg.UseSqlite(connectionString, optionsBuilder);
}, lifetime);
break;
//
// SQLServer ...
case XDbProviders.SQLServer:
//
source.AddDbContext<TContext>(cfg =>
{
cfg.UseSqlServer(connectionString, optionsBuilder);
}, lifetime);
break;
//
case XDbProviders.MongoDB:
// There is notRequired to Register DBConext forMongo DB ...
break;
}
}
}
}