Skip to content

Commit

Permalink
Update test cases;
Browse files Browse the repository at this point in the history
  • Loading branch information
agile.zhou committed Jan 27, 2024
1 parent 8ec4382 commit 497fa7b
Show file tree
Hide file tree
Showing 26 changed files with 241 additions and 70 deletions.
2 changes: 2 additions & 0 deletions src/AgileConfig.Server.Apisite/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using AgileConfig.Server.Data.Repository.Selector;
using AgileConfig.Server.Data.Abstraction;

namespace AgileConfig.Server.Apisite
{
Expand Down Expand Up @@ -71,6 +72,7 @@ public void ConfigureServices(IServiceCollection services)
}

services.AddEnvAccessor();
services.AddDbConfigInfoFactory();
services.AddFreeSqlFactory();
// Add freesqlRepositories or other repositories
services.AddRepositories();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
using AgileConfig.Server.Common;
using Microsoft.Extensions.Configuration;
using System.Collections.Concurrent;

namespace AgileConfig.Server.Data.Abstraction.DbProvider
{
public static class DbConfigInfoFactory
public class DbConfigInfoFactory : IDbConfigInfoFactory
{
private static ConcurrentDictionary<string, IDbConfigInfo> _envProviders = new ConcurrentDictionary<string, IDbConfigInfo>();
private static IDbConfigInfo _default { get; }
private ConcurrentDictionary<string, IDbConfigInfo> _envProviders = new ConcurrentDictionary<string, IDbConfigInfo>();
private readonly IConfiguration _configuration;

static DbConfigInfoFactory()
private IDbConfigInfo _default { get; }

public DbConfigInfoFactory(IConfiguration configuration)
{
var providerPath = $"db:provider";
var connPath = $"db:conn";

var providerValue = Global.Config[providerPath];
var connValue = Global.Config[connPath];
var providerValue = configuration[providerPath];
var connValue = configuration[connPath];

if (string.IsNullOrEmpty(providerValue))
{
Expand All @@ -28,9 +31,10 @@ static DbConfigInfoFactory()
var configInfo = new DbConfigInfo("", providerValue, connValue);
_default = configInfo;
_envProviders.TryAdd(providerPath, configInfo);
this._configuration = configuration;
}

public static IDbConfigInfo GetConfigInfo(string env = "")
public IDbConfigInfo GetConfigInfo(string env = "")
{
if (string.IsNullOrEmpty(env))
{
Expand All @@ -49,8 +53,8 @@ public static IDbConfigInfo GetConfigInfo(string env = "")
return configInfo;
}

var providerValue = Global.Config[providerPath];
var connValue = Global.Config[connPath];
var providerValue = _configuration[providerPath];
var connValue = _configuration[connPath];

if (string.IsNullOrEmpty(providerValue))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace AgileConfig.Server.Data.Abstraction.DbProvider
{
public interface IDbConfigInfoFactory
{
IDbConfigInfo GetConfigInfo(string env = "");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using AgileConfig.Server.Data.Abstraction.DbProvider;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AgileConfig.Server.Data.Abstraction
{
public static class ServiceCollectionExtension
{
public static IServiceCollection AddDbConfigInfoFactory(this IServiceCollection sc)
{
sc.AddSingleton<IDbConfigInfoFactory, DbConfigInfoFactory>();

return sc;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FreeSql" Version="3.2.693" />
<PackageReference Include="FreeSql.DbContext" Version="3.2.693" />
<PackageReference Include="FreeSql.Provider.MySqlConnector" Version="3.2.693" />
<PackageReference Include="FreeSql.Provider.Oracle" Version="3.2.693" />
<PackageReference Include="FreeSql.Provider.PostgreSQL" Version="3.2.693" />
<PackageReference Include="FreeSql.Provider.Sqlite" Version="3.2.693" />
<PackageReference Include="FreeSql.Provider.SqlServer" Version="3.2.693" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="4.0.0" />
<PackageReference Include="Microsoft.Data.SqlClient.SNI.runtime" Version="4.0.0" />
<PackageReference Include="FreeSql" Version="3.2.810" />
<PackageReference Include="FreeSql.DbContext" Version="3.2.810" />
<PackageReference Include="FreeSql.Provider.MySqlConnector" Version="3.2.810" />
<PackageReference Include="FreeSql.Provider.Oracle" Version="3.2.810" />
<PackageReference Include="FreeSql.Provider.PostgreSQL" Version="3.2.810" />
<PackageReference Include="FreeSql.Provider.Sqlite" Version="3.2.810" />
<PackageReference Include="FreeSql.Provider.SqlServer" Version="3.2.810" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.1.4" />
<PackageReference Include="Microsoft.Data.SqlClient.SNI.runtime" Version="5.1.1" />
</ItemGroup>

<ItemGroup>
Expand Down
9 changes: 8 additions & 1 deletion src/AgileConfig.Server.Data.Freesql/EnvFreeSqlFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ namespace AgileConfig.Server.Data.Freesql
{
public class EnvFreeSqlFactory : IFreeSqlFactory
{
private readonly IMyFreeSQL _freeSQL;

public EnvFreeSqlFactory(IMyFreeSQL freeSQL)
{
this._freeSQL = freeSQL;
}

public IFreeSql Create(string env)
{
return FreeSQL.GetInstanceByEnv(env);
return _freeSQL.GetInstanceByEnv(env);
}
}
}
10 changes: 0 additions & 10 deletions src/AgileConfig.Server.Data.Freesql/FreeSqlDbContextFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,5 @@ namespace AgileConfig.Server.Data.Freesql
{
public class FreeSqlDbContextFactory
{
/// <summary>
/// 根据环境构建 dbcontext
/// </summary>
/// <param name="env"></param>
/// <returns></returns>
public static FreeSqlContext Create(string env)
{
Console.WriteLine("create env:{env} freesql dbcontext instance .");
return new FreeSqlContext(FreeSQL.GetInstanceByEnv(env));
}
}
}
7 changes: 7 additions & 0 deletions src/AgileConfig.Server.Data.Freesql/IMyFreeSQL.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace AgileConfig.Server.Data.Freesql
{
public interface IMyFreeSQL
{
IFreeSql GetInstanceByEnv(string env);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,25 @@

namespace AgileConfig.Server.Data.Freesql
{
public static class FreeSQL
public class MyFreeSQL : IMyFreeSQL
{
private static Dictionary<string, IFreeSql> _envFreesqls = new();
private Dictionary<string, IFreeSql> _envFreesqls = new();
private static object _lock = new object();
private readonly IDbConfigInfoFactory _dbConfigInfoFactory;

public MyFreeSQL(IDbConfigInfoFactory dbConfigInfoFactory)
{
this._dbConfigInfoFactory = dbConfigInfoFactory;
}

/// <summary>
/// 根据环境配置的字符串返回freesql 实例
/// </summary>
/// <param name="env"></param>
/// <returns></returns>
public static IFreeSql GetInstanceByEnv(string env)
public IFreeSql GetInstanceByEnv(string env)
{
var dbConfig = DbConfigInfoFactory.GetConfigInfo(env);
var dbConfig = _dbConfigInfoFactory.GetConfigInfo(env);

var dbType = ProviderToFreesqlDbType(dbConfig.Provider);
if (!dbType.HasValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public static class ServiceCollectionExt
public static void AddFreeSqlFactory(this IServiceCollection sc)
{
sc.AddScoped<IUow, FreeSqlUow>();
sc.AddSingleton<IMyFreeSQL, MyFreeSQL>();
sc.AddSingleton<IFreeSqlFactory, EnvFreeSqlFactory>();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public T GetServiceByEnv<T>(IServiceProvider sp, string env) where T : class

public bool IsSuit4Provider(string provider)
{
var freesqlType = FreeSQL.ProviderToFreesqlDbType(provider);
var freesqlType = MyFreeSQL.ProviderToFreesqlDbType(provider);

return freesqlType.HasValue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,30 @@ public void AddFixedRepositories(IServiceCollection sc)

public T GetServiceByEnv<T>(IServiceProvider sp, string env) where T : class
{
var dbConfigInfoFactory = sp.GetRequiredService<IDbConfigInfoFactory>();

if (typeof(T) == typeof(IUow))
{
return new MongodbUow() as T;
}
if (typeof(T) == typeof(IConfigPublishedRepository))
{
var envDbConfig = DbConfigInfoFactory.GetConfigInfo(env);
var envDbConfig = dbConfigInfoFactory.GetConfigInfo(env);
return new ConfigPublishedRepository(envDbConfig.ConnectionString) as T;
}
if (typeof(T) == typeof(IConfigRepository))
{
var envDbConfig = DbConfigInfoFactory.GetConfigInfo(env);
var envDbConfig = dbConfigInfoFactory.GetConfigInfo(env);
return new ConfigRepository(envDbConfig.ConnectionString) as T;
}
if (typeof(T) == typeof(IPublishDetailRepository))
{
var envDbConfig = DbConfigInfoFactory.GetConfigInfo(env);
var envDbConfig = dbConfigInfoFactory.GetConfigInfo(env);
return new PublishDetailRepository(envDbConfig.ConnectionString) as T;
}
if (typeof(T) == typeof(IPublishTimelineRepository))
{
var envDbConfig = DbConfigInfoFactory.GetConfigInfo(env);
var envDbConfig = dbConfigInfoFactory.GetConfigInfo(env);
return new PublishTimelineRepository(envDbConfig.ConnectionString) as T;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ namespace AgileConfig.Server.Data.Repository.Mongodb;

public class SysInitRepository : ISysInitRepository
{
private readonly MongodbAccess<Setting> _access = new(Global.Config["db:conn"]);
public SysInitRepository(IConfiguration configuration)
{
this._configuration = configuration;
}

private MongodbAccess<Setting> _access => new MongodbAccess<Setting>(_configuration["db:conn"]);
private readonly IConfiguration _configuration;

public Task<string?> GetDefaultEnvironmentAsync()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ public static IServiceCollection AddRepositories(this IServiceCollection sc)
{
sc.AddFreeRepository();

var defaultProvider = DbConfigInfoFactory.GetConfigInfo();
var serviceProvider = sc.BuildServiceProvider();
var dbConfigInfoFactory = serviceProvider.GetRequiredService<IDbConfigInfoFactory>();

var defaultProvider = dbConfigInfoFactory.GetConfigInfo();

if (string.IsNullOrEmpty(defaultProvider.Provider))
{
Expand All @@ -30,35 +33,35 @@ public static IServiceCollection AddRepositories(this IServiceCollection sc)
#region these repositories genereated dependency env provider, if no env provider use default provider
sc.AddScoped<Func<string, IUow>>(sp => env =>
{
var envDbConfig = DbConfigInfoFactory.GetConfigInfo(env);
var envDbConfig = dbConfigInfoFactory.GetConfigInfo(env);

return GetRepositoryServiceRegister(envDbConfig.Provider).GetServiceByEnv<IUow>(sp, env);
});

sc.AddScoped<Func<string, IConfigPublishedRepository>>(sp => env =>
{
var envDbConfig = DbConfigInfoFactory.GetConfigInfo(env);
var envDbConfig = dbConfigInfoFactory.GetConfigInfo(env);

return GetRepositoryServiceRegister(envDbConfig.Provider).GetServiceByEnv<IConfigPublishedRepository>(sp, env);
});

sc.AddScoped<Func<string, IConfigRepository>>(sp => env =>
{
var envDbConfig = DbConfigInfoFactory.GetConfigInfo(env);
var envDbConfig = dbConfigInfoFactory.GetConfigInfo(env);

return GetRepositoryServiceRegister(envDbConfig.Provider).GetServiceByEnv<IConfigRepository>(sp, env);
});

sc.AddScoped<Func<string, IPublishDetailRepository>>(sp => env =>
{
var envDbConfig = DbConfigInfoFactory.GetConfigInfo(env);
var envDbConfig = dbConfigInfoFactory.GetConfigInfo(env);

return GetRepositoryServiceRegister(envDbConfig.Provider).GetServiceByEnv<IPublishDetailRepository>(sp, env);
});

sc.AddScoped<Func<string, IPublishTimelineRepository>>(sp => env =>
{
var envDbConfig = DbConfigInfoFactory.GetConfigInfo(env);
var envDbConfig = dbConfigInfoFactory.GetConfigInfo(env);

return GetRepositoryServiceRegister(envDbConfig.Provider).GetServiceByEnv<IPublishTimelineRepository>(sp, env);
});
Expand Down
5 changes: 3 additions & 2 deletions src/AgileConfig.Server.Service/SystemInitializationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@
using AgileConfig.Server.Data.Abstraction;
using AgileConfig.Server.Data.Entity;
using AgileConfig.Server.IService;
using Microsoft.Extensions.Configuration;

namespace AgileConfig.Server.Service;

public class SystemInitializationService(ISysInitRepository sysInitRepository):ISystemInitializationService
public class SystemInitializationService(ISysInitRepository sysInitRepository, IConfiguration configuration):ISystemInitializationService
{
/// <summary>
/// 如果 配置文件或者环境变量没配置 JwtSetting:SecurityKey 则生成一个存库
/// </summary>
/// <returns></returns>
public bool TryInitJwtSecret()
{
var jwtSecretFromConfig = Global.Config["JwtSetting:SecurityKey"];
var jwtSecretFromConfig = configuration["JwtSetting:SecurityKey"];
if (string.IsNullOrEmpty(jwtSecretFromConfig))
{
var jwtSecretSetting = sysInitRepository.GetJwtTokenSecret();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ public void TestGetConfigInfo()
var configuration = configurationBuilder.Build();
Global.Config = configuration;

var configInfo = DbConfigInfoFactory.GetConfigInfo();
var configInfo = new DbConfigInfoFactory(configuration).GetConfigInfo();
Assert.IsNotNull(configInfo);
Assert.AreEqual("sqlserver", configInfo.Provider);
Assert.AreEqual("localhost", configInfo.ConnectionString);

configInfo = DbConfigInfoFactory.GetConfigInfo("test");
configInfo = new DbConfigInfoFactory(configuration).GetConfigInfo("test");
Assert.IsNotNull(configInfo);
Assert.AreEqual("sqlite", configInfo.Provider);
Assert.AreEqual("Data Source=agile_config.db", configInfo.ConnectionString);

configInfo = DbConfigInfoFactory.GetConfigInfo("x");
configInfo = new DbConfigInfoFactory(configuration).GetConfigInfo("x");
Assert.IsNotNull(configInfo);
Assert.AreEqual("sqlserver", configInfo.Provider);
Assert.AreEqual("localhost", configInfo.ConnectionString);
Expand Down
11 changes: 7 additions & 4 deletions test/AgileConfig.Server.Data.FreesqlTests/FreeSQLTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using AgileConfig.Server.Common;
using AgileConfig.Server.Data.Abstraction.DbProvider;

namespace AgileConfig.Server.Data.Freesql.Tests
{
Expand All @@ -27,20 +28,22 @@ public void GetInstanceByEnvTest()
var configuration = configurationBuilder.Build();
Global.Config = configuration;

var fsq = FreeSQL.GetInstanceByEnv("");
var myfreesql = new MyFreeSQL(new DbConfigInfoFactory(configuration));

var fsq = myfreesql.GetInstanceByEnv("");
Assert.IsNotNull(fsq);
Assert.AreEqual(FreeSql.DataType.Sqlite, fsq.Ado.DataType);

var fsqtest = FreeSQL.GetInstanceByEnv("test");
var fsqtest = myfreesql.GetInstanceByEnv("test");
Assert.IsNotNull(fsqtest);
Assert.AreEqual(FreeSql.DataType.Sqlite, fsqtest.Ado.DataType);

Assert.AreNotSame(fsq, fsqtest);
var fsqtest_ag = FreeSQL.GetInstanceByEnv("test");
var fsqtest_ag = myfreesql.GetInstanceByEnv("test");
Assert.AreSame(fsqtest, fsqtest_ag);


var fsq_none = FreeSQL.GetInstanceByEnv("x");
var fsq_none = myfreesql.GetInstanceByEnv("x");
Assert.IsNotNull(fsq_none);
Assert.AreEqual(FreeSql.DataType.Sqlite, fsq_none.Ado.DataType);
Assert.AreSame(fsq, fsq_none);
Expand Down
Loading

0 comments on commit 497fa7b

Please sign in to comment.