Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jacob Buus 5 posts 75 karma points
    Jul 18, 2022 @ 23:20
    Jacob Buus
    0

    Best practice for dependency injection use in base model

    Hi guys,

    I'm upgrading an Umbraco 8 solution to Umbraco 10. The old solution has a base class for all content type models. This base class accesses stuff like HttpContext. The solution also has a static AppSettings class which makes call to ConfigurationManager etc. to make these available to the different Templates in the solution.

    I'm trying to figure out the best way to do this in Umbraco 10. I really don't like the static stuff but would like to make use of the Dependency Injector. I'm looking into extending UmbracoViewPage and then having all of my templates inherit this.

    But if I do stuff like this:

    public class CustomViewPage<T> : UmbracoViewPage<T>
        {
            private readonly IOptions<ApplicationConfiguration> _applicationConfiguration;
    
            public ContiniaViewPage(IOptions<ApplicationConfiguration> applicationConfiguration)
            {
                _applicationConfiguration = applicationConfiguration;
            }
    
            public bool IsDebuggingEnabled => _applicationConfiguration.Value.IsDebuggingEnabled;
    

    Then I get an error when trying to use it in my template:

    @inherits CustomViewPage<BasePage>
    

    saying:

    CustomViewPage does not contain a parameterless constructor.

    So I'm guessing this might not be the correct way to implement base functionality - seeing as how I can't use dependency injection with my own custom ViewPage. Could someone help me out with how to accomplish this in Umbraco 10?

    Thanks a lot :)

Please Sign in or register to post replies

Write your reply to:

Draft