DapperLib / DapperLib/Dapper

Complex object Mapping returns null if First column value null

Open
#1,693 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
18.4k
Forks
3.7k
Avg merge
5h 8m
Merged PRs (30d)
1

Description

From the query

SELECT
B.BookId, b.BookName, b.Rate,
a.AuthorId,
a.AuthorName,
a.StreetName,
a.DoorNo,
a.AddressLine1,
a.AddressLine2,
a.District
FROM Book b INNER JOIN Author a
ON b.AuthorId = a.AuthorId
below is the dataset.
image

With dapper, populate Book, Author and Address complex objects.

Book:

public class Book
    {
        public long BookId { get; set; }
        public string Name { get; set; }
        public Author Author { get; set; }
        public decimal Rate { get; set; }
    }

Author

public class Author
    {
        public long AuthorId { get; set; }
        public string AuthorName { get; set; }
        public Address Address { get; set; }
    }

Address

public class Address
    {
        public string StreetName { get; set; }
        public string DoorNo { get; set; }
        public string AddressLine1 { get; set; }
        public string AddressLine2 { get; set; }
        public string District { get; set; }
    }

Below is the dapper implementation.

await _connection.QueryAsync(@"SELECT 
B.BookId, 
b.BookName, 
b.Rate, 
a.AuthorId,
a.AuthorName,
a.StreetName,
a.DoorNo,
a.AddressLine1,
a.AddressLine2,
a.District
FROM Book b INNER JOIN Author a 
ON b.AuthorId = a.AuthorId",
    new[] { typeof(Book), typeof(Author), typeof(Address) },
    (responseObj) =>
    {
        var book = responseObj[0] as Book;
        if (book != null)
        {
            var author = responseObj[1] as Author;
            if (author != null)
            {
                author.Address = responseObj[2] as Address;
            }
            book.Author = author;
        }
        return book;
    },
    splitOn: "BookId, AuthorId, StreetName");

After mapping below is the response.
image

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 by reproducing the provided SQL query with Dapper's multi-type QueryAsync mapping and splitOn values, including a null first-column value. Compare the Book, Author, and Address results with the reported response. Done means the intended nested objects are mapped correctly in this case and the behavior is covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, sql
Domain
backend-api-design, databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.