Summary

Class:FakeXrmEasy.Services.DefaultEntityInitializerService
Assembly:FakeXrmEasy
File(s):F:\Git\fake-xrm-easy\FakeXrmEasy.Shared\Services\EntityInitializer\DefaultEntityInitializerService.cs
Covered lines:36
Uncovered lines:3
Coverable lines:39
Total lines:74
Line coverage:92.3%
Branch coverage:100%

Metrics

MethodCyclomatic ComplexitySequence CoverageBranch Coverage
.ctor()1100100
Initialize(...)7100100
Initialize(...)100

File(s)

F:\Git\fake-xrm-easy\FakeXrmEasy.Shared\Services\EntityInitializer\DefaultEntityInitializerService.cs

#LineLine coverage
 1using System;
 2using Microsoft.Xrm.Sdk;
 3using FakeXrmEasy.Extensions;
 4using System.Collections.Generic;
 5using Microsoft.Xrm.Sdk.Query;
 6
 7namespace FakeXrmEasy.Services
 8{
 9    public enum EntityInitializationLevel
 10    {
 11        Default = 0,  //Minimal initialization of common attributes
 12        PerEntity = 1 //More detailed initialization of entities, on an entity per entity basis
 13    }
 14    public class DefaultEntityInitializerService : IEntityInitializerService
 15    {
 16
 17        public Dictionary<string, IEntityInitializerService> InitializerServiceDictionary;
 18
 427019        public DefaultEntityInitializerService()
 427020        {
 427021            InitializerServiceDictionary = new Dictionary<string, IEntityInitializerService>()
 427022            {
 427023                { InvoiceDetailInitializerService.EntityLogicalName, new InvoiceDetailInitializerService() },
 427024                { InvoiceInitializerService.EntityLogicalName, new InvoiceInitializerService() }
 427025            };
 427026        }
 27
 28        public Entity Initialize(Entity e, Guid gCallerId, XrmFakedContext ctx, bool isManyToManyRelationshipEntity = fa
 2848229        {
 30            //Validate primary key for dynamic entities
 2848231            var primaryKey = string.Format("{0}id", e.LogicalName);
 2848232             if (!e.Attributes.ContainsKey(primaryKey))
 2228433            {
 2228434                e[primaryKey] = e.Id;
 2228435            }
 36
 2848237             if (isManyToManyRelationshipEntity)
 7238            {
 7239                return e;
 40            }
 41
 2841042            var CallerId = new EntityReference("systemuser", gCallerId); //Create a new instance by default
 43
 2841044            var now = DateTime.UtcNow;
 45
 2841046            e.SetValueIfEmpty("createdon", now);
 47
 48            //Overriden created on should replace created on
 2841049             if (e.Contains("overriddencreatedon"))
 650            {
 651                e["createdon"] = e["overriddencreatedon"];
 652            }
 53
 2841054            e.SetValueIfEmpty("modifiedon", now);
 2841055            e.SetValueIfEmpty("createdby", CallerId);
 2841056            e.SetValueIfEmpty("modifiedby", CallerId);
 2841057            e.SetValueIfEmpty("ownerid", CallerId);
 2841058            e.SetValueIfEmpty("statecode", new OptionSetValue(0)); //Active by default
 59
 2841060             if (ctx.InitializationLevel == EntityInitializationLevel.PerEntity)
 36061            {
 36062                 if (!string.IsNullOrEmpty(e.LogicalName) && InitializerServiceDictionary.ContainsKey(e.LogicalName))
 14463                    InitializerServiceDictionary[e.LogicalName].Initialize(e, gCallerId, ctx, isManyToManyRelationshipEn
 36064            }
 65
 2841066            return e;
 2848267        }
 68
 69        public Entity Initialize(Entity e, XrmFakedContext ctx, bool isManyToManyRelationshipEntity = false)
 070        {
 071            return this.Initialize(e, Guid.NewGuid(), ctx, isManyToManyRelationshipEntity);
 072        }
 73    }
 74}