Widget with custom Data Access

Topics: Customizing Orchard, Writing modules
Jul 12, 2011 at 3:58 PM

We are building a widget that talks to a web service behind the scenes rather than the Orchard database. However, I can't see the best way to do this.


As far as I can see if I want to use a widget I need to use a Driver class. I can do the web service call in the driver, but that doesn't look to be the right place (if I have a ContentPartDriver the display method is passed the model which I presume should already be populated with data by that point).


I could easily write a custom controller to do this but then as far as I can see I wouldn't be able to use a widget. Is this correct?


Any ideas on how we should be approaching this? Should we be writing a custom repository?

Jul 12, 2011 at 4:23 PM
Edited Jul 12, 2011 at 4:25 PM

As far as I understand, business logic of any part (and accordingly widget) may (or even should) be moved into the 'service' class.
Many of standard Orchard modules (e.g. Blogs) use this approach - so talking to external data sources may be implemented within service - you can look through the code of standard module to get the idea. Service should implement IDependency interface in order to be accessible for driver via DI.
Another possible approach is using XmlRpc mechanism.

Jul 13, 2011 at 8:16 AM

Thanks for your reply. Putting the logic into the service does make sense. However, is using a ContentPartDriver the right thing to do in this case? As far as I can see we don't need the part object passed to the Display and Editor methods (the generic set on the ContentPartDriver)  because we are just going to access the service in the driver and then pass data down to the view.


I guess if we have some configuration properties, such as the number of items to display, then we could store those in the part.



Jul 13, 2011 at 9:57 AM
Edited Jul 13, 2011 at 9:58 AM

Possibly I don't fully get the context, but how are you going to pass data retrieved from the external source to the view? I guess this will be a simple custom view model, but if you would like to avoid using parts, then what your widget will consist of?
Typically widget will include a part, managed by the driver, which will call your 'service' class. If you drop a part, it's not clear for me how this chain will work..

I propose you looking through the code of the following module: http://orchardproject.net/Gallery/List/Modules/Orchard.Module.WeatherDotCom, it does something similar to your task. It uses a part for the settings purposes and passes a plain view model to the view - seems just like you want to do. + it talks to external service.

Jul 13, 2011 at 10:53 AM

Okay, the weather.com module does do exactly the type of thing we need to do. So the WeatherPart is only actually used in the Editor methods (it is passed to the display view, but not actually used).


I was thinking that maybe we needed to do something more complex such as implementing a custom repository that accessed our service.


Out of interest, if I had some content that I wanted users to be able to place anywhere on the page (i.e. it needs to be a widget), but had no need of any configuration in the admin (i.e. all I needed was the Display method in the driver), would I still need to use a ContentPartDriver for that? I would have thought in that situation it would have been easier just to have a controller action, but as far as I can see that wouldn't plug into the way Orchard displays shapes.


Thanks for your help!

Jul 13, 2011 at 10:33 PM

A controller action is for the whole request, and a driver is just for a content part. It's the same sort of concept, but it is scaled down to a part of the page instead of the whole of it. This seems to be exactly what you are asking for.

Jul 17, 2014 at 2:00 PM
we have similar problem. We would like to use Orchard as portal solution for some kind of users and we use our external data source for storing informations about these users. But we use Orchard login feature and Orchard's userId is "virtualy connected" to our DB where are more detailed infos, roles etc. In controller we can read data from Entity Framework Model through our DAL layer, on Views we can select user from dropdownlist, send his UserId to some controller's actions or write it to cookie and than load user's data and show it on main content. Now we are developing a widget, which should show some specific part of data on aside column in our theme, but if i can load data, i need to know selected UserId, but in Driver or Handler objects there aren't Request namespace to getting cookie, or some routing mechanism like in controllers actions, where we can get some optional parameters... All these features (main content, widgets etc.) should be in our custom module.
How can we solve this? Is there any samples with code?

Thanks for your help!