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: 
If you had the following:

```
struct 
{
    int A;
    int B;
    int C;
}

```

Assuming A, B, C are in the range `[1, 1000]`. It is possible to create a "perfect hash" (without collision) since A, B, C can have each 1000 different possible values. Indeed, `log2(1000^3) <= 32` (`1000^3` is the number of possible values of the sturcture and the `log2` is used to get the number of bits required to store all these values without collision and `32` is the number of bits of an integer).

```
int MyHashCode()
{
    return 1000 * (1000 * (A - 1) + (B - 1)) + (C - 1);  // There is no overflow or collision since A, B, C are in the range [1, 1000]
}

```

We can simplify it by using a weaker condition: A, B, C are in the range [0, 1000]:

```
int MyHashCode()
{
    return 1001 * (1001 * A + B) + C;  // There is no overflow or collision since A, B, C are in the range [0, 1000]
}

```

Update
------

Given that your structure contains a string inside it. What you want to achieve is impossible. Because the string can represent an infinite number of values.

If it was possible to do it one could create a very powerful compression algorithm. That could store any file into a... 32 bit number! Mathematically, it comes from the fact that an injective function can only map to a bigger space.
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