Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public interface IDbContext : IDisposable
- {
- IUnitOfWork CreateUnitOfWork();
- }
- public interface IDbContextFactory
- {
- IDbContext Create();
- }
- public interface IRepositoryFactory
- {
- T GetRepository<T>(IDbContext context, IUnitOfWork unitOfWork = null)
- where T : class;
- }
- public interface IUnitOfWork : IDisposable
- {
- bool IsInTransaction { get; }
- void BeginTransaction();
- void BeginTransaction(IsolationLevel isolation);
- void CommitTransaction();
- void RollbackTransaction();
- }
- public class MysqlDbContext : IDbContext
- {
- #region Fields
- private MySqlConnection _connection;
- private bool _disposed;
- #endregion
- #region Constructor
- public MysqlDbContext(MysqlConnectionFactory connectionFactory)
- {
- //Todo: create and open the connection
- _connection = connectionFactory.Create();
- if (_connection.State != ConnectionState.Open)
- _connection.Open();
- }
- #endregion
- public MySqlConnection Connection
- {
- get { return _connection; }
- }
- public IUnitOfWork CreateUnitOfWork()
- {
- return new MysqlUnitOfWork(_connection);
- }
- public void Dispose()
- {
- Dispose(true);
- }
- public void Dispose(bool disposing)
- {
- if (!disposing)
- return;
- /*
- * Dispose resources
- */
- if(_connection != null)
- {
- Console.WriteLine("Dispose connection!");
- _connection.Dispose();
- _connection = null;
- }
- //Set disposed
- _disposed = true;
- }
- }
- public class MysqlDbContextFactory : IDbContextFactory
- {
- private MysqlConnectionFactory _connectionFactory;
- public MysqlDbContextFactory(MysqlConnectionFactory connectionFactory)
- {
- _connectionFactory = connectionFactory;
- }
- public IDbContext Create()
- {
- return new MysqlDbContext(_connectionFactory);
- }
- }
- public class MysqlRepositoryFactory : IRepositoryFactory
- {
- #region Constructor
- public MysqlRepositoryFactory()
- {
- }
- #endregion
- public TRepository GetRepository<TRepository>(IDbContext context, IUnitOfWork unitOfWork = null)
- where TRepository : class
- {
- if (typeof(TRepository) == typeof(IUserRepository))
- return (new UserRepository(context, unitOfWork)) as TRepository;
- return default(TRepository);
- }
- }
- public class MysqlUnitOfWork : IUnitOfWork
- {
- private readonly MySqlConnection _connection;
- private MySqlTransaction _transaction;
- private bool _disposed;
- #region Constructor
- public MysqlUnitOfWork(MySqlConnection connection)
- {
- if (connection.State != ConnectionState.Open)
- throw new ApplicationException("Cannot begin transaction. Connection is not opened!");
- _connection = connection;
- }
- #endregion
- #region Properties
- public bool IsInTransaction
- {
- get
- {
- if (_transaction != null)
- return true;
- return false;
- }
- }
- public MySqlTransaction Transaction
- {
- get
- {
- return _transaction;
- }
- }
- #endregion
- public void BeginTransaction()
- {
- BeginTransaction(IsolationLevel.ReadCommitted);
- }
- public void BeginTransaction(IsolationLevel isolation)
- {
- if (_transaction != null)
- throw new ApplicationException("Cannot begin a new transaction while an existing transaction is still running. Please commit or rollback the existing transaction before starting a new one");
- _transaction = _connection.BeginTransaction(isolation);
- }
- public void CommitTransaction()
- {
- if (_transaction == null)
- throw new ApplicationException("Cannot commit transaction while there is no transaction running");
- _transaction.Commit();
- }
- public void RollbackTransaction()
- {
- if (_transaction == null)
- throw new ApplicationException("Cannot rollback transaction while there is no transaction running");
- _transaction.Rollback();
- }
- public void Dispose()
- {
- Dispose(true);
- }
- public void Dispose(bool disposing)
- {
- if (!disposing)
- return;
- if(_transaction != null)
- {
- _transaction.Dispose();
- _transaction = null;
- }
- _disposed = true;
- }
- }
- public class UserService : IUserService
- {
- #region Fields
- private readonly IDbContextFactory _dbContextFactory;
- private readonly IRepositoryFactory _repositoryFactory;
- #endregion
- #region Constructor
- public UserService(IDbContextFactory dbContextFactory, IRepositoryFactory repositoryFactory)
- {
- _dbContextFactory = dbContextFactory;
- _repositoryFactory = repositoryFactory;
- }
- #endregion
- public void Test()
- {
- using(var context = _dbContextFactory.Create())
- {
- var userRepository = _repositoryFactory.GetRepository<IUserRepository>(context);
- userRepository.Create(new User
- {
- Username = "admin",
- Password = "admin",
- Email = "admin@bidit.com",
- });
- }
- }
- public void TestTransaction()
- {
- using(var context = _dbContextFactory.Create())
- {
- using(var uow = context.CreateUnitOfWork())
- {
- var userRepository = _repositoryFactory.GetRepository<IUserRepository>(context, uow);
- //Begin new transaction (defualt isolation level: ReadCommited)
- uow.BeginTransaction();
- try
- {
- userRepository.Create(new User
- {
- Username = "rlydontknow",
- Password = "12345",
- Email = "rlydontknow@bidit.com",
- });
- //Commit transaction
- uow.CommitTransaction();
- }
- catch
- {
- //Rollback transaction on error
- uow.RollbackTransaction();
- throw;
- }
- }
- }
- }
- }
- public interface IDbContext : IDisposable
- {
- IUnitOfWork CreateUnitOfWork();
- }
- public interface IUnitOfWork : IDisposable
- {
- void Commit();
- void Rollback();
- }
- public interface IConnectionFactory
- {
- IDbConnection Create();
- }
- public class MysqlConnectionFactory : IConnectionFactory
- {
- private string _connectionString;
- public MysqlConnectionFactory(string connectionString)
- {
- _connectionString = connectionString;
- }
- public string ConnectionString
- {
- get
- {
- return _connectionString;
- }
- }
- public IDbConnection Create()
- {
- var connection = new MySqlConnection(_connectionString);
- connection.Open();
- return connection;
- }
- }
- public class MysqlDbContext : IDbContext
- {
- private IDbConnection _connection;
- private MysqlUnitOfWork _currentUnitOfWork;
- public MysqlDbContext(IConnectionFactory connectionFactory)
- {
- //Create new connection
- _connection = connectionFactory.Create();
- if (_connection.State != ConnectionState.Open)
- _connection.Open();
- }
- public IDbConnection Connection
- {
- get
- {
- return _connection;
- }
- }
- public MysqlUnitOfWork CurrentUnitOfWork
- {
- get
- {
- return _currentUnitOfWork;
- }
- }
- public IUnitOfWork CreateUnitOfWork()
- {
- if (_currentUnitOfWork != null)
- throw new InvalidOperationException("Cannot create new unit of work. Commit or rollback current one.");
- _currentUnitOfWork = new MysqlUnitOfWork(_connection);
- return _currentUnitOfWork;
- }
- public void Dispose()
- {
- Dispose(true);
- }
- public void Dispose(bool disposing)
- {
- if (!disposing)
- return;
- if(_connection != null)
- {
- _connection.Dispose();
- _connection = null;
- }
- }
- }
- public abstract class MysqlRepository
- {
- private readonly MysqlDbContext _context;
- public MysqlRepository(IDbContext context)
- {
- if (context.GetType() != typeof(MysqlDbContext))
- throw new InvalidOperationException("Incorrect context type. MysqlDbContext required!");
- _context = (MysqlDbContext)context;
- }
- public MysqlDbContext Context
- {
- get
- {
- return _context;
- }
- }
- protected IDbConnection Connection
- {
- get
- {
- if (_context != null)
- return _context.Connection;
- return null;
- }
- }
- protected IDbTransaction Transaction
- {
- get
- {
- if (_context != null && _context.CurrentUnitOfWork != null)
- return _context.CurrentUnitOfWork.Transaction;
- return null;
- }
- }
- }
- public class MysqlUnitOfWork : IUnitOfWork
- {
- private IDbTransaction _transaction;
- private bool _commited;
- public MysqlUnitOfWork(IDbConnection connection, IsolationLevel isolation = IsolationLevel.ReadCommitted)
- {
- if (connection == null)
- throw new ArgumentNullException("connection");
- if (connection.State != ConnectionState.Open)
- throw new InvalidOperationException("Cannot create unit of work. Connection to the database is not estabilished!");
- //Begin new transaction
- _transaction = connection.BeginTransaction(isolation);
- }
- public IDbTransaction Transaction
- {
- get
- {
- return _transaction;
- }
- }
- public void Commit()
- {
- if (_transaction == null)
- throw new InvalidOperationException("Cannot commit transaction while there is no transaction running");
- _transaction.Commit();
- _commited = true;
- }
- public void Rollback()
- {
- if (_transaction == null)
- throw new InvalidOperationException("Cannot rollback transaction while there is no transaction running");
- if (_commited)
- throw new InvalidOperationException("Cannot rollback already commited transaction");
- _transaction.Rollback();
- }
- public void Dispose()
- {
- Dispose(true);
- }
- public void Dispose(bool disposing)
- {
- if (!disposing)
- return;
- if(_transaction != null)
- {
- //Automatically rollback if not commited
- if (!_commited)
- _transaction.Rollback();
- _transaction.Dispose();
- _transaction = null;
- }
- }
- }
- public class UserRepository : MysqlRepository, IUserRepository
- {
- public UserRepository(IDbContext context)
- : base(context)
- {
- }
- public void Create(User user)
- {
- var parameters = new
- {
- username = user.Username,
- password = user.Password,
- email = user.Email,
- };
- user.Id = (uint)Connection.Query<ulong>("INSERT INTO users (username, password, email) VALUES (@username, @password, @email); select last_insert_id();", parameters).First();
- }
- }
- public class UserService : IUserService
- {
- private readonly IUserRepository _userRepository;
- public UserService(IUserRepository userRepository)
- {
- _userRepository = userRepository;
- }
- public void Create()
- {
- var user = new User
- {
- Username = "rlydontknow",
- Password = "12345",
- Email = "rlydontknow@sharpbid.com"
- };
- _userRepository.Create(user);
- Console.WriteLine("New user created! User id: " + user.Id);
- }
- }
- class Program
- {
- private static IConnectionFactory _connectionFactory;
- static void Main(string[] args)
- {
- _connectionFactory = new MysqlConnectionFactory("Server=localhost;Database=sharpbid;Uid=root;Pwd=maniek1;");
- SomeLogicalOperation();
- Console.ReadLine();
- }
- static void SomeLogicalOperation()
- {
- using(var context = GetContext())
- {
- var userRepository = new UserRepository(context);
- /*
- * Do some stuff without transaction
- */
- //Begin transaction
- using (var uow = context.CreateUnitOfWork())
- {
- var userService = new UserService(userRepository);
- userService.Create();
- //Commit transaction
- uow.Commit();
- }
- }
- }
- static IDbContext GetContext()
- {
- return new MysqlDbContext(_connectionFactory);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement