How do I translate complex objects in ServiceStack?

后端 未结 2 925
醉话见心
醉话见心 2021-02-10 09:22

Suppose I have two objects:

class Order
{
    string Name {get; set;}
    Customer Customer {get; set;}
    Item[] Items {get; set;}
}

and

2条回答
  •  醉酒成梦
    2021-02-10 09:59

    You are going to have to handle complex mapping explicitly yourself. Here are some unit tests from ServiceStack src that show how complex types are currently handled.

    You can see that the the Car object is serialized into JSON.

    var user = new User() {
        FirstName = "Demis",
        LastName = "Bellot",
        Car = new Car() { Name = "BMW X6", Age = 3 }
    };
    
    var userDto = user.TranslateTo();
    
    Assert.That(userDto.FirstName, Is.EqualTo(user.FirstName));
    Assert.That(userDto.LastName, Is.EqualTo(user.LastName));
    Assert.That(userDto.Car, Is.EqualTo("{Name:BMW X6,Age:3}"));
    

    I agree with Trust Me - I'm a Doctor that Automapper is worth using. The built in Translating was designed to reduce dependencies.

提交回复
热议问题