简体   繁体   中英

Is this an efficient way of mapping with Dapper?

Im trying to map the list of Products and CategoryPictures properties inside the Category entity.

Then im mapping the Picture object inside the CategoryPictures.

Im not sure if this an effiecient way of using dapper as its my first time working with dapper. I used to work with entityframework.

Im using Dapper instead of EF as i want to improve my sql skills.

Should i use multiple results instead?

  public async Task<IEnumerable<Category>> GetCategoriesListAsync()
    {
        using (var conn = SqlConnection())
        {
            string sql = @"select c.*, cp.*, pd.*, p.*
                        from Categories c 
                        inner join CategoryPictures cp on cp.CategoryId = c.Id 
                        inner join Products pd on pd.CategoryId = c.Id
                        inner join Pictures p on p.Id = cp.PictureId";



            //string sqlmulti = $@"select * from {tableName};
            //                   select * from Products";

            //List<Category> cat = null;
            //List<Product> prod = null;


            //using (var lists = conn.QueryMultiple(sqlmulti))
            //{
            //    cat = lists.Read<Category>().ToList();
            //    prod = lists.Read<Product>().ToList();
            //}

            var lookup = new List<Category>();

            await conn.QueryAsync<Category, CategoryPicture, Product, Picture, Category>(sql,
                                        (c, cp, pd, p) =>
                                        {
                                            if (lookup.FirstOrDefault(x => x.Id == c.Id) == null)
                                            {
                                                lookup.Add(c);
                                            }

                                            c.CategoryPictures.AsList().Add(cp);
                                            c.Products.AsList().Add(pd);
                                            foreach (var item in c.CategoryPictures)
                                            {
                                                item.Picture = p;
                                            }

                                            return null;
                                        });
            return lookup;

        }
    }

I've removed the link between Category and Product where the Product was child of Category.

The problem i was having with the previous code was that the Picture child of CategoryPictures was not populating properly. So i set the Picture object of CategoryPicture before populating CategoryPicture.

  public async Task<IEnumerable<Category>> GetAllCategories()
    {
        using (var conn = SqlConnection())
        {

            string sql = @"select c.Id, c.Name, c.Description, c.Created, c.LastModified,
                               cp.Id, cp.CategoryId, cp.PictureId, 
                               p.Id, p.Url
                        from Categories c 
                        inner join CategoryPictures cp on cp.CategoryId = c.Id
                        inner join Pictures p on p.Id = cp.PictureId";

            var lookup = new Dictionary<int, Category>();
            await conn.QueryAsync<Category, CategoryPicture, Picture, Category>(sql,
                                   (c, cp, p) =>
                                   {
                                       if (!lookup.TryGetValue(c.Id, out Category found))
                                       {
                                           lookup.Add(c.Id, found = c);
                                       }

                                       cp.Picture = p;

                                       found.CategoryPictures.AsList().Add(cp);


                                       return null;
                                   });

            return lookup.Values.ToList();

        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM