Is it possible to use DI in your workflow activities? 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
?
(I'm using Spring.Net)
The advantages of using Dependency Injection pattern and Inversion of Control are the following: Reduces class coupling. Increases code reusing. Improves code maintainability.
NET supports the dependency injection (DI) software design pattern, which is a technique for achieving Inversion of Control (IoC) between classes and their dependencies. Dependency injection in . NET is a built-in part of the framework, along with configuration, logging, and the options pattern.
IoC means that one code calls another; DI goes beyond that and implements IoC by using composition. A DI or IoC container needs to instantiate objects (dependencies) and provide them to the application. To do so, it must deal with constructor injection, setter injection, and interface injection.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With