我正在尝试使用6.4.1版的dozermapper来映射和覆盖具有新对象的对象列表。
更准确地说,当映射完成时,我想要一个包含仅目标类别的项目的列表。
这是我要映射的字段。目标类和源类都具有确切的字段。
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;
using System;
using System.Linq;
namespace StackOverflow
{
public class Program
{
public class Entity
{
[BsonId]
public ObjectId Id { get; set; }
public DateTime TimeStamp { get; set; }
}
public class Sample : Entity
{
public string Something { get; set; }
}
private static void Main(string[] args)
{
var collection = new MongoClient("mongodb://localhost:27017")
.GetDatabase("Test")
.GetCollection<Entity>("Samples");
var sample = new Sample
{
Id = ObjectId.GenerateNewId(),
Something = "something",
TimeStamp = DateTime.UtcNow
};
collection.InsertOne(sample);
var result = collection.AsQueryable()
.Where(s =>
s.TimeStamp >= DateTime.UtcNow.AddMinutes(-1) &&
s.TimeStamp <= DateTime.UtcNow.AddMinutes(1))
.ToArray();
}
}
}
图片类:
@Getter
@Setter
public class UpdateProductRequest {
private Images images;
}
最初,我的目标类别( Product )的每个列表中都有2个项目。
@Getter
@Setter
public class Images {
private List<String> small;
private List<String> medium;
private List<String> large;
}
我的目标类别( UpdateProductRequest )中每个1个项目:
{
"images":{
"small":[
"https://google.com",
"https://google.com"
],
"medium":[
"https://amazon.com",
"https://amazon.com"
],
"large":[
"https://microsoft.com",
"https://microsoft.com"
]
}
}
具有此映射器配置...
{
"images":{
"small":[
"aa"
],
"medium":[
"bb"
],
"large":[
"cc"
]
}
}
...将目标类中的项目添加到源类中:
<mapping map-null="false" type="one-way">
<class-a>com.project.api.models.requests.UpdateProductRequest</class-a>
<class-b>com.project.api.persistence.domain.Product</class-b>
<field remove-orphans="true">
<a>images</a>
<b>images</b>
</field>
</mapping>
具有此映射器配置...
{
"images":{
"small":[
"https://google.com",
"https://google.com",
"aa"
],
"medium":[
"https://amazon.com",
"https://amazon.com",
"bb"
],
"large":[
"https://microsoft.com",
"https://microsoft.com",
"cc"
]
}
}
...产生以下结果:
...
<!-- Specifying the relationship-type doesn't change a thing.-->
<field remove-orphans="true" relationship-type="non-cumulative">
<a>images.small</a>
<b>images.small</b>
</field>