How to suppress a specific IRepository

Topics: Customizing Orchard
Apr 21, 2012 at 4:23 AM

I guess I'll start with a brief explanation of the scenario so you know where I am at right now.  As I'm sure most are aware, the stock Orchard system uses nHibernate to persist the content items in the database.  For this specific custom feature, I'm attempting to create a content part which sources its data from a remote web service.  Let's call it "SampleContentPart".  Basically I want to set this up so that Orchard knows to forward all repository requests for standard content to the database like normal, but for the part "SampleContent", perform the persistence using my code.  I believe the repository pattern is setup to abstract this functionality for just such a purpose.

I'm familiar with the process of suppressing a specific dependency using the OrchardSuppressDependency attribute and then passing in the name of the default interface implementation that you want to override (i.e. [OrchardSuppressDependency("Orchard.Blogs.Services.BlogService")]).  I expected to be able to create a new class:

public class SampleContentRepository : IRepository<SampleContentRecord>

and have AutoFac just do the dependency resolution.  I have tried various suppress dependency attributes, but the repository constructor was never called in the debugger.  I do have a handler defined for the SampleContentRecord. 

I then created an AutoFac module which registered the SampleContentRepository type manually.  After doing this, when debugging I could see that the constructor of the SampleContentRepository was getting called, but the content manager was not using the custom repository.  It was using the standard nHibernate repository for content items:

Orchard.ContentManagement.DefaultContentManager (line 127)  
_contentItemVersionRepository.Get(options.VersionRecordId);

I think I'm getting very close to having this working.  My stumbling block seems to be getting Orchard to not use the nHibernate-based Orchard.Data.Repository<T> for one specific type of ContentRecord.  If you look in Orchard.Data.DataModule you can see where the generic repository type is getting registered:

Orchard.Data.DataModule.cs (line 9) 
builder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>)).InstancePerDependency();

The custom AutoFac module is present and registering the SampleContentRepository type correctly.  There must be some additional AutoFac trickery to set the precedence or otherwise tell the dependency resolver to "use this one, not that one".

I would appreciate any assistance, thanks,

Sep 20, 2012 at 8:31 PM

Hello.

Did you manage to make this work?

Or has anyone found a solution to the problem of suppressing a specific IRepository?

Thanks

Sep 23, 2012 at 2:09 AM

No.  I ended up attacking the problem a different direction.