2 using System.Collections.Generic;
4 using System.Reflection;
5 using Tizen.NUI.Binding.Internals;
8 namespace Tizen.NUI.Binding
10 internal static class DependencyService
12 static bool s_initialized;
14 static readonly List<Type> DependencyTypes = new List<Type>();
15 static readonly Dictionary<Type, DependencyData> DependencyImplementations = new Dictionary<Type, DependencyData>();
17 public static T Resolve<T>(DependencyFetchTarget fallbackFetchTarget = DependencyFetchTarget.GlobalInstance) where T : class
19 var result = DependencyResolver.Resolve(typeof(T)) as T;
21 return result ?? Get<T>(fallbackFetchTarget);
24 public static T Get<T>(DependencyFetchTarget fetchTarget = DependencyFetchTarget.GlobalInstance) where T : class
28 Type targetType = typeof(T);
30 if (!DependencyImplementations.ContainsKey(targetType))
32 Type implementor = FindImplementor(targetType);
33 DependencyImplementations[targetType] = implementor != null ? new DependencyData { ImplementorType = implementor } : null;
36 DependencyData dependencyImplementation = DependencyImplementations[targetType];
37 if (dependencyImplementation == null)
40 if (fetchTarget == DependencyFetchTarget.GlobalInstance)
42 if (dependencyImplementation.GlobalInstance == null)
44 dependencyImplementation.GlobalInstance = Activator.CreateInstance(dependencyImplementation.ImplementorType);
46 return (T)dependencyImplementation.GlobalInstance;
48 return (T)Activator.CreateInstance(dependencyImplementation.ImplementorType);
51 public static void Register<T>() where T : class
53 Type type = typeof(T);
54 if (!DependencyTypes.Contains(type))
55 DependencyTypes.Add(type);
58 public static void Register<T, TImpl>() where T : class where TImpl : class, T
60 Type targetType = typeof(T);
61 Type implementorType = typeof(TImpl);
62 if (!DependencyTypes.Contains(targetType))
63 DependencyTypes.Add(targetType);
65 DependencyImplementations[targetType] = new DependencyData { ImplementorType = implementorType };
68 static Type FindImplementor(Type target)
70 return DependencyTypes.FirstOrDefault(t => target.IsAssignableFrom(t));
73 static void Initialize()
80 Assembly[] assemblies = Device.GetAssemblies();
81 if (Tizen.NUI.Binding.Internals.Registrar.ExtraAssemblies != null)
83 assemblies = assemblies.Union(Tizen.NUI.Binding.Internals.Registrar.ExtraAssemblies).ToArray();
86 Initialize(assemblies);
89 internal static void Initialize(Assembly[] assemblies)
91 if (s_initialized || assemblies == null)
95 DependencyService.Register<IValueConverterProvider, ValueConverterProvider>();
97 Type targetAttrType = typeof(DependencyAttribute);
99 // Don't use LINQ for performance reasons
100 // Naive implementation can easily take over a second to run
101 foreach (Assembly assembly in assemblies)
103 Attribute[] attributes;
106 attributes = assembly.GetCustomAttributes(targetAttrType).ToArray();
108 catch (System.IO.FileNotFoundException)
110 // Sometimes the previewer doesn't actually have everything required for these loads to work
111 Console.WriteLine(nameof(Registrar), "Could not load assembly: {0} for Attibute {1} | Some renderers may not be loaded", assembly.FullName, targetAttrType.FullName);
115 if (attributes.Length == 0)
118 foreach (DependencyAttribute attribute in attributes)
120 if (!DependencyTypes.Contains(attribute.Implementor))
122 DependencyTypes.Add(attribute.Implementor);
127 s_initialized = true;
132 public object GlobalInstance { get; set; }
134 public Type ImplementorType { get; set; }