Alvin
Alvin

Reputation: 302

ValueInjecter not binding properties correctly

I am having trouble getting SourceModel data getting mapped to DestinationModel data. The DestinationModel has complex object type. Although the name matches but I don't see any data getting bind correctly. I am new to ValueInjector and as per my understanding this is what I have tried.

public class SourceModel
{
    [Column("ctr_shname")]
    public string CtrShname { get; set; }

    [Column("reg_name")]
    public string RegName { get; set; }

    [Column("Male")]
    public Int64 Male { get; set; }

    [Column("Female")]
    public Int64 Female { get; set; }

    [Column("Single")]
    public Int64 Single { get; set; }

    [Column("Married")]
    public Int64 Married { get; set; }

    [Column("Divorced")]
    public Int64 Divorced { get; set; }

    [Column("Separated")]
    public Int64 Separated { get; set; }

    [Column("Widowed")]
    public Int64 Widowed { get; set; }
}

public class DestinationModel
{
    public string CtrShname { get; set; }
    public string RegName { get; set; }
    public Gender Genders { get; set; }
    public MaritalStatus MaritalStatuses { get; set; }
}

public class Gender
{
    public Int64 Male { get; set; }
    public Int64 Female { get; set; }
}

public class MaritalStatus
{
    public Int64 Single { get; set; }
    public Int64 Married { get; set; }
    public Int64 Divorced { get; set; }
    public Int64 Separated { get; set; }
    public Int64 Widowed { get; set; }
}

And this is my code to map.

// get data from DB (row count 123)
IEnumerable<SourceModel> data = GetDataFromDB();
List<DestinationModel> finalAnswer = new List<DestinationModel>();

// Try 1: all properties are null for all 123 records
finalAnswer.InjectFrom(data);

// Try 2: Zero count. Nothing gets binds
var mapper1 = new MapperInstance();
finalAnswer = mapper1.Map<List<DestinationModel>>(data);

Please help how do I correctly map?

Upvotes: 0

Views: 105

Answers (1)

Bruno Caceiro
Bruno Caceiro

Reputation: 7189

I think the ValueInjector only allows the injection a single object, however you can do this.

   IEnumerable<SourceModel> data = GetDataFromDB();
IList<DestinationModel> finalAnswer = categoryList
    .Select(x => new DestinationModel().InjectFrom(x)).Cast<DestinationModel>()
    .ToList();

Or do a foreach and inject each object:

foreach (var a in data)
{
    finalAnswer.InjectFrom(a);
}

Upvotes: 1

Related Questions