How To Pass Parameters Into An Episever SelectionFactory?

On most projects, there will be a requirement for users to select more options out of a dropdown list or a checkout. In EPiserver this is done via a selection factory, more information can be found here.  

 

Like the good developers we are, when we're building our websites we want to stick to the DRY principle (don't repeat yourself) and we don't want to have to create a new selection factory class everytime that we want to display different data.  THe best way to prevent having to write all this duplicate code is to pass a parameter into the selection factory and then change the data-source of the factory based on the parameter.

How Do I Pass A Parameter Into A Selection Factory?

First, let's look at the code to set a selection factor on a property.

        
        [Display(
            Name = "Name",
            Description = "Name")]
        [SelectMany(SelectionFactoryType = typeof(MySelectionFactory))]
        public virtual string Name { get; set; }

As we pass a type into the selection factory, then it's not quite as simple as passing a string, or enum in via the selection factory. If we look at the selection factory code:

        
        public IEnumerable GetSelections(ExtendedMetadata metadata)
        {
        }

GetSelections() is the method Episerver calls when it tries to get the data. The input parameter is an ExtendedMetadata. If you look in here you can see that it contains a property called Attributes. This is the way that we can pass information from our class/block definitions into the selection factory. We create a custom attribute, declare it on the same property we want to set as a selectionfactory. When the selection factory gets called, we then read the contents of that attribute. So let's dig into some code:

    public class MyCustomAttribute : SelectOneAttribute
    {
        public string ParameterToPassIn { get; set; }
    }

Our property declaration now looks like this:

        
        [Display(Name = "Name",
                 Description = "Name")]
        [SelectOne(SelectionFactoryType = typeof(MySelectionFactory))]
        [MyCustomAttribute(SelectionFactoryEnum = "any value")]
        public virtual string SomethingToPick { get; set; }

Our selection factory code will now look something like this:

		
        public IEnumerable GetSelections(ExtendedMetadata metadata)
        {
            var myAttributes = metadata.Attributes.OfType();

            if (myAttributes == null && !myAttributes.Any())
            	return new List();

            var myAttribute = myAttributes.FirstOrDefault();
            var paramterValue = myAttribute.ParameterToPassIn;

            // Now do whatever with paramterValue
        }

Conclusion

Passing parameters is possible when you use a selection factory, however, the way you need to do it isn't that obvious. When you want to pass in a parameter, all you need to do is via custom attributes, rather than directly through the selection factories constructor. Enjoy!

 

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

Back to top