Podczas korzystania z CRM możecie natrafić naprawdę na kilka miejsc gdzie aż prosi się o rozszrzenia – konwersja OptionSetValue, EntityReference czy innych tego typu rzeczy. Po prostu aż szkoda tych zbędnych znaków w edytorze :)

W tym odcinku jedna z metod, którą zacząłem ostatnio używać notorycznie. Umożliwia ona konwersje EntityReference do Guid? i na odwrót.

Kod rozszerzeń:

using System;
using Microsoft.Xrm.Sdk;

namespace Gutek.Utils.Crm
{
    public static class EntityReferenceExtensions
    {
        public static Guid? ToId(this EntityReference @this)
        {
            if(@this == null)
            {
                return null;
            }

            return @this.Id;
        }

        public static EntityReference ToEntityReference<TEntity>(this Guid? @this)
            where TEntity : Entity, new()
        {
            if(@this == null)
            {
                return null;
            }

            var name = new TEntity().LogicalName;

            return new EntityReference(name, @this.Value);
        }

        public static EntityReference ToEntityReference<TEntity>(this Guid @this)
            where TEntity : Entity, new()
        {
            // IsEmpty -> Guid extension @this == Guid.Empty
            if(@this.IsEmpty())
            {
                return null;
            }

            var name = new TEntity().LogicalName;

            return new EntityReference(name, @this);
        }
    }
}

Kod testów:

using System;
using Some.TypedEntities;
using Gutek.Utils.Crm
using Microsoft.Xrm.Sdk;
using Xunit;

namespace Gutek.Utils.Tests.Crm
{
    public class entity_reference_extensions_tests
    {
        protected Guid? _nullGuid = null;
        protected Guid? _valueGuid = Guid.Parse("E47A16D8-B4C2-44BB-896D-4A6702B3198F");
        protected Guid _emptyGuid = Guid.Empty;
        protected Guid _notEmptyGuid = Guid.Parse("E47A16D8-B4C2-44BB-896D-4A6702B3198F");

        protected EntityReference _nullReference = null;
        protected EntityReference _valueReference = new EntityReference(Guid.New().ToStrong(), Guid.New());
    }

    public class to_id_tests : entity_reference_extensions_tests
    {
        [Fact]
        public void should_return_null_guid_if_entity_is_null()
        {
            var result = _nullReference.ToId();

            Assert.Null(result);
        }

        [Fact]
        public void should_return_guid_if_entity_is_not_null()
        {
            var result = _valueReference.ToId();

            Assert.NotNull(result);
            Assert.Equal(_valueReference.Id, result.Value);
        }
    }

    public class to_entity_reference_tests : entity_reference_extensions_tests
    {
        [Fact]
        public void should_return_null_ref_if_guid_is_null()
        {
            var result = _nullGuid.ToEntityReference<Account>();

            Assert.Null(result);
        }

        [Fact]
        public void should_return_null_ref_if_guid_is_empty()
        {
            var result = _emptyGuid.ToEntityReference<Account>();

            Assert.Null(result);
        }

        [Fact]
        public void should_return_ref_with_id_and_proper_logical_name_from_not_null_guid()
        {
            var result = _valueGuid.ToEntityReference<Account>();

            Assert.NotNull(result);
            Assert.Equal(_valueGuid.Value, result.Id);
            Assert.Equal(Account.EntityLogicalName, result.LogicalName);
        }

        [Fact]
        public void should_return_ref_with_id_and_proper_logical_name_from_not_empty_guid()
        {
            var result = _notEmptyGuid.ToEntityReference<Account>();

            Assert.NotNull(result);
            Assert.Equal(_valueGuid.Value, result.Id);
            Assert.Equal(Account.EntityLogicalName, result.LogicalName);
        }
    }
}