2

示例:我有一个国家目录存储在另一个数据库中,我需要将它用作某些 ContentParts 中的属性。我正在尝试在不干扰 Orchard 布线的情况下建立连接。

public class MoviePart : ContentPart<MoviePartRecord>
{
    public IEnumerable<CountryRecord> Countries
    {
        get
        {
            return Record.Countries.Select(r => r.CountryRecord);
        }
    }
}

CountryRecords 和 MovieParts 之间的关系将在 Orchard 数据库中,但 CountryRecord 数据在另一个数据库中。我只需要读取访问权限,但我不知道哪个以及如何覆盖处理程序以使用其他源。

我是否需要创建一个 ContentHandler 并覆盖所有方法,并创建另一个使用新存储库和外部源的 StorageFilter?我如何将新的 repo 注入到处理程序中?

public class CountryPartHandler : ContentHandler
{
    public CountryPartHandler(IRepository<CountryPartRecord> repository)
    {
        Filters.Add(StorageFilter.For(repository));
    }

    protected override void Loading(LoadContentContext context)
    {
        base.Loading(context);
    }
}

更新:

在这个Using External Data with Orchard(大约第 25 分钟)视频中,他似乎正在用这段代码做我需要的事情:

public ProductPartHandler(IRepository<ProductPartRecord> repository, Work<IProductService> productServiceWork)
    {
        Filters.Add(StorageFilter.For(repository));

        OnActivated<ProductPart>((context, part) => {
            part.ProductField.Loader(() => productServiceWork.Value.GetProduct(part.Id));
        });
    }

但是在我的代码中它找不到“加载器”功能,即使我也有视频中的所有引用,所以也许 ProductField 是一个自定义类型?

4

1 回答 1

2

所以这是一个懒惰的领域,像这样:

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);
         });
   }
}

我不知道您是如何加载外部数据的,无论是通过 rest、wcf 等,但逻辑可以直接放入自定义服务中

于 2017-11-26T12:11:19.693 回答