DapperLib / DapperLib/Dapper.Contrib

Composite Key Support

Open
#96 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
293
Forks
109
PR merge metrics
No merged PRs in 30d

Description

SqlMapperExtensions.cs

At the beginning of the method for GetAll, there is a GetSingleKey call within the method that seems a little unnecessary.

I have a situation, which is not uniques, where I don't have control over the data I am storing. This data needs a composite key to be unique.

I propose commenting out GetSingleKey(nameof(GetAll)) since the only thing being done in this method is a 'select * from tablename'

It might take more than just commenting out to create a fix.

The other option would be to allow for a single KeyAttribute and one or more ExplicitKeyAttributes in the GetSingleKey method.

`public static IEnumerable GetAll(this IDbConnection connection, IDbTransaction transaction = null, int? commandTimeout = null) where T : class
{
var type = typeof(T);
var cacheType = typeof(List);

        if (!GetQueries.TryGetValue(cacheType.TypeHandle, out string sql))
        {
            **GetSingleKey<T>(nameof(GetAll));**
            var name = GetTableName(type);

            sql = "select * from " + name;
            GetQueries[cacheType.TypeHandle] = sql;
        }

        if (!type.IsInterface()) return connection.Query<T>(sql, null, transaction, commandTimeout: commandTimeout);

        var result = connection.Query(sql);
        var list = new List<T>();
        foreach (IDictionary<string, object> res in result)
        {
            var obj = ProxyGenerator.GetInterfaceProxy<T>();
            foreach (var property in TypePropertiesCache(type))
            {
                var val = res[property.Name];
                if (val == null) continue;
                if (property.PropertyType.IsGenericType() && property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
                {
                    var genericType = Nullable.GetUnderlyingType(property.PropertyType);
                    if (genericType != null) property.SetValue(obj, Convert.ChangeType(val, genericType), null);
                }
                else
                {
                    property.SetValue(obj, Convert.ChangeType(val, property.PropertyType), null);
                }
            }
            ((IProxy)obj).IsDirty = false;   //reset change tracking and return
            list.Add(obj);
        }
        return list;
    }

`

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in SqlMapperExtensions.cs at GetAll and its GetSingleKey(nameof(GetAll)) call, then inspect how KeyAttribute and ExplicitKeyAttribute are handled. Determine the intended behavior for composite-key entities and verify that GetAll can retrieve them without inappropriate single-key validation; done means the chosen behavior is implemented and covered by the repository's existing tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
database
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.