Click here to Skip to main content
15,867,568 members
Articles / Web Development / ASP.NET

Workflow Manager using Unity config File in ASP.NET

Rate me:
Please Sign up or sign in to vote.
2.00/5 (2 votes)
10 Apr 2018CPOL3 min read 9.4K   53   6   2
This article explains a design approach that involves setting the work flow of the process using config file and Unity Framework. It helps to change the flow only by updating the config file and no other code change is required.

Introduction

This article involves a design pattern that helps to write the code in a way that makes it very easy to change the work flow. Usually, there are different approaches to update the flow dynamically like using database but this article explains the use of Unity Framework and config file. Changing the flow and deploying to production usually involves code change and retesting everything as we deploy either a whole package or just one DLL. But having the flow in the config file and using Unity Framework to implement it gives you the flexibility in your flow without the need of any code change.

Background

Before this article, it would be more beneficial that you refer to Microsoft Unity and How to use Unity in ASP.NET MVC Framework articles to get a better understanding about how Unity works and how can it solve most of our problems making life easier. Also, go over Unity Configuration schema link to know different Unity tags and schema details.

Business Requirement

I had a business requirement where the expectation was to keep the workflow implementation as loosely coupled as possible that there should be minimum/no code change required when we wish to change the workflow. Intention of this approach is to have the setup in such a way that a small change in the config file should reflect the new workflow without needing to make any code change. As deploying a config file is very simple, it gives the opportunity to switch the flow and also rollback to original workflow quickly if something goes wrong.

Using the Code

Before starting with coding, we need to get Unity Framework Package from Nuget Package manager. To grab the package, go to Tools -> NuGet Package Manager -> Manage NuGet Packages for Solution.

Image 1

For the demo, I'm using a simple Console application project type. I have four tasks/steps which implement a single interface ITask.

Image 2

We've registered all these objects using Unity in the config file.

Image 3

Note: I've written the objects and registered it in such a way that we can use the same create new workflows or update. So at some point, I wish to create a new workflow, all I need to do is create a new Registration in Unity Config with corresponding steps and we're ready to roll.

Image 4

Now is the time to see this code in action.

C#
IUnityContainer container = new UnityContainer();
container.LoadConfiguration();

Console.WriteLine("Executing basic workflow...");
IWorkFlowManager workFlowManager = container.Resolve<IWorkFlowManager>("BasicWorkflow");
workFlowManager.Execute();

Console.WriteLine("Executing Advanced workflow...");
workFlowManager = container.Resolve<IWorkFlowManager>("AdvancedWorkflow");
workFlowManager.Execute();

Here, when I resolved my IWorkFlowManager workflow with that is registered by the name "BasicWorkflow" returns the object with the tasks in sequence and then executes it. Here in the example, we have four tasks in the workflow "BasicWorkFlow" and when we execute it, all the four tasks gets executed.

Image 5

If in future, I plan on changing my workflow steps/tasks and no longer need to use TaskB, all I've to do is update the config file and remove it from "BasicWorkFlow" Register tag and boom, your flow is updated. It is as simple as that.

I've another "AdvanceWorkflow" that uses the same steps but different registrations in Unity config. We change the property values and it's a whole new object with the same business logic (can be changed based on our requirement).

Image 6

Points of Interest

The best part of this exercise is that I realized how extensively Unity can be used when we switch to config file implementation. This design pattern can be integrated with any Creational, Structural or Behavioral design patterns. Although it adds a little complexity to the code, once it is mastered, it can help to make the code modular and less coupled. If implemented correctly, you can have your feature toggling just like plug and play.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
I'm an aspiring Developer recently started more and more interested towards ASP.NET and related technologies. I tend to love playing around with different technologies and get more hands on experience by working on different projects. My prime interest lies in Mobile Development(both native and hybrid) and have developed few basic application during my course of studies and work. Always up for a challenge and plan on to finish it through best possible approach.

Comments and Discussions

 
GeneralMy vote of 4 Pin
Thomas Schittli16-Apr-18 2:04
Thomas Schittli16-Apr-18 2:04 
GeneralRe: My vote of 4 Pin
Harshit Gindra24-Apr-18 6:37
professionalHarshit Gindra24-Apr-18 6:37 
Nice to hear that it was useful for you.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.