November Happy Hour will be moved to Thursday December 5th.

Structuremap v2 vs v3 syntax

Vote:
 

Hi there,

In structuremap v2 we could do this:

container.IfTypeMatches(type => type == typeof(PageRepositoryDescriptor)).InterceptWith(i => new PageNodeTooltipDescriptor());

However, things moved around in v3, so I had to rewrite that expression. I figured I could do this:

container.For().Use(new PageNodeTooltipDescriptor());

However, I can't seem to inject my custom class by doing this. If I set a break point on my overridden property within PageNodeTooltipDescriptor, it'll never hit that break point.

Any advice?

Thanks!

#152250
Aug 22, 2016 15:05
Vote:
 

Hi,

When I look at the quick start of StructureMap shouldn't the right code be like this?:

container.For<PageRepositoryDescriptor>().Use<PageNodeTooltipDescriptor>();

Maybe you've already tried.

Jan Kees

Reference: http://structuremap.github.io/registration/

#152253
Aug 22, 2016 15:58
Vote:
 

Looks like you are using it as a decorator in the first example? For structuremap you can then use the decorateallwith extension to add a decorator type of class on top of the current instance.

x.For<IDevice>().DecorateAllWith<DecoratorIWantInFront>();
#152266
Edited, Aug 22, 2016 22:23
Vote:
 

Thanks - I've already tried both suggestions actually;

            container.For<PageRepositoryDescriptor>().DecorateAllWith<PageNodeTooltipDescriptor>();

            container.For<PageRepositoryDescriptor>().Use<PageNodeTooltipDescriptor>();
#152275
Aug 23, 2016 8:33
Vote:
 

...and if you only use the top line?

container.For<PageRepositoryDescriptor>().DecorateAllWith<PageNodeTooltipDescriptor>();

#152283
Aug 23, 2016 10:24
Vote:
 

Sorry, I meant that I've tried both lines individually, so yes I've tried only that line as well :-)

#152286
Aug 23, 2016 10:39
Vote:
 

The class PageRepositoryDescriptor is registered in IOC container for abstraction IContentRepositoryDescriptor. One approach is to use the Intercept feature that was introduced in 9.10 (see http://world.episerver.com/documentation/Items/Developers-Guide/Episerver-CMS/9/Initialization/dependency-injection/). There in your factory delegate where you will get in an instance of IContentRepositoryDescriptor you could see if it is an instance of PageRepositoryDescriptor in which case you can return your decorator otherwise you return the default service. Something like:

 [ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
    public class InterceptModule : IConfigurableModule
    {
        public void ConfigureContainer(ServiceConfigurationContext context)
        {
            context.Services.Intercept<IContentRepositoryDescriptor>((locator, defaultService) =>
            {
                var pageRepositoryDescriptor = defaultService as PageRepositoryDescriptor;
                return pageRepositoryDescriptor != null ?
                    new MyPageRepositoryDescriptor(pageRepositoryDescriptor) :
                    defaultService;
            });
        }

        public void Initialize(InitializationEngine context)
        {}

        public void Uninitialize(InitializationEngine context)
        {}
    }
#152289
Aug 23, 2016 11:19
Vote:
 

That did it. Thanks!

#152451
Aug 24, 2016 15:05
Vote:
 

So to fully understand, the instance of the PageRepositoryDescriptor class is already registered by EPiServer on startup so you need to intercept it from the repository?

#152566
Aug 25, 2016 13:25
Vote:
 

Yes in this case he wants to change the behaviour of the default type PageRepositoryDescriptor (that is registered in IOC container).

Intercept is designed as a decorator pattern where you can register a new type in the container where the new type has access to the default/old type so it can delgates calls back to the default implementations.

#152610
Aug 26, 2016 10:22
Vote:
 

I blogged a little bit about decorator pattern here if that helps...

http://world.episerver.com/blogs/Daniel-Ovaska/Dates/2013/4/EPiServer-architecture-and-cross-cutting-concerns/

It's mainly used to tweak the original implementation a little...
It's similar to inheriting the original class but has a few pros since you can stack multiple decorators in any order you wish. The con is that you are limited to use the same interface so you can't add new methods. I mainly use it for solving cross cutting concerns like logging, caching etc. 

#152613
Aug 26, 2016 10:52
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.