Last active
June 16, 2016 17:34
-
-
Save juniorkrvl/048afeff71e2377e8fe9073895c364bc to your computer and use it in GitHub Desktop.
Generic unif of work class based on DbConnection class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using BTFIT.AutoTraining.Repository.Dapper.Base; | |
using StackExchange.Profiling; | |
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Configuration; | |
using System.Data; | |
using System.Data.Common; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace BTFIT.AutoTraining.Repository.Dapper | |
{ | |
public class UnitOfWork : IDisposable | |
{ | |
private IDbConnection _conn; | |
private IDbTransaction _transaction = null; | |
public UnitOfWork(Boolean transaction = true) | |
{ | |
DbProviderFactories.GetFactoryClasses(); | |
_conn = GetConnection(); | |
if (_conn.State == ConnectionState.Closed) | |
{ | |
_conn.Open(); | |
} | |
if (transaction) | |
{ | |
_transaction = _conn.BeginTransaction(IsolationLevel.ReadCommitted); | |
} | |
Repositories = new Hashtable(); | |
} | |
protected DbConnection GetConnection() | |
{ | |
try | |
{ | |
DbProviderFactory factory = DbProviderFactories.GetFactory(ConfigurationManager.ConnectionStrings["BTLiveLib"].ProviderName); | |
DbConnection conn = factory.CreateConnection(); | |
conn.ConnectionString = ConfigurationManager.ConnectionStrings["BTLiveLib"].ConnectionString; | |
// to get profiling times, we have to wrap whatever connection we're using in a ProfiledDbConnection | |
// when MiniProfiler.Current is null, this connection will not record any database timings | |
//conn = new StackExchange.Profiling.Data.ProfiledDbConnection(conn,MiniProfiler.Start()); | |
// wrap the connection with a profiling connection that tracks timings | |
return conn; | |
} | |
catch (DbException myerro) | |
{ | |
throw myerro; | |
} | |
} | |
private Dictionary<Type, object> repositories = new Dictionary<Type, object>(); | |
protected Hashtable Repositories; | |
public IRepository<T> GenericRepository<T>() where T : class | |
{ | |
if (repositories.Keys.Contains(typeof(T)) == true) | |
{ | |
return repositories[typeof(T)] as IRepository<T>; | |
} | |
IRepository<T> repo = new Repository<T>(_conn, _transaction); | |
repositories.Add(typeof(T), repo); | |
return repo; | |
} | |
public T Repository<T>() | |
{ | |
if (Repositories[typeof(T)] == null) | |
{ | |
dynamic reposit = Activator.CreateInstance(typeof(T), _conn, _transaction); | |
Repositories[typeof(T)] = (reposit); | |
} | |
return (T)Repositories[typeof(T)]; | |
} | |
public void Save() | |
{ | |
_transaction.Commit(); | |
_conn.Close(); | |
} | |
public void Undo() | |
{ | |
_transaction.Rollback(); | |
_conn.Close(); | |
} | |
private bool disposed = false; | |
protected virtual void Dispose(bool disposing) | |
{ | |
if (!this.disposed) | |
{ | |
if (disposing) | |
{ | |
if (_transaction != null) | |
{ | |
if (_conn.State != ConnectionState.Closed) | |
{ | |
_transaction.Rollback(); | |
} | |
_transaction.Dispose(); | |
} | |
_conn.Close(); | |
_conn.Dispose(); | |
} | |
} | |
this.disposed = true; | |
} | |
public void Dispose() | |
{ | |
Dispose(true); | |
GC.SuppressFinalize(this); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment