3

I have one table but I'd like to map it as a composition between two classes.

Suppose I have a Customer table with following fields: Id, Name, ExtraDataValue, ExtraDataDate.

class Customer
{
  public int Id {get;set;}
  public string Name {get;set;}
  public ExtraData Extra {get;set;}
}

class ExtraData
{
  public int Value {get;set;}
  public DateTime Date {get;set;}
}

What is the best way to query it and fill ExtraData inside Customer object?

Edit:

I'll extend the question to a more complex scenario, since XenoPuTtSs's answer solves the first one but will not work with this one.

Suppose now that I have a join to address table. The splitOn option will split the result to create ExtraData but will fail to split to create Address.

I see another problem using splitOn. We can't use Select * safely because if we alter table with more fields, we have to always remember to move split fields to bottom. Or we will always have to describe all fields in safe order in Select command.

5
  • I agree with what you are saying about the "Select * ", but doing that is typically bad practice anywhere for the same reasons. splitOn is being used because the default key is not "Id"
    – XenoPuTtSs
    Apr 14, 2015 at 18:21
  • But in this case I really want all fields, so Select * is perfect. I understand now the use of splitOn but it has a problem in this case. I want to split by a field name (for ExtraData) and for a key (for Address Table). Maybe if it accepts a list of split option would be better. Something like splitOn: new {"Id", "ExtraDataValue"} Apr 14, 2015 at 18:32
  • 1
    splitOn: "Value1, ID, SomethingElse, Chickens"
    – XenoPuTtSs
    Apr 14, 2015 at 18:44
  • Now I see that this is not necessary. The splitOn parameter is used by other values beside "Id". Apr 14, 2015 at 19:00
  • Forget about this comment. I was wrong. Look at my comments in the other answer. Apr 14, 2015 at 19:34

2 Answers 2

2

I was just looking this up for myself. This is what I came up with, changed to your scenario.

class Customer
{
  public int Id {get;set;}
  public int Name {get;set;}
  public ExtraData Extra {get;set;}
}

class ExtraData
{
  public int Value {get;set;}
  public int Date {get;set;}
}

using (var conn = DatabaseService.CreateConnection())
{
    var t = conn.Query<Customer, ExtraData, Customer>(@"
    select 
        cust_num as Id,
        cust_name as Name,
        ex_data1 as Value,
        ex_date as Date
    from Customer
    ",
     (cust, extra) =>
     {
         cust.Extra = extra;
         return cust;
     },
    splitOn: "Value");
    return t;
}
3
  • I tested your solution and it works, but it didn't work in my case because I have a join with other table. I really don't know if I should mark your answer as a solution because it doesn't work in all cases. I understand that this is a Dapper limitation and maybe we don't have a better solution. I'll wait for a while to see if we get another idea. Thanks. Apr 14, 2015 at 14:28
  • Can you post your query? I updated my answer to have a query that joins on to the "extraData" table.
    – XenoPuTtSs
    Apr 14, 2015 at 17:26
  • Sorry, I wasn't clear enough. ExtraData is in the same table, just like you coded first time. I have a join with another table (Not ExtraData). In my case it is an address table. I think you should rollback to your last answer because that was correct. Apr 14, 2015 at 17:58
1

splitOn:"Id, ExtraDataValue, Id"I believe this answers your newest rendition of the question. Now you have a join to address and it populates the address portion of your customer object.

class Customer
{
  public int Id {get;set;}
  public int Name {get;set;}
  public ExtraData Extra {get;set;}
  public Address Address{get;set;} 
}

class ExtraData
{
  public int Value {get;set;}
  public int Date {get;set;}
}

class Address {
    public string line1{get;set;}
}

using (var conn = DatabaseService.CreateConnection())
{
    var t = conn.Query<Customer, ExtraData, Address, Customer>(@"
    select 
        c.cust_num as Id,
        c.cust_name as Name,
        c.ex_data1 as Value,
        c.ex_date as Date,
        a.*
    from Customer c
     join Address a on c.addressid = a.addressid
    ",
     (cust, extra, address) =>
     {
         cust.Extra = extra;
         cust.Address = address;
         return cust;
     },
    splitOn:"Id, Value, Id");
    return t;
}
2
  • I tried this option too but it is not working. I always get this error: "When using the multi-mapping APIs ensure you set the splitOn param if you have keys other than Id". I already set splitOn but it only works without the join. Maybe I'm doing something wrong. Apr 14, 2015 at 19:00
  • It's working! I had to use splitOn:"Id, ExtraDataValue, Id". The first Id for Customer and the last one for Address. Strange, but it worked. Thanks a lot. Do you want to change on of your answers? I'd like to vote as a solution. Apr 14, 2015 at 19:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.