Frage

Havin a Response with a complex property, i want to to map to my responseDTO properly. For all basic types it works out flawlessly.

The ResponseDTO looks like this:

public class ResponseDto
{
    public string Id {
        get;
        set;
    }

    public struct Refs
    {
        public Genre GenreDto {
            get;
            set;
        }

        public Location LocationDto {
            get;
            set;
        }
    }

    public Refs References {
        get;
        set;
    }
}

Genre and Location are both for now simple classes with simple properties (int/string)

public class GenreDto {

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

}

Question: Is there any way, without changing/replacing the generic unserializer ( and more specific example) (in this example JSON ) to map such complex properties? One specific difference to the GithubResponse example is, that i cant use a dictionry of one type, since i have different types under references. Thats why i use a struct, but this seems not to work. Maybe only IEnumerable are allowed?

Update There is a way using lamda expressins to parse the json manually github.com/ServiceStack/ServiceStack.Text/blob/master/tests/ServiceStack.Text.Tests/UseCases/CentroidTests.cs#L136 but i would really like to avoid this, since the ResponseDTO becomes kinda useless this way - since when writing this kind of manual mapping i would no longer us Automapper to map from ResponseDto to DomainModel - i though like this abstraction and "seperation".

Thanks

War es hilfreich?

Lösung

I used lambda expressions to solve this issue, a more complex example would be

static public Func<JsonObject,Cart> fromJson = cart => new Cart(new CartDto {
        Id = cart.Get<string>("id"),
        SelectedDeliveryId = cart.Get<string>("selectedDeliveryId"),
        SelectedPaymentId = cart.Get<string>("selectedPaymentId"),
        Amount = cart.Get<float>("selectedPaymentId"),
        AddressBilling = cart.Object("references").ArrayObjects("address_billing").FirstOrDefault().ConvertTo(AddressDto.fromJson),
        AddressDelivery = cart.Object("references").ArrayObjects("address_delivery").FirstOrDefault().ConvertTo(AddressDto.fromJson),
        AvailableShippingTypes = cart.Object("references").ArrayObjects("delivery").ConvertAll(ShippingTypeDto.fromJson),
        AvailablePaypmentTypes = cart.Object("references").ArrayObjects("payment").ConvertAll(PaymentOptionDto.fromJson),
        Tickets = cart.Object("references").ArrayObjects("ticket").ConvertAll(TicketDto.fromJson)
    });

So this lamda exprpession is used to parse the JsonObject response of the request and map everything inside, even nested ressources. This works out very well and flexible

Andere Tipps

Some time ago i stumbled upon a similar problem. Actually ServiceStack works well with complex properties. The problem in my scenario was that i was fetching data from a database and was passing the objects returned from the DB provider directly to ServiceStack. The solution was to either create DTOs out of the models returned by the DB provider or invoke .ToList() on those same models.

I'm just sharing some experience with SS but may be you can specify what's not working for you. Is there an exception thrown or something else.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top