Advertisement
Guest User

CodeReview test

a guest
Aug 5th, 2016
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.87 KB | None | 0 0
  1. First we implement everything explicitly using `private IDbConnection Connection;` to perform all the actions. It's a bit cumbersome, but it's OK, we just really need to do it once:
  2.    
  3.     public class UnityConnection : IDbConnection
  4.     {
  5.         private readonly UnityContainer _unityContainer;
  6.  
  7.         public UnityConnection() : this("abc") { }
  8.  
  9.         public UnityConnection(string connectionStringName)
  10.         {
  11.             var connectionString = ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
  12.             Connection = new MySqlConnection(connectionString);
  13.             Connection.Open();
  14.  
  15.             _unityContainer = new UnityContainer();
  16.             _unityContainer.RegisterInstance<IDbConnection>(Connection);
  17.         }
  18.  
  19.         private IDbConnection Connection;
  20.  
  21.         #region "IDbConnection implementation"        
  22.  
  23.         public string ConnectionString
  24.         {
  25.             get
  26.             {
  27.                 return Connection.ConnectionString;
  28.             }
  29.  
  30.             set
  31.             {
  32.                 Connection.ConnectionString = value;
  33.             }
  34.         }
  35.  
  36.         public int ConnectionTimeout
  37.         {
  38.             get
  39.             {
  40.                 return Connection.ConnectionTimeout;
  41.             }
  42.         }
  43.  
  44.         public string Database
  45.         {
  46.             get
  47.             {
  48.                 return Connection.Database;
  49.             }
  50.         }
  51.  
  52.         public ConnectionState State
  53.         {
  54.             get
  55.             {
  56.                 return Connection.State;
  57.             }
  58.         }
  59.  
  60.         public IDbTransaction BeginTransaction()
  61.         {
  62.             return Connection.BeginTransaction();
  63.         }
  64.  
  65.         public IDbTransaction BeginTransaction(IsolationLevel il)
  66.         {
  67.             return Connection.BeginTransaction(il);
  68.         }
  69.  
  70.         public void ChangeDatabase(string databaseName)
  71.         {
  72.             Connection.ChangeDatabase(databaseName);
  73.         }
  74.  
  75.         public void Close()
  76.         {
  77.             Connection.Close();
  78.         }
  79.  
  80.         public IDbCommand CreateCommand()
  81.         {
  82.             return Connection.CreateCommand();
  83.         }
  84.  
  85.         public void Dispose()
  86.         {
  87.             Connection.Dispose();
  88.         }
  89.  
  90.         public void Open()
  91.         {
  92.             Connection.Open();
  93.         }
  94.  
  95.         #endregion
  96.     }
  97.    
  98. Then the implementation of the repository would be:
  99.  
  100.     public class FooRepository<T> where T : IDisposable, IDbConnection, new()
  101.     {
  102.         private readonly IDbConnection _connection;
  103.  
  104.         public FooRepository() : this(new T()) { }
  105.  
  106.         public FooRepository(IConnection connection)
  107.         {
  108.             _connection = connection;
  109.         }
  110.  
  111.         public List<string> GetBar()
  112.         {
  113.             // ...
  114.         }
  115.  
  116.         public void Dispose()
  117.         {
  118.             _connection.Dispose();
  119.         }
  120.     }
  121.    
  122. ...and it can be called as:
  123.  
  124.     var repo1 = new FooRepository<UnityConnection>();
  125.     var repo2 = new FooRepository<UnityConnection>(new UnityConnection("xyz"));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement