Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

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

Posted on by Microsoft Employee

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

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: CRM Plugin - Initialize another class in Plugin Class Constructor. will there be any issue?

    Hi Pawel,

    Thanks for good explanation.

  • Suggested answer
    tw0sh3ds Profile Picture
    tw0sh3ds 5,600 on at
    RE: CRM Plugin - Initialize another class in Plugin Class Constructor. will there be any issue?

    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.).

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans