IContentManager vs IRepository

Topics: Customizing Orchard, Writing modules
Apr 9, 2011 at 9:14 PM


   Can someone give a detailed explanation of the intended difference between IContentManager and IRepository for querying purposes?  For example, the Blog module makes use of an IBlogService implementation which uses the ContentManager to query for information.  Why is it using this instead of IRepository?  When do you use one instead of the other?

Apr 11, 2011 at 1:24 AM

IContentManager is a service implementation which itself accesses various IRepository instances to query and manipulate data in the database.

So, if within your module you are creating new database tables, you should generally create some sort of IMyModuleService which is where you'll import an IRepository<MyModuleRecord> and do the data manipulation.

Then, any other parts of your module that need to perform data access should just talk to IMyModuleService, rather than attempting any access of IRepository.

If you specifically want to access any Content Items from the database, just use IContentManager; you should never directly talk to IRepository<ContentItem> since the IContentManager performs a lot of additional useful functions.

The basic message is that you should always keep all your database access in one place; this will make it far easier if you ever need to swap it out for a different storage mechanism or for testing purposes.