advapi
advapi

Reputation: 3907

Mapping with Dapper cascading objects

I'm refactoring an old query made with EF that's taking so much time. I was wondering with Dapper if I can automatically map such objects

public class Chest
{
  public Item Item {get;set;}
}

public class Item
{
   public IList<Property> Properties {get;set;}
}

public class Property
{
    public int Id {get;set;}
    public string Description {get;set;}
}

Is there a way I can retrieve all those items as I would do with EF?

I've seen the Query and so on but I don't understand if it meets the case

Upvotes: 1

Views: 323

Answers (1)

Mrinal Kamboj
Mrinal Kamboj

Reputation: 11478

Your model is pretty straight forward, since there's only 1 collection - IList<Property>, let's assume your query is Select Id, Description from PropertyTable, then using Dapper, you can do the following:

IList<Property> PropertyList = conn.Query<Property>("Select Id, Description from PropertyTable").ToList();

After that its simple assignment:

Chest chest = new Chest{Item = new Item{Properties = PropertyList}};

This still need extra assignment, since from Dapper you get IEnumerable<T> as result, there could be a Dapper Extension, which can directly fill the Chest object, if you provide explicit object mapping, though in my view its not required, since the solution is simple

Upvotes: 1

Related Questions