Question: I'm trying to write a wrapper to hide much of the code I find myself repeating for each db call in Dapper. (namely the Sql Connection, try, default catch and finally) Essentially I would like to do something like the following code but I understand that because of there being a dynamic parameter I can't use generics int this way. 

The way it is I get the error:

> 
> Consider casting the dynamic arguments or calling the extension method without the extension method syntax (refering to the conn.Query method)
> 
> 
> 

Is there a way to refactor my `ExecuteQuery` or to something like it that works?

```
public abtract class IDbAccessService
{
   public LogService Logger { get; set; }

   public virtual IEnumerable<T> ExecuteQuery<T>(string sql, dynamic param, string connString)
      where T : BaseModel
   {
      using (var conn = DataAccessHelpers.GetOpenConnection(connString))
      {
         try
         {
            return conn.Query<T>(sql, param).ToList<T>();
         }
         catch (Exception ex)
         {
            Logger.Logger.Error(ex.Message, ex);
            throw ex;
         }
      }
   }
}

```

Answer: 
Anonymous types have an autogenerated sensible `GetHashCode()` implementation. I'd try just using: 

```
struct MyStruct 
{
    int _intField1;
    int _intField2;
    int _intField3;
    string _stringField;

    public long GetHashCode() 
    {
        return new { _intField1, _intField2, _intField3, _stringField }.GetHashCode();
    }
}

```

Since both `int`s and `string`s are immutable types, the hash code should remain the same between runs of the application as long the underlying .NET framework version is the same. (This might or might not be "persistent enough".)

That said, it *might* change should the internal implementation of `GetHashCode()` change. In that case, use a cryptographic hash. It doesn't matter that it exceeds 32 bits, because cryptographic hashes are designed to produce wildly different output for small changes in input. This means that for two different inputs, **any** given 32 bits of the hash code are very unlikely to be equal. Just use [`BitConverter.ToInt32()`](http://msdn.microsoft.com/en-us/library/system.bitconverter.toint32.aspx) to convert whichever part of the hash you want to an `int`.

Also, obviously, this will just make it somewhat unlikely that two different structures will produce different hash codes. (This can be determined using an approximate formula for the birthday paradox, if I'm reading wiki correctly, it means you'd have a 10% chance of getting duplicates once you store ~140,000 ~30,000 records. Assuming the cryptographic hash has ideal properties. I'm not sure you can do better without a perfect hash.)
1. Serialize your type to a byte[]
2. Apply common hash algorithm to byte[] to obtain hash byte[]
3. Pull out, for example, the first 32 bits of the hash byte[] and use that