Why the plugins implement IPlugin interface and Workflows (custom) inherits the Code Activity abstract class

Someone asked me this question which is frequently asked in interviews i.e. why plugins implemented iplugin i.e. an interface and custom workflow inherit codeactivity i.e. an abstract class?

The purpose of question is to know why codeactivty is an abstract class and not an interface like iplugin?

So to understand the answer we fist need to understand the difference b/w interface and an abstract class:

What is an Abstract Class?

An abstract class is a special kind of class that cannot be instantiated. So the question is why we need a class that cannot be instantiated? An abstract class is only to be sub-classed (inherited from). In other words, it only allows other classes to inherit from it but cannot be instantiated. The advantage is that it enforces certain hierarchies for all the subclasses. In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards.

What is an Interface?

An interface is not a class. It is an entity that is defined by the word Interface. An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body. As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class. Since C# doesn’t support multiple inheritance, interfaces are used to implement multiple inheritance.

 

Enough theory?, coming to the main point, the main difference b/w interface and abstract class is that it is mandatory to implement all the methods of an interface while in case of abstract class, it’s your choice which method you implement and which you don’t.

Ref: https://www.codeproject.com/Questions/195703/is-compulsory-to-implement-all-the-methods-in-inte

So it means if we implement interface, we have to implement all of it’s methods.

Now coming to the structure of Iplugin interface and Codeactivity abstract class:

IPlugin Interface:

It only have one method i.e. Execute which we implement in our plugin code.

Ref: https://msdn.microsoft.com/en-us/library/microsoft.xrm.sdk.iplugin.aspx

CodeActivity Abstract Class:

If you look closely the structure of CodeActivity abstract class, you’ll find that it have 35 different methods.Check below link for details.

Ref: https://msdn.microsoft.com/en-us/library/system.workflow.activities.codeactivity(v=vs.110).aspx

Conclusion:

As CodeActivity have 35 methods, it seems logical to make it abstract class so that developer have flexibility to implement the method/methods on his choice/requirement in the class inheriting CodeActivity.

If CodeActivity was an interface, it become mandatory to implement all the 35 methods in class implementing interface which is illogical.

That’s why Microsoft made CodeActivity as abstract class while Iplugin as Interface.

3 thoughts on “Why the plugins implement IPlugin interface and Workflows (custom) inherits the Code Activity abstract class

Add yours

  1. “If CodeActivity was an interface, it become mandatory to implement all the 35 methods in class implementing interface which is illogical.”

    This reason doesn’t justify to me, coz if the abstract class can have both abstract and concrete methods, if some method is abstract then the child class has to implement it. Main use of the abstract class is to provide some default or common functionality to child classes(Which is leveraged in Template method design pattern). So CodeActivity class has only one abstract method which is Execute method so child class has to implement it. Other methods than abstract, I believe helps this class to implement long running robust processes (unlike plugins), because namespace System.Activities has it’s wires connected to Windows Workflow Foundation.

    Further discussions welcome, let’s beat this evergreen interview question.

    Experts, please correct me if my views are far from reality.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: