public static void Initialize()
 {
     if (_key == null)
     {
         _key = new SingletonKey();
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Unregisters a singleton instance of a type.
        /// </summary>
        /// <param name="Object">Singleton object instance.</param>
        /// <param name="Arguments">Any constructor arguments associated with the object instance.</param>
        /// <returns>If the instance was found and removed.</returns>
        public static bool Unregister(object Object, params object[] Arguments)
        {
            SingletonKey Key = new SingletonKey(Object.GetType(), Arguments);

            lock (instances)
            {
                return(instances.Remove(Key));
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Checks if a singleton type (with optional associated arguments) is registered.
        /// </summary>
        /// <param name="Type">Singleton type</param>
        /// <param name="Arguments">Any constructor arguments associated with the type.</param>
        /// <returns>If such a singleton type is registered.</returns>
        public static bool IsRegistered(Type Type, params object[] Arguments)
        {
            SingletonKey Key = new SingletonKey(Type, Arguments);

            lock (instances)
            {
                return(instances.ContainsKey(Key));
            }
        }
Exemplo n.º 4
0
        public static T Get <T>(object key, bool throwExceptionIfNotFind = false)
        {
            var res = Get(SingletonKey.GetKey <T>(key));

            if (res != null)
            {
                return((T)res);
            }
            if (throwExceptionIfNotFind)
            {
                throw new KeyNotFoundException("No se ha encontrado el objecto de tipo '" + typeof(T).Name + " con la clave '" + (key ?? "null") + "'.");
            }
            return(default(T));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Registers a singleton instance of a type.
        /// </summary>
        /// <param name="Object">Singleton objcet instance.</param>
        /// <param name="Arguments">Any constructor arguments associated with the object instance.</param>
        public static void Register(object Object, params object[] Arguments)
        {
            SingletonKey Key = new SingletonKey(Object.GetType(), Arguments);

            lock (instances)
            {
                if (instances.ContainsKey(Key))
                {
                    throw new InvalidOperationException("Singleton already registered.");
                }

                instances[Key] = Object;
            }
        }
Exemplo n.º 6
0
 private static void Add <T>(T objectInstance, SingletonKey key)
 {
     if (Instance.objects.ContainsKey(key))
     {
         throw new ArgumentException("No se puede agregar el objeto porque la combinación clave/tipo esta en uso");
     }
     Instance.objects.Add(key, objectInstance);
     foreach (var sub in Instance.subscriptions)
     {
         if (sub.Type == ((object)objectInstance).GetType() && sub.Key == key)
         {
             sub.Invoke(default(T), objectInstance, SingletonAction.Add);
         }
     }
 }
Exemplo n.º 7
0
 //public static bool Set(Type type, object key, object substitute) { return Set(SingletonKey.GetKey(type, key), substitute); }
 private static bool Set <T>(SingletonKey key, T substitute)
 {
     if (Instance.objects.ContainsKey(key))
     {
         var previous = (T)Instance.objects[key];
         Instance.objects[key] = substitute;
         foreach (var sub in Instance.subscriptions)
         {
             if (sub.Type == substitute.GetType() && sub.Key == key)
             {
                 sub.Invoke(previous, substitute, SingletonAction.Set);
             }
         }
         return(true);
     }
     return(false);
 }
Exemplo n.º 8
0
        /// <summary>
        /// Returns an instance of the type <paramref name="Type"/>.
        /// </summary>
        /// <param name="Type">Type of objects to return.</param>
        /// <param name="Arguments">Constructor arguments.</param>
        /// <returns>Instance of <paramref name="Type"/>.</returns>
        public object Instantiate(Type Type, params object[] Arguments)
        {
            SingletonKey Key = new SingletonKey(Type, Arguments);

            lock (instances)
            {
                if (instances.TryGetValue(Key, out object Object))
                {
                    return(Object);
                }

                Object         = Activator.CreateInstance(Type, Arguments);
                instances[Key] = Object;

                return(Object);
            }
        }
Exemplo n.º 9
0
        private static bool Remove <T>(SingletonKey key)
        {
            if (!Instance.objects.ContainsKey(key))
            {
                return(false);
            }
            var obj = Instance.objects[key];
            var res = Instance.objects.Remove(key);

            if (!res)
            {
                return(false);
            }
            foreach (var sub in Instance.subscriptions.Where(sub => sub.Type == obj.GetType() && sub.Key == key))
            {
                sub.Invoke(obj, default(T), SingletonAction.Remove);
            }
            return(true);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Returns an instance of the type <paramref name="Type"/>.
        /// </summary>
        /// <param name="ReturnNullIfFail">If null should be returned instead for throwing exceptions.</param>
        /// <param name="Type">Type of objects to return.</param>
        /// <param name="Arguments">Constructor arguments.</param>
        /// <returns>Instance of <paramref name="Type"/>.</returns>
        public object Instantiate(bool ReturnNullIfFail, Type Type, params object[] Arguments)
        {
            SingletonKey Key = new SingletonKey(Type, Arguments);
            object       Object;

            lock (instances)
            {
                if (instances.TryGetValue(Key, out Object))
                {
                    return(Object);
                }
            }

            Object = Types.Create(ReturnNullIfFail, Type, Arguments);
            if (Object is null)
            {
                return(null);
            }

            lock (instances)
            {
                if (instances.TryGetValue(Key, out object Object2))
                {
                    if (Object is IDisposable Disposable)
                    {
                        Disposable.Dispose();
                    }

                    return(Object2);
                }

                instances[Key] = Object;
            }

            return(Object);
        }
Exemplo n.º 11
0
 private static object Get(SingletonKey key)
 {
     return(Instance.objects.ContainsKey(key) ? Instance.objects[key] : null);
 }
Exemplo n.º 12
0
 protected MySingleton(SingletonKey key) : base(key)
 {
 }
Exemplo n.º 13
0
 public static bool Set <T>(T substitute)
 {
     return(Set <T>(SingletonKey.GetKey <T>(), substitute));
 }
Exemplo n.º 14
0
 protected Singleton(SingletonKey key)
 {
 }
Exemplo n.º 15
0
 public static bool Contains <T>()
 {
     return(Contains <T>(SingletonKey.GetKey <T>()));
 }
Exemplo n.º 16
0
 private static bool Contains <T>(SingletonKey key)
 {
     return(Instance.objects.ContainsKey(key));
 }
Exemplo n.º 17
0
 public static void Add <T>(T objectInstance, object key)
 {
     Add(objectInstance, SingletonKey.GetKey <T>(key));
 }
Exemplo n.º 18
0
 public static void Add <T>() where T : new()
 {
     Add(new T(), SingletonKey.GetKey <T>());
 }
Exemplo n.º 19
0
 private static void Subscribe <T>(Action <SingletonSubscriptionArgs <T> > changeAction, SingletonKey key, bool raiseAddIfAlreadyAdded = true)
 {
     Instance.subscriptions.Add(new SubscriptionItem {
         Type = typeof(T), Key = key, Action = changeAction
     });
     if (raiseAddIfAlreadyAdded && Instance.objects.ContainsKey(key))
     {
         changeAction(new SingletonSubscriptionArgs <T>(default(T), (T)Instance.objects[key], SingletonAction.Add));
     }
 }
Exemplo n.º 20
0
 public static void Subscribe <T>(Action <SingletonSubscriptionArgs <T> > changeAction, object key, bool raiseAddIfAlreadyAdded = true)
 {
     Subscribe <T>(changeAction, SingletonKey.GetKey <T>(key));
 }
Exemplo n.º 21
0
 public static bool Remove <T>(object key)
 {
     return(Remove <T>(SingletonKey.GetKey <T>(key)));
 }
Exemplo n.º 22
0
 public static bool Set <T>(T substitute, object key)
 {
     return(Set <T>(SingletonKey.GetKey <T>(key), substitute));
 }
Exemplo n.º 23
0
 public static bool Remove <T>()
 {
     return(Remove <T>(SingletonKey.GetKey <T>()));
 }
Exemplo n.º 24
0
 public static bool Contains <T>(object key)
 {
     return(Contains <T>(SingletonKey.GetKey <T>(key)));
 }