HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Micro ORM on the lines of dapper

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
microthedapperormlines

Problem

Designing a micro ORM inspired by Dapper. I'm building this as an exercise to learn. I would appreciate if you guys can review my code.

I would also appreciate any suggestions regarding the features that can/should be added to this ORM. The source is available here.


ConnectionStringProvider.cs

public interface IDbConnectionStringProvider {
      ConnectionStringEntity GetConnectionString(string connectionStringKey);
   }

   public class ConnectionStringProvider : IDbConnectionStringProvider {
      public ConnectionStringEntity GetConnectionString(string connectionStringKey) {
         Require.NotNullOrEmptyString(connectionStringKey, @"Connection string cannot be empty.");
         Require.NotNull(ConfigurationManager.ConnectionStrings, "Please provide a config file with a connection strings entry");

         ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings[connectionStringKey];

         if (settings == null)
            throw new ConfigurationErrorsException(string.Format("Cannot find the connection string: [{0}] in the config file.", connectionStringKey));

         if (string.IsNullOrWhiteSpace(settings.ConnectionString) || string.IsNullOrWhiteSpace(settings.ProviderName))
            throw new ConfigurationErrorsException(string.Format("Require connection string as well as provider name to instantiate connection for Key [{0}].", connectionStringKey));

         return new ConnectionStringEntity() {ConnectionString = settings.ConnectionString, ProviderName = settings.ProviderName};
      }
   }



ConnectionProvider.cs

```
public interface IConnectionProvider : IDisposable {
DbConnection CreateConnection(string connectionStringKey);
DbDataAdapter Adapter { get; }
IDbDataParameter CreateParameter();
}

public class ConnectionProvider : IConnectionProvider {

IDbConnectionStringProvider _connectionStringProvider;
DbProviderFactory _dbProviderFactory;
DbConnection _dbConnec

Solution

Some notes.

  • This piece of code propertyInfo.SetValue(obj, MassageValue(row[column.Name], propertyInfo.PropertyType)); will trow exception for nullable types. Did you test it with classes with int? or double? properties ?



  • Client code with your Tuples are very hard to read ... Look here. As for me, using Tuple for public methods is usually bad idea. I would like to have something like SqlParameter class there which encapsulates all parameter properties



  • I would recommend you to use Dispose according to official recommendations .. . Look here. Section "Basic Dispose pattern". It seems that your Dispose will work but at least it looks strange ...

Context

StackExchange Code Review Q#138980, answer score: 2

Revisions (0)

No revisions yet.