4
Vote

Introducing a Widget extension method

description

Scenario:

Defining a widget content type in migrations always require the following parts and settings:

.WithPart("CommonPart")
.WithPart("WidgetPart")
.WithSetting("Stereotype", "Widget");

Suggestion:

So it may be nice to have an extension method to help with that:
public static class ContentTypeDefinitionBuilderExtensions {
    public static ContentTypeDefinitionBuilder Widget(this ContentTypeDefinitionBuilder builder) {
        return builder
            .WithPart("CommonPart")
            .WithPart("WidgetPart")
            .WithSetting("Stereotype", "Widget");
    }
}
Then defining a widget in a migration can look like this:

contentDefinitionManager.AlterTypeDefinition("MyWidget", type => type
            .DisplayedAs("My Widget")
            .Widget();
Another idea is this: we have an AlterTypeDefinition() and AlterPartDefinition() method. Wouldn't it be nice if there were also an AlterWidgetDefinition()? Which could also be implemented as an extension method:

public static class ContentDefinitionManagerExtensions {
    public static void AlterWidgetDefinition(
        this IContentDefinitionManager contentDefinitionManager, 
        string name, 
        string displayName, 
        Action<ContentTypeDefinitionBuilder> alteration = null
    ){
        contentDefinitionManager.AlterTypeDefinition(name, type => { type
            .DisplayedAs(displayName)
            .Widget();

            if (alteration != null)
                alteration(type);
        });
    }
}
Note that it uses the .Widget() extension method mentioned earlier, and that it takes a lambda so the user can add more parts and fields and settings:

ContentDefinitionManager.AlterWidgetDefinition("FeaturedProductWidget", "Featured Product Widget", widget => widget.WithPart("BodyPart"));

comments