上面是我用来操作从我的域到dto模型的数据的代码,我用它来进行wcf序列化。我的问题是如何将对象母亲与儿童的集合传递给MotherDTO。使用当前代码情况,我只传递没有收集子项的数据。我是否需要在线使用会话并添加会话MotherDTO dto = new MotherDTO(数据,会话);并利用该会议来审视dto中儿童的收集。如果是这样,怎么样?请帮忙。
问候,
public MotherDTO GetMotherData()
{
using (ISession session = instance.OpenSession())
{
using (ITransaction tx = session.BeginTransaction())
{
Mother data = session.Query<Mother>()
.Fetch(x => x.Childrens)
.FirstOrDefault();
tx.Commit();
MotherDTO dto = new MotherDTO(data);
return dto;
}
}
}
MotherDTO.cs
public MotherDTO(Mother x)
{
Name = x.Name;
List<Children>Childrens= new List<Children>();
foreach (Children obj in x.Childrens)
{
States.Add(obj);
}
}
Mother.cs
public virtual string Name
{
get { return _Name; }
set
{
_Name = value;
}
}
public virtual Iesi.Collections.Generic.ISet<Children> Childrens
{
get
{
return _Childrens;
}
set
{
if (_Childrens == value)
return;
_Childrens = value;
}
}
答案 0 :(得分:0)
由于您已经(急切)加载了您的儿童系列,您可以使用Automapper填充您的DTO。
如果您想知道如何配置Automapper以使用嵌套集合,您可以阅读here:
Mapper.CreateMap<Order, OrderDto>()
.ForMember(dest => dest.OrderLineDtos, opt => opt.MapFrom(src => src.OrderLines));
Mapper.CreateMap<OrderLine, OrderLineDto>()
.ForMember(dest => dest.ParentOrderDto, opt => opt.MapFrom(src => src.ParentOrder));
Mapper.AssertConfigurationIsValid();