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
you could refactor:
(please note that it's possible that I got some error in there - did not really check in fsharpi - sorry)