Esempio n. 1
0
        public void Register <TInterface>(TInterface instance, string key = null)
        {
            key = GetKeyValueOrDefault(key);
            Type type = typeof(TInterface);

            bool useLock = ThreadSafe;

            if (useLock)
            {
                lockSlim.EnterWriteLock();
            }

            try
            {
                if (!typeDictionary.TryGetValue(type,
                                                out ResolverDictionary value))
                {
                    value = new ResolverDictionary();
                }

                value[key] = new Resolver {
                    CreateInstanceFunc = () => instance
                };
                typeDictionary[type] = value;
            }
            finally
            {
                if (useLock)
                {
                    lockSlim.ExitWriteLock();
                }
            }
        }
Esempio n. 2
0
        public void Register(
            Type type,
            Func <object> getInstanceFunc,
            bool singleton = false,
            string key     = null)
        {
            AssertArg.IsNotNull(type, nameof(type));
            AssertArg.IsNotNull(getInstanceFunc, nameof(getInstanceFunc));

            key = GetKeyValueOrDefault(key);

            bool useLock = ThreadSafe;

            if (useLock)
            {
                lockSlim.EnterWriteLock();
            }

            try
            {
                if (!typeDictionary.TryGetValue(type,
                                                out ResolverDictionary resolverDictionary))
                {
                    resolverDictionary = new ResolverDictionary();
                }

                Resolver resolver = new Resolver {
                    Singleton = singleton
                };

                Func <object> getObjectFunc = () =>
                {
                    var result = getInstanceFunc();

                    if (resolver.Singleton)
                    {
                        resolver.CreateInstanceFunc = null;
                        resolver.Instance           = result;
                    }

                    return(result);
                };

                resolver.CreateInstanceFunc = getObjectFunc;

                resolverDictionary[key] = resolver;
                keyDictionary[key]      = type;

                typeDictionary[type] = resolverDictionary;
            }
            finally
            {
                if (useLock)
                {
                    lockSlim.ExitWriteLock();
                }
            }
        }
Esempio n. 3
0
        public void Register(Type fromType, Type toType,
                             bool singleton = false, string key = null)
        {
            key = GetKeyValueOrDefault(key);

            bool useLock = ThreadSafe;

            if (useLock)
            {
                lockSlim.EnterWriteLock();
            }

            try
            {
                if (!typeDictionary.TryGetValue(fromType,
                                                out ResolverDictionary resolverDictionary))
                {
                    resolverDictionary       = new ResolverDictionary();
                    typeDictionary[fromType] = resolverDictionary;
                }

                Resolver resolver = new Resolver
                {
                    CreateInstanceFunc = () => Instantiate(toType),
                    Singleton          = singleton
                };

                resolverDictionary[key] = resolver;
                keyDictionary[key]      = fromType;
            }
            finally
            {
                if (useLock)
                {
                    lockSlim.ExitWriteLock();
                }
            }
        }
Esempio n. 4
0
        public void Register(Type type, object instance, string key = null)
        {
            AssertArg.IsNotNull(type, nameof(type));

            key = GetKeyValueOrDefault(key);

            bool useLock = ThreadSafe;

            if (useLock)
            {
                lockSlim.EnterWriteLock();
            }

            try
            {
                if (!typeDictionary.TryGetValue(
                        type, out ResolverDictionary value))
                {
                    value = new ResolverDictionary();
                }

                Resolver info = new Resolver {
                    CreateInstanceFunc = () => instance
                };

                value[key] = info;

                typeDictionary[type] = value;
            }
            finally
            {
                if (useLock)
                {
                    lockSlim.ExitWriteLock();
                }
            }
        }
Esempio n. 5
0
        public void Register <T>(
            Func <T> getInstanceFunc,
            bool singleton = false,
            string key     = null)
        {
            AssertArg.IsNotNull(getInstanceFunc, nameof(getInstanceFunc));

            key = GetKeyValueOrDefault(key);
            Type type = typeof(T);

            bool useLock = ThreadSafe;

            if (useLock)
            {
                lockSlim.EnterWriteLock();
            }

            try
            {
                if (!typeDictionary.TryGetValue(type,
                                                out ResolverDictionary resolverDictionary))
                {
                    resolverDictionary = new ResolverDictionary();
                }

                Resolver resolver = new Resolver {
                    Singleton = singleton
                };

                Func <object> getObjectFunc = () =>
                {
                    if (cycleDictionary.TryGetValue(type, out List <string> keys))
                    {
                        if (keys.Contains(key))
                        {
                            /* TODO: Rather than throwing an exception,
                             * we could Post the property set operation to the UI thread. */
                            throw new ResolutionException(
                                      $"Cycle detected for {type} with key \"{key}\"");
                        }

                        keys.Add(key);
                    }
                    else
                    {
                        keys = new List <string> {
                            key
                        };
                        cycleDictionary.Add(type, keys);
                    }

                    T result;
                    try
                    {
                        result = getInstanceFunc();
                    }
                    finally
                    {
                        keys.Remove(key);
                    }

                    if (resolver.Singleton)
                    {
                        resolver.CreateInstanceFunc = null;
                        resolver.Instance           = result;
                    }

                    return(result);
                };

                resolver.CreateInstanceFunc = getObjectFunc;

                resolverDictionary[key] = resolver;
                keyDictionary[key]      = type;

                typeDictionary[type] = resolverDictionary;
            }
            finally
            {
                if (useLock)
                {
                    lockSlim.ExitWriteLock();
                }
            }
        }