< Summary

Class:DCL.ServiceLocator
Assembly:Environment
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Environment/ServiceLocator.cs
Covered lines:32
Uncovered lines:10
Coverable lines:42
Total lines:100
Line coverage:76.1% (32 of 42)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ServiceLocator()0%110100%
ServiceLocator()0%110100%
Register[T](...)0%220100%
Unregister[T]()0%6200%
Get[T]()0%3.13077.78%
Initialize()0%4.034087.5%
Dispose()0%220100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Environment/ServiceLocator.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using UnityEngine;
 4using UnityEngine.Assertions;
 5
 6namespace DCL
 7{
 8    public delegate IService ServiceBuilder();
 9
 10    public class ServiceLocator : IDisposable
 11    {
 112        private static bool VERBOSE = false;
 113        private static Logger logger = new Logger("ServiceLocator") { verboseEnabled = VERBOSE };
 14
 88315        private Dictionary<Type, IService> services = new Dictionary<Type, IService>();
 88316        private Dictionary<Type, ServiceBuilder> serviceBuilders = new Dictionary<Type, ServiceBuilder>();
 17
 18        public ServiceBuilder Register<T>(ServiceBuilder data) where T : IService
 19        {
 1996920            Type type = typeof(T);
 1996921            Assert.IsTrue( type.IsInterface, "ServiceLocator's generic type should be an interface." );
 22
 1996923            if (serviceBuilders.ContainsKey(type))
 24            {
 284225                serviceBuilders[type] = data;
 284226                return data;
 27            }
 28
 1712729            serviceBuilders.Add(type, data);
 1712730            return data;
 31        }
 32
 33        public void Unregister<T>() where T : IService
 34        {
 035            Type type = typeof(T);
 036            Assert.IsTrue( type.IsInterface, "ServiceLocator's generic type should be an interface." );
 37
 038            if (!serviceBuilders.ContainsKey(type))
 39            {
 040                logger.Warning($"Trying to unregister non-existent type! type: {type.FullName}");
 041                return;
 42            }
 43
 044            serviceBuilders.Remove(type);
 045        }
 46
 47        public T Get<T>() where T : class, IService
 48        {
 4523449            Type type = typeof(T);
 4523450            Assert.IsTrue( type.IsInterface, "ServiceLocator's generic type should be an interface." );
 51
 4523452            if (!serviceBuilders.ContainsKey(type))
 53            {
 5754                logger.Verbose($"Not registered! use Register<T> to set the type builder first and then Initialize() to 
 5755                return null;
 56            }
 57
 4517758            if (!services.ContainsKey(type))
 59            {
 060                logger.Warning($"Not initialized! use Initialize() to create this service. type: {type.FullName}");
 061                return null;
 62            }
 63
 4517764            return services[type] as T;
 65        }
 66
 67        public void Initialize()
 68        {
 176469            foreach ( var service in services )
 70            {
 071                service.Value.Dispose();
 72            }
 73
 88274            services.Clear();
 75
 3601876            foreach ( var kvp in serviceBuilders )
 77            {
 1712778                var builderType = kvp.Key;
 1712779                var builder = kvp.Value;
 1712780                services.Add( builderType, builder.Invoke() );
 81            }
 82
 3601883            foreach ( var service in services )
 84            {
 1712785                service.Value.Initialize();
 86            }
 88287        }
 88
 89        public void Dispose()
 90        {
 3777891            foreach ( var service in services )
 92            {
 1712793                service.Value.Dispose();
 94            }
 95
 176296            services.Clear();
 176297            serviceBuilders.Clear();
 176298        }
 99    }
 100}