开发者

Dependency injection / IoC in Workflow Foundation 4

开发者 https://www.devze.com 2023-01-17 23:06 出处:网络
Is it possible to use DI in your workflow activities? and if yes, how? For example if you have an activi开发者_如何学JAVAty like

Is it possible to use DI in your workflow activities? and if yes, how?

For example if you have an activi开发者_如何学JAVAty like

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

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

how can i set Dependency?

(I'm using Spring.Net)


Workflow doesn't use an IOC container. 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.

A ServiceLocator and IOC pattern are similar and have the same purpose in decoupling dependencies. The apporach is different though in an IOC container pushing dependencies in while a ServiceLocator is used to pull dependencies out.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消