Creating a custom data consuming module

Topics: General, Writing modules
May 27, 2011 at 3:32 PM

1) Does Orchard have any built-in mechanisms for data exchange between custom modules? I'm going to make a module that will consume data from other parts of application, so would like it to publish some kind of API to be implemented by other Orchard modules. I guess the answer will be using dependency injection, but possibly there are some details on this topic.

2) Another related question is - so I would like other modules to implement my API (treating them as data sources) in order to get them as injections to my model constructor and call pre-defined data retrieval methods. How should I carry this out (multiple implementations injecting using AutoFac)? Will this approach work - having interface declaration in one module and its implementations in other modules (I mean does it conform Orchard best practices)?

Thanks in advance.

May 27, 2011 at 3:40 PM
Edited May 27, 2011 at 3:40 PM

There are two common approaches;

1. Service pattern

This involves creating an IMyService (which inherits from IDependency) and a default implementation. Consumers can import this dependency to access methods on your service. Typically there is one service implemented per module or feature.

2. Event / provider pattern

This involves creating an interface which with inherits from IDependency or IEventHandler; then in your service or elsewhere you can import an IEnumerable<ISomeProvider>. With an event handler you just import IMyEventHandler and a single call to it will actually call all the implementors. Using either of these methods you can consume implementations from modules so other people can then extend your module.

I use this a lot in Science Project and Media Garden; both those projects are implemented as a number of separate modules that interact with each other in this way.