Reinoud van Dalen

August 28, 2017

Sitecore Pipeline ValueProvider for Mvc

Lumi.Foundation.PipelineValueProvider is a module that helps you speed up Sitecore development for Controller Renderings and allow you to supply Sitecore context type objects to your Controller Actions.

It’s magic

Let me not bore you with the technical details but let’s skip to the results. We’ll slide into details as we go and you can leave the information train whenever you feel like it ;-)

This, is what it’s all about:

undefined

You have your Controller and Action for your Controller Rendering. You have a few parameters and then the SitecorePipelineValueProvider will step in and magically provide these values for you.

As long as you stick to the naming convention

The pipeline valueprovider will kick off these pipelines if your parameters is named to either one of these:

  • Contextitem -> mvc.valueprovider.contextitem
  • Datasource -> mvc.valueprovider.datasource
  • RenderingParameters -> mvc.valueprovider.renderingparameters

The core package is equipped with some standard processors that will resolve the data if the generic Type matches the parameter Type:

  • ResolveContextItem<Item>
  • ResolveDatasourceItem<Item>
  • ResolveRenderingParameters<RenderingParameters>

Adding Glass processors

This is where I think it gets interesting. My end game was to be able to have GlassType objects resolved for me and with this ValueProvider it would be easy to create custom processors for that. Lumi.Foundation.PipelineValueProvider.Glass is another package I’ve created and you can install it on top of the standard. When installed, your controller could then look like:

undefined

The contextItem will still be resolved by the default processor (like in the previous screenshot). But the datasource will not be resolved by the default processor and is handed over to the ResolveGlassDatasourceItem processor which nicely returns the datasource as a mapped Title object.

Oh and by the way, this works really well combined with the Code First Renderings module. It also has an affinity with Glass and uses the same naming conventions. Check it out: http://www.chrisvandesteeg.nl/2015/06/16/sitecore-codefirst-renderings/

Creating your own processors

If you prefer to use another ORM then you can just create add and replace processors to the pipelines. There are some base processors to help you get on your way:

  • ValueProviderProcessor
    • Checks if there already is a Result (if so then it stops processing)
    • Executes the abstract GetResult method
    • Aborts the pipeline if Result is not null
  • ValueProviderProcessor<T>
    • Derrives from ValueProviderProcessor
    • Checks if args.RequestedType is equal to T (if not then it stops processing)

But you don’t need to use either of them. The only thing you are required to use is the ValueProviderArgs. It contains 2 properties that are important:

  • RequestedType (type of the parameter)
  • Result (this will be returned to the action as parameter value)

Get it while it’s hot

Of course, the source and packages are available. Please drop a comment if you like, have questions or anything to add to this.

Github:
https://github.com/RvanDalen/Lumi.Foundation.PipelineValueProvider

Nuget:
https://www.nuget.org/packages/Lumi.Foundation.PipelineValueProvider
https://www.nuget.org/packages/Lumi.Foundation.PipelineValueProvider.Glass

TAGS: sitecore mvc glass pipelines