mappingvalueinjecter

Map a dynamic object with ValueInjecter


I'm retrieving data with Simple.Data - which maps the database table to a dynamic object. I want to map the dynamic object to a simple type. I have tried this:

var dbObject = Database.Default.LocationStatus.FindByLocationStatusId(locationStatusId);
ILocationStatus domainObject = new LocationStatus();
domainObject.InjectFrom((object)dbObject);

But no properties in the domainObject are set. The mapping should be simple as the property names are the same, ei: dbObject.Name and domainObject.Name

Where am I going wrong? Note: I can in fact magically cast (duck typing?) (LocationStatus)dbObject but I'd like to know how to map with ValueInjecter. Thanks.


Solution

  • Strange as it may sound, I ran into this same problem a few days ago, and the solutions was simple.

    You need to cast the output of your dynamic to the type your trying to map too.

    In my case:

    WeatherData myData = new WeatherData().InjectFrom((object)weatherData);
    

    as shown in the post referenced in the comments above didn't work (I suspect with the same problem as the original poster), but when cast using as ...

    WeatherData myData = new WeatherData().InjectFrom((object)weatherData) as WeatherData;
    

    Everything works fine.

    So it seems even with the newer versions, 3+ years later this can still be an issue, and casting the output type is the fix.