Copied to clipboard

Flag this post as spam?

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


  • Murray Roke 503 posts 967 karma points c-trib
    May 17, 2022 @ 05:05
    Murray Roke
    0

    How do I inject dependencies into a ModelsBuilder Partial?

    I would like to use some dependencies in a ModelsBuilder partial class. How do I do that?

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    May 17, 2022 @ 08:38
    Dave Woestenborghs
    0

    Hi Murray,

    That is not possible. The models generated have a constructor that can't be changed.

    See the documentation : https://our.umbraco.com/Documentation/Reference/Templating/Modelsbuilder/Understand-And-Extend#extending

    There is a new modelsbuilder package released not so long ago : https://our.umbraco.com/packages/developer-tools/limbo-models-builder/

    Maybe this has the option to do this. I did not check it out yet.

    Dave

  • Murray Roke 503 posts 967 karma points c-trib
    May 17, 2022 @ 21:54
    Murray Roke
    0

    That article pointed out some problems that could arise because models are cached. So perhaps I will do something else, a controller seems heavy-handed for my situation, so ended up just putting more in the top of the view. Perhaps I should look at react I hear that combines all your view and logic together, and I should just embrace the idea.

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    May 17, 2022 @ 08:40
    Dave Woestenborghs
    1

    Hi Murray,

    I just checked out the documentation of that package. It's possible to tweak the models generation with notification handlers.

    https://github.com/limbo-works/Limbo.Umbraco.ModelsBuilder#getmodelsnotification

    Maybe that will work for you.

    Dave

  • Murray Roke 503 posts 967 karma points c-trib
    May 17, 2022 @ 21:07
    Murray Roke
    0

    Thanks for the suggestion Dave, Currently, I am injecting the dependency into the view and passing it as a parameter to the relevant method on the ModelsBuilder Class. It's a little clunky, but I will stick with that rather than add an additional dependency.

    Thanks Again.
    Murray.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies