Dependency injection / IoC in Workflow Foundation 4

和自甴很熟 提交于 2019-11-30 08:37:38

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!