Classes and interfaces

In this section, each class and interface of the concept will be explained in details.

Kipon.Xrm.IfAttribute

The [If(typeof(conditionevaluator))] can be used as decorator on plugin step method, ex. OnPreCreate, OnPreUpdate etc. The decorator allow you to implement a simple class that validates if the plugin method should be called at all.


namespace Kipon.PluginExample.Plugins.Account
{
    public class RevenueChangedPlugin : Kipon.Xrm.BasePlugin
    {
        [Kipon.Xrm.Attributes.If(typeof(Filters.Accounts.NewRevenueLargerThanOneMill))]
        public void OnPostCreate(Entities.Account.IRevenueChanged target)
        {
            // code to send an email to the director about the good news
        }

        [Kipon.Xrm.Attributes.If(typeof(Filters.Accounts.NewRevenueLargerThanOneMill))]
        public void OnPostUpdate(Entities.Account.IRevenueChanged target)
        {
            // code to send an email to the director about the good news
        }
    }
}

Above method is listening post create and post update on an account, where revenue was changed (IRevenueChanged jummy jummy interface), but the methods are also decorated with the If attribute, binding the if to the class Filters.Accounts.NewRevenueLargerThanOneMill.


using Kipon.Xrm.Attributes;
using Microsoft.Xrm.Sdk;
using Kipon.Xrm.Extensions.Sdk;

namespace Kipon.PluginExample.Filters.Accounts
{
    public class NewRevenueLargerThanOneMill : Kipon.Xrm.IMethodCondition
    {
        public bool Execute(IfAttribute ifAttr, IPluginExecutionContext ctx)
        {
            var revenue = ((Microsoft.Xrm.Sdk.Entity)ctx.InputParameters["Target"])
                .Attributes.ValueOf<Microsoft.Xrm.Sdk.Money>(nameof(Entities.Account.Revenue));

            if (revenue != null && revenue.Value > 1000000)
            {
                return true;
            }
            return false;
        }
    }
}

The filter class is implementing Kipon.Xrm.IMethodCondition, and the Execute implementation of this method is returning true only if the revenue property was assign a value not null, and above 1000000.

The result of using above design is, that the PostCreate and PostUpdate method are only called in situations where it is relevant, and the code within the plugin becomes more simple. On top of that, that condition becomes a reusable component itself.

© Kipon ApS 2020, 2021, 2022, 2023. All content on the page is the property of Kipon ApS. Any republish or copy of this content is a violation. The content of this site is NOT open source, and cannot be copied, republished or used in any context without explcit permission from the owner.