This project is read-only.

Show content from another data source

Jan 26, 2011 at 4:05 PM

I would like to show content from another datasource (not the orchard database) and was wondering how to approach this.

Jan 26, 2011 at 4:39 PM
Edited Jan 26, 2011 at 4:41 PM


There are couple of ways to solve this situation:

  • Storing whole custom content part outside.

You can write your own implementation of generic Orchard IRepository<YourContentPartRecord> to point to different datasource as a place for storing your content part. Then, you have to set StorageFilter in YourContentPart handler to this repository. In this situation I assume that you create custom content parts in your module.

  • Displaying some data from your source in content part.

You can create a ContentPart and let Orchard handle it's storage (default - how to create content parts is explained well in docs). Then, you create your custom business object eg. MyObjectFetchingDataFromOutside and decorate it with custom interface eg. IOutsideSourceFetcher decorated with IDependency interface (needed for IoC dependency resolving). In the last step you add parameter IOutsideSourceFetcher to your part driver (so the instance is passed to the driver object) and voila - you can pass whatever you want from your business object to your shape to display (Display method)

  • Displaying some data directly from controller without any custom parts (as in ordinary ASP.NET MVC app).

You don't have to create any content parts, only some controllers with which you display your data. In this case it's analogous to 2. - create custom business object, and it's interface decorated with IDependency and add a parameter typed as this interface to the controller constructor (in former case you passed it to the driver). And now you can use your object inside controller actions to display your data in views (as in every ASP.NET MVC application).

Summarising, the whole thing about showing data from different sources can be narrowed to:

  1. Creating custom business object
  2. Creating an interface for the object and decorating it with IDependency
  3. Putting a parameter (typed as that interface) in another object constructor (driver, controller and whatever else implements IDependency). IoC container automatically injects an appropriate instance (in this case your business object) when constructing those objects.
  4. Using the object passed to the constructor.

I hope it was helpful:)

Cheers, Piotr