web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

CRM Plugin - Initialize another class in Plugin Class Constructor. will there be any issue?

(0) ShareShare
ReportReport
Posted on by

Hi,

I am trying to write plugins which should be unit testable. I have divided the plugin into 2 parts as below.

1. Plugin class - class which will get register as plugin on particular event and will have validations whether plugin should execute or not.

2. Business logic class - which contains business logic as per requirement.

I have read articles mentioning that plugin class should not have class level variables. Do you see any problem with sample code snippet of plugin class (see below) as I have created 2 constructor in plugin class. One for CRM and other to use it for unit testing?

Sample:

public class Followup: IPlugin
  {
      readonly IFollowupBusinesslogic businessLogic;
 
      //Constructor for CRM
      public Followup()
      {
          businessLogic = new FollowupBusinesslogic();
      }
 
      //Constructor for unit testing
      public Followup(IFollowupBusinesslogic businessLogic)            
      {
          this.businessLogic = businessLogic;
      }
 
      public void Execute(IServiceProvider serviceProvider)
      {
         // code....
      }
  }

Thanks in advance!!!

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    tw0sh3ds Profile Picture
    5,600 on at

    So you have read "articles mentioning that plugin class should not have class level variables", yet you decided to create a field in your plugin class... The reason why you should not do that is that this instance of this plugin will be shared between different pipeline instances, so in this case different calls can use the same reference for your IFollowupBusinessLogic. Unless this class is stateless (which I doubt and also - you can never be sure that the next developer in your project will know that it should be stateless) this is likely going to blow off in the most unexpected moment (and probably on production, not on test environment). Believe me, that is really bad idea, so simply remove that field from Plugin class. The only place when you can instantiate your BusinessLogic class is Execute method of your plugin.

    Having said that, two constructors should work, but if you have stateless plugin, it will make no sense to have them both. The way I do it is to have some kind of handler class which

    a) Checks if the logic should be fired

    b) Fires the logic

    And the only thing I do in plugin class is to instantiate this Handler class. So I don't even write any unit test for that, because it would make totally no sense. I have total control over my Handler class, so I can write unit test for it (it can implement interface, have as many constructors as I want etc.).

  • Community Member Profile Picture
    on at

    Hi Pawel,

    Thanks for good explanation.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
ScottDurow Profile Picture

ScottDurow 2

#2
GJones Profile Picture

GJones 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans