Loading...

I’ve Been Doing Plugin Parameters Wrong For 7 Years

I’ve Been Doing Plugin Parameters Wrong For 7 Years
TLDR: Use request/response classes to get typed parameters in your plugins, and share your knowledge!

I’ve been an active Stack Overflow (SO) user for about 7 years now.  There are 3 ways to increase your knowledge from SO.
  1. Being able to ask a question and get an answer in hours.  It’s not always the correct, or the answer you want, but more often than not, it is a great way to increase your knowledge.  This though, is not the primary conduit to increasing your knowledge, that privilege belongs to way #2.
  2. Finding the answers to the questions that have already been asked, and instantaneously getting the knowledge you want (sometimes by the former, “smarter” you).  This is by far the most common method of learning on SO.  It also brings up a new not quite as beneficial coding paradigms (Stack-Overflow-Copy-and-Paste-Yourself-to-Victory).  If this second option is the bread and butter of learning for developers today, then third option is the dessert, the unexpected delightfulness that makes the SO community a great place to contribute to, and learn from.
  3. Providing an answer to a question that you believe to be 100% correct, but then having someone else respond with an even better/more awesome/even more “correcter” answer, to which you tweet about, and then someone else responds with an even better/more awesome/even more “correcter”/more ludicrous answer.  This third option is the subject of today’s post.
Here is how this particular instance of #3 went down…


The MSDN documented approach to accessing parameters is to use “magic” strings and assumed casting
// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is EntityReference)
{
    // Obtain the target entity from the input parameters.
    EntityReference entity = (EntityReference)context.InputParameters["Target"];
}
Martin’s technique is so simple, I can't believe I haven't seen it used before:
var createReq = new CreateRequest { Parameters = context.InputParameters };
createReq.Target; // Is typed as Entity vs EntityReference (DeleteRequest)
To which I’ve wrapped in an extension method to help remove some additional key strokes (which I’ve added to the DLaB.Xrm.Plugin namespace):
/// <summary>
/// Populates a local version of the request using the parameters from the context.  This exposes (most of) the parameters of that particular request
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="context">The context.</param>
/// <returns></returns>
public static T GetRequestParameters<T>(this IPluginExecutionContext context) where T: OrganizationRequest
{
    var request = Activator.CreateInstance<T>();
    request.Parameters = context.InputParameters;
    return request;
}
 
/// <summary>
/// Populates a local version of the response using the parameters from the context.  This exposes (most of) the parameters of that particular response
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="context">The context.</param>
/// <returns></returns>
public static T GetResponseParameters<T>(this IPluginExecutionContext context) where T : OrganizationResponse
{
    var response = Activator.CreateInstance<T>();
    response.Results = context.OutputParameters;
    return response;
}
So now, this is even shorter:
var parameters = context.GetRequestParameters<CreateRequest>();
parameters.Target;
So my challenge to you dear reader, get involved in your community.  You may never know when an attempt to share your knowledge with someone else, ends up unexpectedly resulting in others sharing their knowledge with you!

Published on:

Learn more
.Net Dust
.Net Dust

NULL

Share post:

Related posts

PowerFx Formatting If Statement Idiosyncrasies

Disclaimer: This entire blog is an opinion, and opinions are like butts, we all have one.  This is not doctrinal truth that must be obser...

1 year ago

Separating Plugin Logic: A Guide to Testing Dataverse Plugins with IOC

I’m not a pure TDD developer.  I frequently take my best guess at a Dataverse plugin, then apply TDD until everything works.  This c...

3 years ago

How to Filter Dates in Canvas Apps Using Greater Than/Less Than Operators

Defining the ProblemRecently I was attempting to filter an on-premise SQL table by a DateTime field using a “greater than” operator, and displ...

3 years ago

Enabling or Disabling All Plugin Steps In Dataverse

If you are facing an issue with a recent bug in PowerPlatform.BuildTools version 0.0.81 that caused your plugin steps to become disabled, fret...

4 years ago

Using AutoFixture To Create Early Bound Entities

AutoFixture is an open source library that is used in testing to create objects without having to explicit set all the values.  I recentl...

4 years ago

Long Functions Are Always A Code Smell

This article is in response to fellow MVP Alex Shelga’s recent article Long functions in dataverse plugins – is it still ” code smell”?. ...

4 years ago

Handle All Your Plugin Exceptions In One Place, And Then Hide It!

In coding, one of the recommended best practices is to avoid creating duplicate code as it leads to maintenance issues. Therefore, it is advis...

6 years ago

Newsletter

Get the latest Dynamics 365 and Power Platform content in your inbox

A curated digest of community blogs, product news, videos, and podcasts — delivered without the noise.

Weekly updates Unsubscribe anytime Fresh community picks
We use your email only for the newsletter and you can unsubscribe at any time.
By subscribing, you agree to the privacy policy.