mapping to composite/nested object

Oct 29, 2011 at 3:32 AM

I've implement ValueInjecter, the problem is when I have the same field name like:

class Customer
{
   
private int id;
   
private string name;
   
private Company company;
}
class Company
{
   
private int id;
   
private string name;
}

and my query:

string sql = "select cust.id, cust.name, comp.name from customer cust inner join company comp on cust.company = comp.id"

and using sqldatareader:

while (dr.Read())
{
   
var cust = new Customer();
   cust
.InjectFrom<ReaderInjection>(dr);

   cust
.Company = new Company();
   cust
.Company.InjectFrom<ReaderInjection>(dr);

   list
.add(cust);
}

I've got cust.Id value same with cust.Company.Id and cust.Name = "" and cust.Company.Name = ""

Could you help me?

Coordinator
Oct 29, 2011 at 6:57 AM

it's not going to work, you need properties not fields ( get;set; missing) and they need to be public

Oct 29, 2011 at 8:36 AM

thanks for the reply,

I've already add setter and getter for each field, sorry I did not explain clearly.

class Customer
{
   
private int id;
   
private string name;
   
private Company company;

Customer(int id, string name, Company company)
{
this.id = id;
this.name = name;
this.company = company;
}
 
public int Id { set; get; }
public string Name { set; get; }
public Company Company { set; get; }
}
class Company
{
   
private int id;
   
private string name;

Company(int id, string name)
{
this.id = id;
this.name = name;
}

public int Id { set; get; }
public string Name { set; get; }
}

as I explain before, when mapped to cust.Company, the cust.Company.Name not filled (empty)