简体   繁体   English

Workflow Foundation 4中的依赖注入/ IoC

[英]Dependency injection / IoC in Workflow Foundation 4

Is it possible to use DI in your workflow activities? 是否可以在工作流程活动中使用DI? and if yes, how? 如果是,怎么样?

For example if you have an activity like 例如,如果您有类似的活动

public sealed class MyActivity : CodeActivity
{
    public MyClass Dependency { get; set; }

    protected override void Execute(CodeActivityContext context)
    {
        Dependency.DoSomething();
    }
}

how can i set Dependency ? 我该如何设置Dependency

(I'm using Spring.Net) (我正在使用Spring.Net)

Workflow doesn't use an IOC container. 工作流程不使用IOC容器。 It uses the ServiceLocator pattern where you add dependencies to the workflow runtime as extensions and workflow activities and retrieve these services from the workflow extensions through the context. 它使用ServiceLocator模式,您可以将依赖项添加到工作流运行时作为扩展和工作流活动,并通过上下文从工作流扩展中检索这些服务。

A ServiceLocator and IOC pattern are similar and have the same purpose in decoupling dependencies. ServiceLocator和IOC模式类似,在解耦依赖关系时具有相同的目的。 The apporach is different though in an IOC container pushing dependencies in while a ServiceLocator is used to pull dependencies out. 虽然在一个IOC容器中推送依赖关系,而ServiceLocator用于拉出依赖关系,但apporach是不同的。

Example activity: 活动示例:

public class MyBookmarkedActivity : NativeActivity
{
    protected override void CacheMetadata(NativeActivityMetadata metadata)
    {
        base.CacheMetadata(metadata);
        metadata.AddDefaultExtensionProvider<MyExtension>(() => new MyExtension());
    }

    protected override void Execute(NativeActivityContext context)
    {
        var extension = context.GetExtension<MyExtension>();
        extension.DoSomething();

    }
}

The MyExtension class is the extension here and it has no base class or interface requirements. MyExtension类是此处的扩展,它没有基类或接口要求。

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

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