How to Easily Eject and Replace an Episerver Registered Implementation

casper.rasmussen/ January 13, 2019/ Uncategorized/ 0 comments

Even though it may sound like an edge-case for some, it is something you once in a while may be required to do in order to achieve the desired functionality. Dependent on the kind of registration Episerver used to setup their implementation, the approach for you to replace it will be different.

StructureMap describes; “Registrations in the Registry DSL can be done with either Add() or Use() methods, but they have a different semantic meaning to StructureMap. Add() means add another Instance to this plugin type while Use() means this one is the default.”. It boils down to the fact that when multiple implementation has been added, either via multiple Add() within an InitializationModule or by executing[ServiceConfiguration(typeof(IInterface))] multiple times, the Registry DSL has more than a single implementation option for a given plugin.

Disclaimer: We’re using the StructureMap API’s directly since Episerver’s ServiceLocation abstraction doesn’t give us sufficient flexibility!

How to Eject and Replace a Single Implementation to one Plugin – Use()

What we’re doing is simply to tell StructureMap that we have a new default implementation we would like to use for the IInterface plugin.

How to Eject and Replace a Single Implementation When Multiple Implementations Exist for one Plugin- Add()

We’re assuming Episerver has multiple implementations of IInterface within the Registry DSL. We’re not interested in ejecting them all, we have one implementation in particular we would like to remove, for us to replace with our own.
By using FirstOrDefault we locate the right instance reference to remove – e.g. matched against the actual implementation rather than the plugin. Afterwards we can use that exact instance reference to eject the registration, for us to replace within the registry via a standard use of Add().

Whenever you’re tempted to use something like this, you should always consider if use of Interceptors or Decorators can help you achieve the same result. Sometimes it is though not enough!

Leave a Comment

Your email address will not be published. Required fields are marked *

Please type the characters of this captcha image in the input box

Please type the characters of this captcha image in the input box
You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>