public static IUnitOfWork From(DbConnection dbConnection, DbTransaction dbTransaction) { UnitOfWork unitOfWork; if ((object)dbConnection == null) throw new ArgumentNullException(nameof(dbConnection)); unitOfWork = new UnitOfWork(dbConnection, dbTransaction); return unitOfWork; }
/// <summary> /// Creates a new unit of work (and opens the underlying connection) for the given connection type and connection string with an optional transaction started. /// </summary> /// <param name="connectionType"> The run-time type of the connection to use. </param> /// <param name="connectionString"> The ADO.NET provider connection string to use. </param> /// <param name="transactional"> A value indicating whether a new local data source transaction isstarted on the connection. </param> /// <param name="isolationLevel"> A value indicating the transaction isolation level. </param> /// <returns> An instance of teh unitOfWork ready for execution of operations. This should be wrapped in a using(...){} block for an optimal usage scenario. </returns> public static IUnitOfWork Create(Type connectionType, string connectionString, bool transactional, IsolationLevel isolationLevel = IsolationLevel.Unspecified) { UnitOfWork unitOfWork; DbConnection dbConnection; DbTransaction dbTransaction; const bool OPEN = true; if ((object)connectionType == null) throw new ArgumentNullException(nameof(connectionType)); if ((object)connectionString == null) throw new ArgumentNullException(nameof(connectionString)); dbConnection = (DbConnection)Activator.CreateInstance(connectionType); if (OPEN) { dbConnection.ConnectionString = connectionString; dbConnection.Open(); if (transactional) dbTransaction = dbConnection.BeginTransaction(isolationLevel); else dbTransaction = null; } unitOfWork = new UnitOfWork(dbConnection, dbTransaction); return unitOfWork; }