Is it possible to set a default `Fallback` for `co...
# help-with-umbraco
l
I'm wondering if there's a way to define a default
Fallback
when I make calls to the
content.Value("propertyAlias")
instead of having to write
content.Value("propertyAlias", fallback: Fallback.ToDefaultLanguage)
every single time? Of course, I could write a custom extension method to handle this, but wondering if it's possible out-of-the-box?
d
There is this device called
IPublishedValueFallback
. You can inject it with dependency injection and you pass it into the constructor of your content type. You can add a decorator onto that type and see if that works. Other than that, I haven't seen any options.
l
Thanks Dennis. Could you clarify what you mean by "add a decorator onto that type"? Do you mean replacing the DI
IPublishedValueFallback
instance with my own?
d
One of two options. You can install Scrutor and then decorate the instance everywhere
Copy code
csharp
public class MyComposer : IComposer
{
    public void Composer(IUmbracoBuilder builder)
    {
        builder.Services.Decorate<IPublishedValueFallback, DecoratorPublishedFallbackWithDefault>();
    }
}

public class DecoratorPublishedFallbackWithDefault : IPublishedValueFallback
{
    private readonly IPublishedValueFallback _decoratee;

    public DecoratorPublishedFallbackWithDefault(IPublishedValueFallback decoratee)
    {
        _decoratee = decoratee;
    }

    // implement interface through _decoratee, but make changes as necessary (I can't remember the interface methods)
}
Or you can decorate it in a more localized way:
Copy code
csharp
public class MyContentController : RenderController
{
    private readonly IPublishedValueFallback _fallback;

    public MyContentController(IPublishedValueFallback fallback)
    {
        _fallback = fallback;
    }

    public IActionResult Index()
    {
        var content = new MyContent(CurrentPage, new DecoratorPublishedFallbackWithDefault(_fallback));
        return CurrentTemplate(content);
    }

    private sealed class DecoratorPublishedFallbackWithDefault : IPublishedValueFallback
    {
        private readonly IPublishedValueFallback _decoratee;

        public DecoratorPublishedFallbackWithDefault(IPublishedValueFallback decoratee)
        {
            _decoratee = decoratee
        }

        // implement interface through _decoratee, but make changes as necessary (I can't remember the interface methods)
    }
}
l
Ah okay, gotcha 👍 Thank you for the clarification and code snippets. 🙏