Skip to content

Instantly share code, notes, and snippets.

@TeaDrivenDev
Last active January 4, 2016 10:09
Show Gist options
  • Save TeaDrivenDev/8606480 to your computer and use it in GitHub Desktop.
Save TeaDrivenDev/8606480 to your computer and use it in GitHub Desktop.
Comparison of a small MSCRM plugin implemented in C# and F#

As requested by several people on Twitter after I mentioned that the C# implementation is 27 lines, and the F# version saves only two lines over this (which is mainly because there isn't much to save at all). In the mean time, I actually managed to shave off two more lines because the last method call incidentally returns void.

Please note that the plugin as such is not very useful; I'm mainly trying to get an F# plugin to run within MSCRM in the first place.

C#:

	using Microsoft.Xrm.Sdk;
	using System;
	
	namespace CSharp
	{
	    public class CSharpPlugin : IPlugin
	    {
	        public void Execute(IServiceProvider serviceProvider)
	        {
	            IOrganizationServiceFactory organizationServiceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
	            IOrganizationService organizationService = organizationServiceFactory.CreateOrganizationService(null);
	            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
	
	            Entity account = context.PreEntityImages["Image"];
	            string oldName = account.GetAttributeValue<string>("name") ?? "";
	
	            Entity record = new Entity("account")
	                            {
	                                Id = context.PrimaryEntityId
	                            };
	
	            record["name"] = oldName + " CSharped";
	
	            organizationService.Update(record);
	        }
	    }
	}

F#:

namespace FSharp
	
	open System
	open Microsoft.Xrm.Sdk
	
	type TestPlugin() =
	    interface IPlugin with
	        member this.Execute serviceProvider =
	            let organizationServiceFactory = serviceProvider.GetService(typeof<IOrganizationServiceFactory>) :?> IOrganizationServiceFactory
	            let organizationService = organizationServiceFactory.CreateOrganizationService(System.Nullable())
	            let context = serviceProvider.GetService(typeof<IPluginExecutionContext>) :?> IPluginExecutionContext
	
	            let account = context.PreEntityImages.["Image"]
	            let name = account.GetAttributeValue<string> "name"
	
	            let oldName = match name with
	                            | null -> ""
	                            | _ -> name
	
	            let record = new Entity("account", Id = context.PrimaryEntityId)
	            record.["name"] <- oldName + " Fsharped"
	
	            organizationService.Update record
@CarstenKoenig
Copy link

ah yes and as mentioned on twitter - you could do:

let createPlugin() = { new IPlugin with
    member this.Execute serviceProvider = ...
    }

of course you get some {} again ;)

@CarstenKoenig
Copy link

and btw: if some function does not return void (in F# we would call this unit - but that's a detail) you can always pipe in ignore (even if this reads a bit strange):

for example many update/delete functions might return bool but you really want unit - so you do

delete stuff |> ignore

or if you like

ignore <| delete stuff

most prefer the first one - but kindof liking Haskells $ I often use the second (same with not - I tend to like not <| something with params over something with params |> not)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment