Copied to clipboard

Flag this post as spam?

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


  • RM 35 posts 97 karma points
    Aug 07, 2019 @ 22:03
    RM
    0

    WorkflowType Dependency Injection (DI)

    Hi all,

    I have a custom workflow, which requires a set of external dependencies. and i am having trouble injecting them.

    I tried to update the constructor but that leads to a boot error and i can't get past it. Also tried to property inject (i know not ideal) but my properties are always null.

    Is there way to do this via a constructor? if not, how do i get property injection to work with the Umbraco DI wrapper around LightInject which seems to do it out of the box?

    Thanks,

    Rob

  • Nathan Woulfe 447 posts 1665 karma points MVP 5x hq c-trib
    Aug 08, 2019 @ 06:01
    Nathan Woulfe
    0

    Hi Rob

    Have you looked at the Composer/Component pattern? There's a solid set of docs at https://our.umbraco.com/documentation/Implementation/Composing/

    Essentially what you need to do is create a component to register your dependencies (or any other code that needs to run on startup), then register that component in a collection, which is implemented by a composer. Probably over-simplifying the explanation, but that's the guts of it.

    Key part from the docs:

    namespace My.Website
    {
        public class SubscribeToContentServiceSavingComposer : IUserComposer
        {
            public void Compose(Composition composition) {
                Composition.Components().Append<SubscribeToContentServiceSavingComponent>();
            }
        }
    
        public class SubscribeToContentServiceSavingComponent : IComponent
        {
            // initialize: runs once when Umbraco starts
            public void Initialize()
            {
                ContentService.Saving += ContentService_Saving;
            }
    
            // terminate: runs once when Umbraco stops
            public void Terminate()
            {
            }
    
            private void ContentService_Saving(IContentService sender, ContentSavingEventArgs e)
            {
                // do something when content saves
            }
        }
    } 
    
  • RM 35 posts 97 karma points
    Aug 08, 2019 @ 08:26
    RM
    0

    Hi Nathan,

    Does something like this work with a Umbraco Forms workflow as that is what i am dealing with, if so does anyone know where i could find any examples to take a look at as well as the docs?

    Also, i have a bunch of dependencies, would i have to create a component for each one, i was hoping this was simple case of me not getting the Umbraco composer wrapper around light inject which has property injection out of the box.

  • Nathan Woulfe 447 posts 1665 karma points MVP 5x hq c-trib
    Aug 12, 2019 @ 03:57
    Nathan Woulfe
    0

    Not sure about Forms sorry... Not being OSS, Forms can be tricky to work with since there's no repo to poke through...

    Re multiple dependencies, these can be registered in a single component, or you could create multiple components to logically group the dependencies. As far as I know, either option is fine.

  • RM 35 posts 97 karma points
    Aug 08, 2019 @ 12:47
    RM
    0

    I am not sure a component would work here. I mean the underlying theory works but there doesn't seem to be a function to hook into, the RecordService no longer seems to exist from samples i have found.

    I am trying to hijack the On Approve event of the form, so when a record is approved some other processing works.

    The custom workflow works, but i have to hard code all dependencies and i also need the UmbracoHelper so i used the IUmbracoContextFactory in the dependendency that needed it, but it becomes pretty useless if i can't inject.

    LightInject states it has this out of the box (property injection) not sure why the Umbraco wrapper cripples this or its not well documented.

    Any help would be greatly appreciated.

  • Tony 105 posts 163 karma points
    Nov 05, 2019 @ 10:55
    Tony
    0

    Did you find a way to inject into a workflow? I'm facing the same issue and at the moment I'm having to use the AutoFac dependency resolver to get the service which isn't ideal.

Please Sign in or register to post replies

Write your reply to:

Draft