5

単一のクエリ(または複数の結果セットを持つストアドプロシージャ)を実行したい。Dapperを使用してマルチマッピングを行う方法は知っていますが、2つのコレクションを同じ親にマッピングする方法を並べ替えることができません。基本的に、このオブジェクト定義を考えると...

class ParentObject
{
    string Name { get; set; }
    ICollection<ChildObjectOne> ChildSetOne {get;set;}
    ICollection<ChildObjectTwo> ChildSetTwo { get; set; }
}

class ChildObjectOne
{
    string Name { get; set; }
}

class ChildObjectTwo
{
    int id { get; set; }
    string LocationName { get; set; }
}

どういうわけか次のようなDapperクエリを実行できるようにしたいと思います。

IQueryable<ParentObject> result = cnn.Query(
          // Some really awesome dapper syntax goes here
);
4

2 に答える 2

7

MultiMapping を使用したくないかどうかはわかりませんが、あなたの場合にどのように機能するかは次のとおりです。私が知っている限り、SOで読んだ限り、深くネストされたオブジェクトグラフを単純なQuery.

 static void Main(string[] args)
        {
            var sqlParent = "SELECT parentId as Id FROM ParentTable WHERE parentId=1;";
            var sqlChildOneSet = "SELECT Name FROM ChildOneTable;"; // Add an appropriate WHERE
            var sqlChildTwoSet = "SELECT Id, LocationName FROM ChildTwoTable;"; // Add an appropriate WHERE

            var conn = GetConnection() // whatever you're getting connections with
            using (conn)
            {
                conn.Open();
                using (var multi = conn.QueryMultiple(sqlParent + sqlChildOneSet + sqlChildTwoSet))
                {
                    var parent = multi.Read<ParentObject>().First();
                    parent.ChildSetOne = multi.Read<ChildOne>().ToList();
                    parent.ChildSetTwo = multi.Read<ChildTwo>().ToList();
                }
            }
        }

ネストされたオブジェクトと dapper に関する同様の質問:

https://stackoverflow.com/search?q=nested+objects+%2B+dapper

于 2012-05-09T13:45:22.473 に答える
3

IEnumerable<TReturn> Query<TFirst, TSecond, TThird, TReturn>(this IDbConnection cnn, string sql, Func<TFirst, TSecond, TThird, TReturn> map);この場合のメソッドを使用して、1 対多の関係を持つオブジェクトを実体化することができます。ただし、十分な情報を得るために、エンティティにいくつかの変更を加える必要があります。

同様の質問があるいくつかの SO スレッドを次に示します。

ネストされたオブジェクトのリストを Dapper でマップするにはどうすればよいですか

よりきれいにする拡張機能

例による Dapper.Net - 関係のマッピング

public class ParentObject
{
    public ParentObject()
    {
        ChildSetOne = new List<ChildObjectOne>();
        ChildSetTwo = new List<ChildObjectTwo>();
    }
    // 1) Although its possible to do this without this Id property, For sanity it is advisable.
    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<ChildObjectOne> ChildSetOne {get; private set;}
    public ICollection<ChildObjectTwo> ChildSetTwo { get; private set; }
}

public class ChildObjectOne
{
    // 2a) Need a ParentId
    public int ParentId { get; set; }
    public string Name { get; set; }
}

public class ChildObjectTwo
{
    // 2b) This ParentId is not required but again for sanity it is advisable to include it.
    public int ParentId { get; set; }
    public int id { get; set; }
    public string LocationName { get; set; }
}

public class Repository
{
    public IEnumerable<ParentObject> Get()
    {
        string sql = 
            @"SELECT 
                p.Id, 
                p.Name, 
                o.Name, 
                o.ParentId, 
                t.Id, 
                t.LocationName, 
                t.ParentId 
            FROM 
                Parent p 
                    LEFT JOIN ChildOne o on o.ParentId = p.Id 
                    LEFT JOIN ChildTwo t on t.ParentId = p.Id 
            WHERE 
                p.Name LIKE '%Something%'";

        var lookup = new Dictionary<int, ParentObject>();
        using (var connection = CreateConnection())
        {

            connection.Query<ParentObject, ChildObjectOne, ChildObjectTwo, ParentObject>(
                sql, (parent, childOne, childTwo) =>
                {
                    ParentObject activeParent;

                    if (!lookup.TryGetValue(childOne.ParentId, out activeParent)) 
                    {
                        activeParent = parent;
                        lookup.add(activeParent.Id, activeParent);
                    }

                    //TODO: if you need to check for duplicates or null do so here
                    activeParent.ChildSetOne.Add(childOne);

                    //TODO: if you need to check for duplicates or null do so here
                    activeParent.ChildSetTwo.Add(childTwo);

                });   
        }
        return lookup.Values;
    }
}
于 2016-03-18T23:15:45.390 に答える