I'm implementing my first Dapper.Net project. Now i'm wondering what's the easiest way to initialize an object that contains another object(multi mapping).
Here's my code:
public static IEnumerable<ShopPrefix> GetShopPrefixes(short fiSL)
{
using (var con = new SqlConnection(Properties.Settings.Default.RM2Con))
{
const String sql = @"
SELECT locShopPrefix.idShopPrefix,
locShopPrefix.fiSL,
locShopPrefix.fiShop,
locShopPrefix.Prefix,
locShopPrefix.Active,
locShop.idShop,
locShop.ShopName,
locShop.ContactPerson,
locShop.Street,
locShop.ZIP,
locShop.City,
locShop.Telephone,
locShop.Telefax,
locShop.Email,
locShop.ShopKey
FROM locShopPrefix
INNER JOIN locShop
ON locShopPrefix.fiShop = locShop.idShop
WHERE (locShopPrefix.fiSL = @fiSL);";
con.Open();
IEnumerable<Tuple<ShopPrefix,Shop>> shops =
con.Query<ShopPrefix, Shop, Tuple<ShopPrefix, Shop>>(
sql
, (shopPrefix, shop) => Tuple.Create(shopPrefix, shop)
, new { fiSL = fiSL }, splitOn: "idShop"
);
foreach (var shop in shops)
shop.Item1.Shop = shop.Item2;
return shops.Select(t => t.Item1);
}
}
So every shopPrefix belongs to (has) a Shop.
Q: Is this the correct way to map two objects since the Tuple approach with the following foreach to initialize the property Shop looks cumbersome?