Map between List of composed dbo's and dto's

Hi there,

I’m trying to map between a list of composed Dbo’s to a list of Dto’s using AutoMapping.

Consider the flowing code:

public class Car
{
	public string Model {get; set;}
	public int Year {get; set;}
	public int MaxSpeed {get; set;}
}

public class TechRevision 
{
	public bool Ok {get; set;}
	public string RevisionDate {get; set;}
}

public class CarRevisionDbo
{
	public Car Car {get; set;}
	public List<TechRevision> TechRevisions {get; set;}
}

public class CarRevisionDto
{
	public string Model {get; set;}
	public int Year {get; set;}
	public int MaxSpeed {get; set;}
	public List<TechRevision> TechRevisions {get; set;}
}

i would like to map between a list of CarRevisionDbo to a list of CarRevisionDto, where Car is also destructed (maped) into CarRevisionDto.

You can see the code here: https://gistlyn.com/?gist=52c37e37b51a0ec92810477be34695ae.

I expect the following result: [{“Year”:2000,“MaxSpeed”:190,“Model”:“BMW”,“TechRevisions”:[{“Ok”:true,“RevisionDate”:“2004”},{“Ok”:true,“RevisionDate”:“2008”}]},{“Year”:“2002”,“MaxSpeed”:190,“Model”:“VW”,“TechRevisions”:[{“Ok”:true,“RevisionDate”:“2006”},{“Ok”:true,“RevisionDate”:“2010”}]}]

Is there any way to do this using AutoMapping?

This result is easily achieved using Automapper library ( https://automapper.org/ ). Why ServiceStack does not use this library?

Cheers,
WLab

The gistlyn link doesn’t contain your example, did you forget to save it? Feel free to paste the entire runnable source code here as well. When pasting source code paste them within code blocks so it’s properly escaped/formatted, e.g:

```csharp
// C# code here
```

If you can do what you want using AutoMapper go ahead and use it, Our built-in Auto Mapping provides a fast, intuitive mapping of matching properties and convertible types/collections, for any custom mapping you can use a Custom Converter. But it’s just an alternative, feel free to use any library you prefer.

But there’s absolutely no need for all ServiceStack projects to depend on an external 3rd Party dependency which it has no reason to use, it would only be a potential source of breaking changes and an unnecessary dependency to all projects that doesn’t use it.

Should work now. https://gistlyn.com/?gist=e90a4d3216c95ba2cdfaedfba124c10c

The shape of the types don’t match, so you need a custom converter:

AutoMapping.RegisterConverter((CarRevisionDbo from) => {
    var to = from.ConvertTo<CarRevisionDto>(skipConverters:true);
    to.PopulateWith(from.Car);
    return to;
});

I’ve modified this example at:

https://gistlyn.com/?gist=dc7d2c2ba523ba657c219f4d40a1591c

1 Like

Thank you for fast response. I’ve tried using Custom Converter with no success in my case.
I would prefer using ServiceStack AutoMapping over Automaper, i was just wondering if any integration was possible.

Doesn’t the updated example do what you need?

Not sure what integration you’re hoping for, they’re just Mapping libraries used within your own Service implementations, ServiceStack doesn’t even use its own built-in AutoMapping to execute your Services. You can use whichever library you prefer, your Service implementation is opaque to ServiceStack.

Yes, is the solution i was looking for.