- VAL.src.zip
- VAL
- Database
- packages
- EntityFramework.4.2.0.0
- lib
- net40
- EntityFramework.dll
- log4net.1.2.10
- lib
- 1.0
- log4net.dll
- 1.1
- log4net.dll
- 2.0
- log4net.dll
- log4net.1.2.11
- lib
- net10-full
- log4net.dll
- net11-full
- log4net.dll
- net20-cf
- log4net.dll
- net20-full
- log4net.dll
- net35-client
- log4net.dll
- net35-full
- log4net.dll
- net40-client
- log4net.dll
- net40-full
- log4net.dll
- Moq.4.0.10827
- lib
- NET35
- Moq.dll
- NET40
- Moq.dll
- Silverlight4
- Castle.Core.dll
- Moq.Silverlight.dll
- License.txt
- Moq.chm
- repositories.config
- structuremap.2.6.3
- lib
- StructureMap.dll
- READ-ME-First!.txt
- VAL.BusinessService
- VAL.ClientService
- VAL.Common
- VAL.Contracts
- VAL.Controls
- VAL.Data
- VAL.GUI
- VAL.Model
- VAL.Service
- VAL.sln
- VAL.Tests
- VAL.User.Documentation
- VAL.zip
- VisualApplicationLauncher
- Database
- Local.testsettings
- NRepo
- ProjectReferences
- EFCachingProvider.dll
- EFCachingProvider.Web.dll
- EFProviderWrapperToolkit.dll
- EFTracingProvider.dll
- log4net.dll
- StructureMap.dll
- READ-ME-First!.txt
- TraceAndTestImpact.testsettings
- VAL.BusinessService
- VAL.Common
- VAL.Contracts
- VAL.Controls
- VAL.Data
- VAL.GUI
- VAL.Model
- VAL.Service
- VAL.sln
- VAL.Tests
- VAL.User.Documentation
- VAL.vsmdi
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Objects;
namespace NRepo.Repositories.EntityFramework
{
/// <summary>
/// Implements the <see cref="IUnitOfWork"/> interface to provide an implementation
/// of a IUnitOfWork that uses Entity Framework to query and update the underlying store.
/// </summary>
public class EntityUnitOfWork : IUnitOfWork
{
#region fields
private bool _disposed;
private IEntitySession _session;
private EntityTransaction _transaction;
#endregion
#region ctor
/// <summary>
/// Default Constructor.
/// Creates a new instance of the <see cref="EFUnitOfWork"/> class that uses the specified object context.
/// </summary>
/// <param name="session">The <see cref="IEFSession"/> instance that the EFUnitOfWork instance uses.</param>
public EntityUnitOfWork(IEntitySession session)
{
Guard.Against<ArgumentNullException>(session == null, "Expected a non-null ObjectContext instance.");
_session = session;
}
#endregion
#region properties
/// <summary>
/// Gets the <see cref="ObjectContext"/> bound with the EFUnitOfWork instance.
/// </summary>
public ObjectContext Context
{
get { return _session.Context; }
}
#endregion
#region Implementation of IDisposable
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
/// <filterpriority>2</filterpriority>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Disposes off the managed and unmanaged resources used.
/// </summary>
/// <param name="disposing"></param>
private void Dispose(bool disposing)
{
if (!disposing) return;
if (_disposed) return;
if (_transaction != null)
{
_transaction.Dispose();
_transaction = null;
}
if (_session != null)
{
_session.Dispose();
_session = null;
}
_disposed = true;
}
#endregion
#region Implementation of IUnitOfWork
/// <summary>
/// Gets a boolean value indicating whether the current unit of work is running under
/// a transaction.
/// </summary>
public bool IsInTransaction
{
get { return _transaction != null; }
}
/// <summary>
/// Instructs the <see cref="IUnitOfWork"/> instance to begin a new transaction.
/// </summary>
/// <returns></returns>
public ITransaction BeginTransaction()
{
return BeginTransaction(IsolationLevel.ReadCommitted);
}
public void Rollback()
{
Guard.Against<InvalidOperationException>(_transaction == null,
"Cannot rollback a transaction, no transaction exists");
_transaction.Rollback();
}
/// <summary>
/// Instructs the <see cref="IUnitOfWork"/> instance to begin a new transaction
/// with the specified isolation level.
/// </summary>
/// <param name="isolationLevel">One of the values of <see cref="IsolationLevel"/>
/// that specifies the isolation level of the transaction.</param>
/// <returns></returns>
public ITransaction BeginTransaction(IsolationLevel isolationLevel)
{
Guard.Against<InvalidOperationException>(_transaction != null,
"Cannot begin a new transaction while an existing transaction is still running. " +
"Please commit or rollback the existing transaction before starting a new one.");
if (_session.Connection.State != ConnectionState.Open)
_session.Connection.Open();
IDbTransaction transaction = _session.Connection.BeginTransaction(isolationLevel);
_transaction = new EntityTransaction(transaction);
_transaction.TransactonComitted += TransactionCommitted;
_transaction.TransactionRolledback += TransactionRolledback;
return _transaction;
}
/// <summary>
/// Flushes the changes made in the unit of work to the data store.
/// </summary>
public void Flush()
{
_session.SaveChanges();
}
/// <summary>
/// Flushes the changes made in the unit of work to the data store
/// within a transaction.
/// </summary>
public void TransactionalFlush()
{
TransactionalFlush(IsolationLevel.ReadCommitted);
}
/// <summary>
/// Flushes the changes made in the unit of work to the data store
/// within a transaction with the specified isolation level.
/// </summary>
/// <param name="isolationLevel"></param>
public void TransactionalFlush(IsolationLevel isolationLevel)
{
// Start a transaction if one isn't already running.
if (!IsInTransaction)
BeginTransaction(isolationLevel);
try
{
_session.SaveChanges();
_transaction.Commit();
}
catch
{
_transaction.Rollback();
throw;
}
}
#endregion
#region methods
/// <summary>
/// Handles the <see cref="ITransaction.TransactionRolledback"/> event.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TransactionRolledback(object sender, EventArgs e)
{
Guard.IsEqual<InvalidOperationException>(sender, _transaction,
"Expected the sender of TransactionRolledback event to be the transaction that was created by the EFUnitOfWork instance.");
ReleaseCurrentTransaction();
}
/// <summary>
/// Handles the <see cref="ITransaction.TransactonComitted"/> event.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TransactionCommitted(object sender, EventArgs e)
{
Guard.IsEqual<InvalidOperationException>(sender, _transaction,
"Expected the sender of TransactionComitted event to be the transaction that was created by the EFUnitOfWork instance.");
ReleaseCurrentTransaction();
}
/// <summary>
/// Releases the current transaction in the <see cref="EFTransaction"/> instance.
/// </summary>
private void ReleaseCurrentTransaction()
{
if (_transaction != null)
{
_transaction.TransactonComitted -= TransactionCommitted;
_transaction.TransactionRolledback -= TransactionRolledback;
_transaction.Dispose();
}
_transaction = null;
//Closing the connection once the transaction has completed.
if (_session.Connection.State == ConnectionState.Open)
_session.Connection.Close();
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.