public static IServiceCollection AddSqlSugar(this IServiceCollection services, ConnectionConfig[] configs, Action<ISqlSugarClient> buildAction = default) {
ServiceDescriptor serviceDescriptor = services.FirstOrDefault(s => typeof(ISqlSugarClient) == s.ServiceType);
SqlSugarClient sqlSugarClient = null;
if (null != serviceDescriptor) sqlSugarClient = serviceDescriptor.ImplementationFactory.Invoke(services.BuildServiceProvider()) as SqlSugarClient;
// 注冊 SqlSugar 客戶端
services.AddScoped<ISqlSugarClient>(u => {
if (null == sqlSugarClient) sqlSugarClient = new SqlSugarClient(configs.ToList());
else Array.ForEach(configs, config => sqlSugarClient.AddConnection(config));
buildAction?.Invoke(sqlSugarClient);
return sqlSugarClient;
});
// 注冊非泛型倉儲
services.AddScoped<ISqlSugarRepository, SqlSugarRepository>();
// 注冊 SqlSugar 倉儲
services.AddScoped(typeof(ISqlSugarRepository<>), typeof(SqlSugarRepository<>));
return services;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/266537.html
標籤:ASP.NET
