1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- using Microsoft.EntityFrameworkCore;
- using Microsoft.Extensions.Logging;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.Data.SqlClient;
- namespace EVCB_OCPP.Domain.ConnectionFactory;
- public class SqlConnectionFactory<T> : ISqlConnectionFactory<T> where T : DbContext
- {
- private readonly ILogger<SqlConnectionFactory<T>> logger;
- public string ConnectionString { get; init; }
- public SqlConnectionFactory(ILogger<SqlConnectionFactory<T>> logger)
- {
- this.logger = logger;
- }
- public SqlConnection Create()
- {
- var sqlConnection = new SqlConnection(ConnectionString);
- sqlConnection.Open();
- return sqlConnection;
- }
- public async Task<SqlConnection> CreateAsync()
- {
- var timer = Stopwatch.StartNew();
- long t0, t1;
- var sqlConnection = new SqlConnection(ConnectionString);
- t0 = timer.ElapsedMilliseconds;
- await sqlConnection.OpenAsync();
- t1 = timer.ElapsedMilliseconds;
- timer.Stop();
- if (t1 > 500)
- {
- logger.LogWarning("{type} SqlConnection Open slow {create}/{open}", typeof(T), t0, t1);
- }
- return sqlConnection;
- }
- }
|