Orchardcms 从外部数据库加载目录,并将其作为ContentRecords链接到ContentParts

Orchardcms 从外部数据库加载目录,并将其作为ContentRecords链接到ContentParts,orchardcms,orchardcms-1.10,Orchardcms,Orchardcms 1.10,示例:我在另一个数据库中存储了一个国家目录,我需要在某些ContentParts中将其用作属性。我试图在不干扰Orchard布线的情况下建立连接 public class MoviePart : ContentPart<MoviePartRecord> { public IEnumerable<CountryRecord> Countries { get { return Record.Countri

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

public class MoviePart : ContentPart<MoviePartRecord>
{
    public IEnumerable<CountryRecord> Countries
    {
        get
        {
            return Record.Countries.Select(r => r.CountryRecord);
        }
    }
}
公共类电影部分:ContentPart
{
无数国家的公共卫生
{
得到
{
return Record.Countries.Select(r=>r.CountryRecord);
}
}
}
CountryRecords和MovieParts之间的关系将位于Orchard数据库中,但CountryRecord数据位于另一个数据库中。我只需要读访问权限,但我不知道如何重写处理程序以使用其他源

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

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

    protected override void Loading(LoadContentContext context)
    {
        base.Loading(context);
    }
}
公共类CountryPartHandler:ContentHandler { 公共CountryPartHandler(IRepository存储库) { Filters.Add(StorageFilter.For(repository)); } 受保护的覆盖无效加载(LoadContentContext上下文) { 加载(上下文); } } 更新:

在这段(大约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));
        });
    }
public-ProductPartHandler(IRepository存储库,Work-productServiceWork)
{
Filters.Add(StorageFilter.For(repository));
OnActivated((上下文、部分)=>{
part.ProductField.Loader(()=>productServiceWork.Value.GetProduct(part.Id));
});
}

但是在我的代码中,它找不到“Loader”函数,即使我也有视频中的所有引用,所以可能ProductField是一个自定义类型?

因此这是一个延迟字段,类似这样:

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);
         });
   }
}
公共类MyPart:ContentPart{
内部只读LazyField CustomDataField=新LazyField();
公共自定义数据自定义数据{
获取{return CustomDataField.Value;}
}
}
公共类自定义数据{
...
}
公共类MyPartHandler:ContentPartHandler{
私人ICCustomService(用户服务);
公共MyPartHandler(ICCustomService customService){
_customService=customService;
激活(初始化);
}
私有void初始化(ActivatedContentContext上下文,MyPart){
part.CustomDataField.Loader(()=>{
return\u customService.Get(part.ContentItem.Id);
});
}
}

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

太好了,谢谢。OnActivated函数是否足以让Orchard处理其余的逻辑,还是我必须对每个事件都这样做?(OnCreated,OnCreating…等)刚刚激活。当对象层次结构被创建时,我相信这是最低级别的事件。Oncreated用于创建内容项时:)