【问题标题】:Populating a DTO with a property list of DTO by using transform使用转换使用 DTO 的属性列表填充 DTO
【发布时间】:2011-11-24 17:36:58
【问题描述】:

我有一个名为 ActivityLog 的实体:

    public class ActivityLog : EntityModel<ActivityLog>
{
    public virtual int activityID { get; set; }
    public virtual int entityType { get; set; }
    public virtual int entityID { get; set; }
    public virtual string entityName { get; set; }
    public virtual int action { get; set; }
    public virtual int parentType { get; set; }
    public virtual int parentID { get; set; }
    public virtual string parentName { get; set; } }
    public virtual string userName { get; set; }
    public virtual int instanceID { get; set; }
    public virtual DateTime? timeStamp { get; set; }
    public virtual DateTime? groupTimeStamp { get; set; }
}

还有一个名为 Activity 的 DTO 类:

    public class Activity
{
    public virtual int activityID { get; set; }
    public virtual int entityType { get; set; }
    public virtual int entityID { get; set; }
    public virtual string entityName { get; set; }
    public virtual int action { get; set; }
    public virtual int parentType { get; set; }
    public virtual int parentID { get; set; }
    public virtual string parentName { get; set; }
    public virtual string userName { get; set; }
    public virtual int instanceID { get; set; }
    public virtual DateTime? timeStamp { get; set; }
    public virtual DateTime? groupTimeStamp { get; set; }
    public IList<Activity> activities { get; set; }
}

我需要使用转换从实体填充 DTO 我还想用具有 parentTypeparentID 的实体填充 IList&lt;Activity&gt;。最好的方法是什么,最少的查询?

【问题讨论】:

标签: nhibernate list entity transform dto


【解决方案1】:
IList<ActivityLog> activityLogs = ...;

var activities = session.Query<Activity>()
    .WhereRestrictionOn(a => a.Parent.Id).IsIn(activityLogs.Select(al => al.parentID))
    .WhereRestrictionOn(a => a.Parent.Type).IsIn(activityLogs.Select(al => al.parentType))
    .AsEnumerable()
    .ToLookUp(a => new { ParentId = a.Parent.Id, ParentType = a.Parent.Type });

var results = activityLogs
    .Select(al => new ActivityDTO
    {
        activityID = al.activityID,
        entityType = al.entityType,
        ...
        activities = activities[new { ParentId = al.parentID, ParentType = al.parentType }].ToList()
    });

【讨论】:

    【解决方案2】:

    您可以使用 ProjectionList 和 AliasToBean 结果转换器:

    var criteria = session.CreateCriteria <ActivityLog>();
    criteria.SetProjection (Projections.ProjectionList()
                                         .Add (Projections.Property ("activityID"), "activityID")
                                         ...);
    
    criteria.SetResultTransformer (Transformers.AliasToBean <Activity>());
    
    return criteria.List<Activity>();
    

    【讨论】:

    • 我试过这个,但它不适用于IList&lt;Activity&gt; activities@Firo 的答案看起来更合适。感谢您的回答。
    猜你喜欢
    • 1970-01-01
    • 2013-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多