Orchard CMS连接外部SQL Server

汤东

我打算创建一些页面,这些页面显示带有Orchard CMS的外部SQL Server的数据。看来我需要编写一个新模块来实现此功能。是否有任何示例或想法可以实现此要求?

西普·肖尔斯特拉

是的,您将需要编写一个新模块,该模块提供一个新的内容部分和一个内容部分驱动程序。该驱动程序将负责从外部SQL Server提取数据,您将在外部SQL Server上将其设置为要从驱动程序返回的形状上的属性。形状的视图将显示您的数据。

本教程将引导您完成自定义内容部分的编写:http : //docs.orchardproject.net/en/latest/Documentation/Writing-a-content-part/

这样做时,请确保不要创建内容部分的记录类型,因为您将不会从Orchard数据库中存储和加载数据-您想从外部数据库加载数据。这些是您应该遵循的步骤:

  1. 创建一个新模块
  2. 创建内容零件类
    1. 让您的部分继承自ContentPart不是 ContentPart<TRecord>因为不会有任何“ TRecord”。
  3. 创建内容部分驱动程序
    1. Display方法上,通过调用ContentShape方法返回形状确保在lambda中添加SQL数据访问逻辑如果您在该lambda之外执行此操作,则每次调用使用您的内容部分的内容项时,都会调用该数据访问代码。尽管这听起来似乎正是您想要的,但这里涉及Placement.info的细微之处,可用于确定何时实际渲染形状或不渲染形状。如果放置逻辑确定不应渲染您的形状,则您不希望无所事事地访问外部数据。
  4. 创建一个Placement.info文件以配置形状的位置(在要呈现的内容项的上下文内)。
  5. 为在步骤3.2中返回的形状创建Razor视图。
  6. 创建一个Migrations类,该类将定义您的自定义内容零件以及要将零件添加到的所有内容类型。有关如何创建迁移的更多信息,请参见http://docs.orchardproject.net/en/latest/Documentation/Understanding-data-access/

PS。建议您不要在驱动程序中直接实现数据访问代码,而建议在单独的类中实现。因为您知道,关注点分离等。然后,您可以将该服务注入驱动程序。要在服务容器中注册服务类,请确保为其定义一个接口,该接口本身是从派生的IDependency

一些示例伪代码:

服务代码:

public interface IMyExternalDataStore : IDependency {
   IList<MyExternalDataRecord> GetMyData();
}

public class MyExternalDataStore : IMyExternalDataStore {
   public IList<MyExternalDataRecord> GetMyData() {
      // Connect to your SQL Server database, perhaps using EF, load the data and return it. Could of course also be simply a DataSet.
   }
}

内容部分:

public class MyExternalDataPart : ContentPart {
    // Nothing here, unless you want to include some properties here that influence the data that you want to load. If so, you'll also want to implement the Editor methods in your content part driver, but I'm keeping it simple.
}

内容部分驱动程序:

public class MyExternalDataPartDriver : ContentPartDriver<MyExternalContentPart> {

   private readonly IMyExternalDataStore _dataStore;

   public MyExternalDataPartDriver(IMyExternalDataStore dataStore) {
      _dataStore = dataStore;
   }

   protected override DriverResult Display(SlideShowProPart part, string displayType, dynamic shapeHelper) {

       return ContentShape("Parts_MyExternalData", () => {
          // Notice that we're performing the data access here within the lambda (the so called "shape factory method").
          var data = _dataStore.GetMyData();

          // Notice that I'm creating a property called "MyData"on the shape (which is a dynamic object).
          return shapeHelper.Parts_MyExternalData(MyData: data));
       }
   }
}

Parts_MyExternalData形状的剃刀视图:文件名:Parts.MyExternalData.cshtml

@{
   var records = (IList<MyExternalDataRecord>)Model.MyData;
}
<ul>
@foreach(var record in records) {
   <li>@record.ToString()</li>
}
</ul>

Placement.info:

<Placement>
   <Place Parts_MyExternalData="Content:0"/>
</Placement>

迁移:

public class Migrations : DataMigrationImpl {
    public int Create() {

       // Define your content part so that you can attach it to any content type from the UI.
       ContentDefinitionManager.AlterPartDefinition("MyExternalDataPart", part => part.Attachable());

       // Optionally, define a new content type here programmatically or attach it to an existing type.

       return 1;
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章