0

本文介绍如何为您的零件编写高效的 DisplayDrivers,以便仅在实际显示形状时执行昂贵的代码。

protected override DriverResult Display(MyPart part, string displayType, dynamic shapeHelper)
{
    // return the shape
    return ContentShape("Parts_MyPart", () => {
        // do computations here
        return shapeHelper.Parts_MyPart();
    });
}

现在我想使用该Combine方法制作一个返回多个 DriverResults 的 Part,每个 DriverResult 包含大部分相同的数据,这些数据是从数据库中获取的。问题是我想不出一种提高效率的好方法,因为Combine它不采用 Func 参数。

protected override DriverResult Display(MyPart part, string displayType, dynamic shapeHelper)
{
    var data = ... // expensive query specific to the part

    return Combined(
       ContentShape("Parts_MyPart_A", () => shapeHelper.Parts_MyPart_A(
            Data: data
        )),
        ContentShape("Parts_MyPart_B", () => shapeHelper.Parts_MyPart_B(
            Data: data
        )),
        ContentShape("Pars_MyPart_C", ...
    );
}

我可以实现相同的结果,以便在不显示任何内容时不执行查询,并且在显示多个形状时只执行一次?

我想这样做,以便我可以在具有不同标记和样式的不同区域中的 ContentItem 的详细信息上显示相同的数据。另一种方法可能是返回一个形状,然后将其他形状推入不同的区域,但随后我将失去使用 Placement 单独控制每个形状的能力。

4

1 回答 1

1

我可能会在您的部分中添加一个惰性字段。

public class MyPart : ContentPart {
    internal readonly LazyField<CustomData> CustomDataField = new LazyField<CustomData>();

    public CustomData CustomData {
      get { return CustomDataField.Value; }
    }
}

public class CustomData {
    ...
}

public class MyPartHandler : ContentPartHandler {

   private ICustomService _customService;

   public MyPartHandler(ICustomService customService){
      _customService = customService;
      OnActivated<MyPart>(Initialize);
   }

   private void Initialize(ActivatedContentContext context, MyPart part){
         part.CustomDataField.Loader(() => {
             return _customService.Get(part.ContentItem.Id);
         });
   }
}

只有在加载时才会计算它,并且所有形状都将共享计算值。

于 2017-08-29T14:36:17.163 回答