2025-03-17 01:56:27 +08:00

60 lines
2.2 KiB
C#

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Seyounth.Hyosung.Data.Entities;
using Seyounth.Hyosung.Data.Repositories;
using Seyounth.Hyosung.Data.Services;
using Seyounth.Hyosung.Data.Services.Hyosung;
using SqlSugar;
namespace Seyounth.Hyosung.Data;
public static class ServiceExtensions
{
public static IServiceCollection AddHyosungData(this IServiceCollection services,
ConfigurationManager configuration)
{
var connectionString = configuration.GetConnectionString("DefaultConnection");
services.AddSingleton<ISqlSugarClient>(s =>
{
#if RELEASE
SqlSugarScope sqlSugar = new SqlSugarScope(new ConnectionConfig()
{
DbType = DbType.SqlServer,
ConnectionString = connectionString,
IsAutoCloseConnection = true,
}
);
#elif DEBUG
SqlSugarScope sqlSugar = new SqlSugarScope(new ConnectionConfig()
{
DbType = DbType.Sqlite,
ConnectionString = "Data Source=hyosung.db",
IsAutoCloseConnection = true,
InitKeyType = InitKeyType.Attribute
}
);
#endif
return sqlSugar;
});
services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
services.AddSingleton<IVarietyService, VarietyService>();
services.AddSingleton<IYarnService, YarnService>();
services.AddSingleton<ITrayService, TrayService>();
services.AddSingleton<IAgvBinService, AgvBinService>();
services.AddSingleton<IDictService, DictService>();
services.AddSingleton<IHyosungWmsService, HyosungWmsService>();
return services;
}
public static void UseHyosungData(this IServiceProvider provider)
{
var db = provider.GetRequiredService<ISqlSugarClient>();
db.DbMaintenance.CreateDatabase();
db.CodeFirst.InitTables(typeof(VarietyEntity),
typeof(PalletEntity),
typeof(ScannedYarnEntity),
typeof(TrayEntity),
typeof(AgvBinEntity),
typeof(DictEntity));
}
}