简体   繁体   English

自定义工作流程活动中的依赖注入

[英]Dependency injection in custom workflow activity

I have a standard application that has a workflow. 我有一个具有工作流程的标准应用程序。 The customer can customize the workflow in the designer. 客户可以在设计器中自定义工作流程。 Now we are making some custom activities for a specific customer. 现在,我们正在为特定客户进行一些自定义活动。 The custom activity is communicating against the business layer by a interface. 定制活动通过接口针对业务层进行通信。 How do I give the interface an implementation of that interface? 如何为接口提供该接口的实现?

It is very important that the standard application doesn't know about the interface and implementation of that interface because it is custom code for that specific customer. 标准应用程序不知道该接口和该接口的实现是非常重要的,因为它是该特定客户的自定义代码。 The activity is found by standard workflow so that works already. 该活动是通过标准工作流程找到的,因此已经可以工作了。

I see a lot of information about extensions but I don't really know how it works. 我看到了很多有关扩展的信息,但我真的不知道它是如何工作的。

Custom activity 自订活动

public sealed class GetDealerDetails : CodeActivity
{
    /////// <summary>
    /////// The dealer controller with all the businesslogic.
    /////// </summary>
    ////private readonly IDealerController _dealerController;

    [Inject]
    public IDealerController DealerController { private get; set; }

    ////public GetDealerDetails()
    ////{

    ////}

    ////[Inject]
    ////public GetDealerDetails(IDealerController dealerController)
    ////{            
    ////    _dealerController = dealerController;
    ////}

    protected override void Execute(CodeActivityContext context)
    {
        Dealer dealer = DealerController.GetDealerDetails(5);
    }
}

I use Ninject in my standard application. 我在标准应用程序中使用Ninject。 I tried to use constructor injection and property injection but it doesn't work. 我尝试使用构造函数注入和属性注入,但是它不起作用。 The DealerController stays null . DealerController保持为null

Edit The rest of the code can be found here: Inject custom code in standard application 编辑其余代码可在此处找到: 在标准应用程序中注入自定义代码

Import is that you have to use the workflow application wrapper I provide with ninject. 导入是您必须使用我随ninject提供的工作流应用程序包装器。 Only with that I can build up the activities. 只有这样,我才能开展活动。 The trick is the following: you cannot use constructor injection with custom workflow activities. 诀窍如下:不能将构造函数注入与自定义工作流程活动一起使用。 Activities are very special in WF. WF中的活动非常特殊。 Usually when you have coded workflow you build them up we the new operator in a lambda expression which is then lazy executed. 通常,当您对工作流程进行编码后,您便会在lambda表达式中构建新的运算符,然后对其进行延迟执行。 So my ninject extension can only work its magic when the activities are already built up. 因此,我的ninject扩展只能在活动已经建立时发挥其魔力。 Therefore you need to pass in the root activities of your activity tree in the workflow application of ninject. 因此,您需要在ninject的工作流应用程序中传递活动树的根活动。 This does then internally resolve the whole activity tree und injects all properties which are decorated with the inject attribute. 然后,这确实在内部解析了整个活动树,并注入了所有用inject属性修饰的属性。

But your actual problem was a bug in the library which I have fixed now. 但是您的实际问题是我现在已修复的库中的错误。 The BookmarkInfo decorator assumed that the scope info is always set and this wasn't the case. BookmarkInfo装饰器假定始终设置范围信息,而事实并非如此。

Extensions are what the framework provides for injection in workflows. 扩展是框架为工作流中的注入提供的功能。 When you execute the workflow you add all dependencies that you will use in your activities. 执行工作流程时,添加将在活动中使用的所有依赖项。

[Dependency]
public IMyService MyService{ get; set; }

WorkflowApplication instance = new WorkflowApplication(myWorkflow, inParameters);
instance.Extensions.Add(MyService);
instance.Run();

And then you can get the extension in your activity in order to use it. 然后,您可以在活动中获取扩展名才能使用它。

protected override void Execute(NativeActivityContext context){    
    var myservice = context.GetExtension<IMyService>();
    myservice.MyMethod();
    }

I hope it helps. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM