XrmContext

using FakeXrmEasy;
using FakeXrmEasy.Extensions;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Metadata;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace NAMESPACE
{
    public class XrmContext
    {
        public Dictionary<string, object> Bag { get; set; }
        public Dictionary<string, Entity> LastEntity { get; set; }

        public XrmFakedContext FakeXrmContext { get; set; }
        IOrganizationService service { get; set; }

        public XrmContext()
        {
            Bag = new Dictionary<string, object>();
            LastEntity = new Dictionary<string, Entity>();
            var mockCRM = new MockCRM();
            FakeXrmContext = mockCRM.xrmContext;

            service = FakeXrmContext.GetOrganizationService();
        }

        public void AddEntityToMockCRM(string known, Entity entity)
        {
            if (!LastEntity.ContainsKey(entity.LogicalName)) { LastEntity.Add(entity.LogicalName, null); }
            LastEntity[entity.LogicalName] = entity;
            Guid id = service.Create(entity);
            Entity entityCreated = service.Retrieve(entity.LogicalName, id, new Microsoft.Xrm.Sdk.Query.ColumnSet(true));
            Bag.Add(known, entityCreated);

        }

        public Entity RetrieveEntityInMockCRM(string key)
        {
            return (Entity)Bag[key];
        }

        public Entity RetrieveRefreshedEntityInMockCRM(string key)
        {
            Entity entityInBag = (Entity)Bag[key];
            Entity toBeRetrieved = new Entity();
            try
            {
                toBeRetrieved = service.Retrieve(entityInBag.LogicalName, entityInBag.Id, new Microsoft.Xrm.Sdk.Query.ColumnSet(true));
            }
            catch (Exception)
            {
                throw;
            }
            return toBeRetrieved;

        }

        public Entity UpdateEntityInMock(string key, Entity entity)
        {
            Entity entityInBag = (Entity)Bag[key];
            entityInBag = entity;
            try
            {
                service.Update(entityInBag);
            }
            catch (Exception)
            {
                throw;
            }
            return entityInBag;
        }

        public object CreateEntity(string entityname)
        {
            var assembly = Assembly.Load("ENTITIES COMMON UTILITIES ASSEMBLY");
            var typeassembly = assembly.GetExportedTypes().Where(x => x.Name != null && x.Name.ToLower() == entityname.ToLower()).Select(x => x.FullName).FirstOrDefault();
            return assembly.CreateInstance(typeassembly);
        }

        public void AddAttributeToEntity(string key, string value, string entityname, ref Entity entity)
        {
            var parsedEntity = CreateEntity(entityname);
            var attribute = parsedEntity.GetType().GetProperties().Where(x => x.Name != null && x.Name.ToLower() == key).Select(x => x).FirstOrDefault();
            string typeAttribute = string.Empty;
            int optionValue = int.MinValue;
            if (attribute.PropertyType.GenericTypeArguments != null && attribute.PropertyType.GenericTypeArguments.Length > 0)
            {
                if (attribute.PropertyType.GenericTypeArguments.First().Namespace.Contains("Common"))
                {
                    var optionset = CreateEntity(attribute.PropertyType.GenericTypeArguments.First().Name);
                    optionValue = (int)Enum.Parse(optionset.GetType(), optionset.GetType().GetFields().Where(x => x.Name.ToLower() == value.ToLower().Replace(" ", "")).Select(x => x.Name).FirstOrDefault());
                    typeAttribute = optionset.GetType().BaseType.Name;
                }
                else
                {
                    typeAttribute = attribute.PropertyType.GenericTypeArguments.FirstOrDefault().Name;
                }
            }
            else
            {
                typeAttribute = attribute.PropertyType.Name;
            }
            switch (typeAttribute)
            {
                case "EntityReference":
                    var entityInTheBag = (Entity)Bag[value];
                    entity[key] = entityInTheBag.ToEntityReference();
                    break;
                case "Enum":
                    entity[key] = new OptionSetValue(optionValue);
                    break;
                case "DateTime":
                    entity[key] = DateTime.Parse(value);
                    break;
                case "Boolean":
                    entity[key] = bool.Parse(value);
                    break;
                default:
                    entity[key] = value;
                    break;
            }
        }

        public void AddOptionSetMetadataToMock(string entityLogicalName, string attributeSchemaName, string attributeLogicalName,
            string attributeDisplayName,
            List<KeyValuePair<string, int>> localizedLabels, int languageCode = 1033,
            AttributeRequiredLevel requiredLevel = AttributeRequiredLevel.SystemRequired,
            OptionSetType enumType = OptionSetType.Picklist, bool isGlobal = false)
        {
            var metadata = FakeXrmContext.GetEntityMetadataByName(entityLogicalName);

            // Craeate the options
            OptionMetadataCollection optionsCollection = new OptionMetadataCollection();
            foreach (var optMetadata in localizedLabels)
            {
                var label = new Label(optMetadata.Key, languageCode);
                optionsCollection.Add(new OptionMetadata(label, optMetadata.Value));
            }
            var optionsMetadata = new OptionSetMetadata(optionsCollection);
            optionsMetadata.IsGlobal = false;
            optionsMetadata.OptionSetType = enumType;

            metadata.SetAttribute(new PicklistAttributeMetadata
            {
                SchemaName = attributeSchemaName,
                LogicalName = attributeLogicalName,
                DisplayName = new Label(attributeDisplayName, languageCode),
                RequiredLevel = new AttributeRequiredLevelManagedProperty(
                    requiredLevel),
                OptionSet = optionsMetadata
            });

            FakeXrmContext.SetEntityMetadata(metadata);
        }

        public void AddPrimaryAttributeNameMetadataToMock(string entityLogicalName, string attributeLogicalName)
        {
            var metadata = FakeXrmContext.GetEntityMetadataByName(entityLogicalName);

            var nameAttribute = new StringAttributeMetadata()
            {
                LogicalName = attributeLogicalName,
                RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.ApplicationRequired),
            };

            metadata.SetAttributeCollection(new[] { nameAttribute });
            metadata.SetFieldValue("_primaryNameAttribute", attributeLogicalName);
            FakeXrmContext.SetEntityMetadata(metadata);
        }

        public void AddRelationshipNN(string namerelationship, string intersectentityname, string entity1logicalname, string entity2logicalname, string entity1attributename, string entity2attributename)
        {
            FakeXrmContext.AddRelationship(namerelationship, new XrmFakedRelationship
            {
                IntersectEntity = intersectentityname,
                Entity1LogicalName = entity1logicalname,
                Entity1Attribute = entity1attributename,
                Entity2LogicalName = entity2logicalname,
                Entity2Attribute = entity2attributename
            });
        }
    }
}

Join the newsletter