When we work on CRM plugins, there is a key decision at the beginning of the development cycle when it comes to structuring your plugin code. There are two main ways of writing your plugins : by events or by functionality. In this post, I will describe the two approaches and discuss some of their pros and cons.
Plugins can be described as handlers for events fired by Microsoft Dynamics CRM (source). There can be multiple handlers for single events. Let’s use an example for the purpose of this post.
For the event, we’ll use the creation of a case (pre operation). We will consider that when the case is created, there are two actions that need to be performed : generate and assign a unique case number and validate that required fields have authorized values in them.
Plugin structured based on Events
For the plugin structure based on event, it means we’ll have a single plugin registered on the Case Pre create event, and inside the plugin class we’ll have two business logic blocks of code to execute (one to generate the unique number, the validate the required fields).
Plugin structured based on Functionality
In this case, we’ll have two plugin classes registered on the same event (again, one plugin for unique number generation and another to validate the required fields) – Sorry, no screen shot here but you get the idea .
Pros and Cons
Functionality Based Plugins
- Ease to see what plugins does what when looking at Plugin Registration tool (or similar tools) because of more descriptive feature name in plugin name
- Ability to disable specific features for various scenario (e.g. disable system integration or auto calculation plugins during data migration)
- Unit Tests can be written against a plugin class to test one functionality
- Provides the ability to easily write generic plugins and register on multiple events by any power user
- Large amount of plugin classes
- Large amount of registered plugin steps; can become big and hard to read/maintain
Event Based Plugins
- Plugin registration is straight forward and easy to maintain
- Ease to easily the operations performed at each event when looking at the code
- Plugin classes are standardized : always tied to an event and single point of entry for all developer to see actions executed on the event
- Unit tests can be written against specific Business logic methods and/or classes (even independently from the plugin execution context depending on how they are written)
- Must use alternate mechanism to disable specific features if needed (e.g. configuration data element)
- Possible timeout if executing multiple large business logic blocs (mind you, if you have a timeout in a plugin – which happens after two minutes, you probably have a design issue)
- General dependency to having a developer around
- Generic business logic code blocks can be written but a developer is required to insert the business logic on additional events
- To view the all operations performed for an event, a developer needs to look at the code
So which model should you use?
I have used both models heavily on various occasions. For me personally, the decision on which one to use comes down to a project’s needs and a coding style preference. As an example, if you know you’ll have a lot of generic features and want to have reusable plugins, you are better off using the functionality based model. The same applies if you know there are processes that require to disable specific functionality on a regular basic. On the other hand, if you project is relatively small and includes simple business logic on a few events, the event based approach can be a good choice. Feel free to add feedback in the comment section if you see other pros and cons for each of these models or want to share experience in the area.