Beispiel #1
0
        public static IUnitOfWork From(IDbConnection dbConnection, IDbTransaction dbTransaction)
        {
            UnitOfWork unitOfWork;

            if ((object)dbConnection == null)
                throw new ArgumentNullException("dbConnection");

            unitOfWork = new UnitOfWork(dbConnection, dbTransaction);

            return unitOfWork;
        }
Beispiel #2
0
        /// <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;
            IDbConnection dbConnection;
            IDbTransaction dbTransaction;
            const bool OPEN = true;

            if ((object)connectionType == null)
                throw new ArgumentNullException("connectionType");

            if ((object)connectionString == null)
                throw new ArgumentNullException("connectionString");

            dbConnection = (IDbConnection)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;
        }