vendredi 20 mai 2016

Trouble Setting internal objects within a Poco

The sample:

public class Order
{
     public int Id { get; set; }
     public decimal Shipping { get; set; }
     public decimal Tax { get; set; }
     public decimal Total { get; set; }
     public IEnumerable<Product> Products { get; set; }
}

    protected void MapEntity<TEntity>(IDataReader reader, Dictionary<string, string> properties, ref TEntity model)
    {
        var table = Enumerable.Range(0, reader.FieldCount).Select(reader.GetName).ToArray();
        foreach (var column in table)
        {
            var matchColumnToProperty = properties.SingleOrDefault(property => String.Compare(property.Key, column, true) == 0).Key;
            var matchColumnToAttribute = properties.SingleOrDefault(property => String.Compare(property.Value, column, true) == 0).Value;

            if (matchColumnToProperty != null)
                if (!reader.IsDBNull(reader.GetOrdinal(matchColumnToProperty)))
                    typeof(TEntity).GetProperty(matchColumnToProperty).SetValue(model, reader.GetValue(reader.GetOrdinal(matchColumnToProperty)), null);

            if (matchColumnToAttribute != null)
                if (!reader.IsDBNull(reader.GetOrdinal(matchColumnToAttribute)))
                    typeof(TEntity).GetProperty(properties.SingleOrDefault(property => String.Compare(property.Value, matchColumnToAttribute, true) == 0).Key)
                        .SetValue(model, reader.GetValue(reader.GetOrdinal(matchColumnToAttribute)), null);
        }
    }

So the issue, why when I call:

using(var command = new dbCommand(dbConnection))
{
     var orders = BuildEntity<Order>(....);
}

All of the Product data doesn't exist? The relevant part of BuildEntity would be:

using(var reader = command.ExecuteReader())
    while(reader.Read())
    {
        var model = new TEntity;
        MapEntity(reader, properties, ref model);
        collection.Add(model);
    }





Aucun commentaire:

Enregistrer un commentaire