예제 #1
0
        private object GetServiceCore(ServiceRegistry registry, Type[] genericArguments)
        {
            var serviceType = registry.ServiceType;

            object GetOrCreate(ConcurrentDictionary <ServiceRegistry, object> services, ConcurrentBag <IDisposable> disposables)
            {
                if (services.TryGetValue(registry, out var service))
                {
                    return(service);
                }
                service            = registry.Factory(this, genericArguments);
                services[registry] = service;
                var disposable = service as IDisposable;

                if (null != disposable)
                {
                    disposables.Add(disposable);
                }
                return(service);
            }

            switch (registry.Lifetime)
            {
            case Lifetime.Singlelton: return(GetOrCreate(_root._services, _root._disposables));

            case Lifetime.Self: return(GetOrCreate(_services, _disposables));

            default:
            {
                var service    = registry.Factory(this, genericArguments);
                var disposable = service as IDisposable;
                if (null != disposable)
                {
                    _disposables.Add(disposable);
                }
                return(service);
            }
            }
        }
예제 #2
0
        private object GetServiceCore(ServiceRegistry registry, Type[] genericArguments)
        {
            var key         = new Key(registry, genericArguments);
            var serviceType = registry.ServiceType;

            switch (registry.Lifetime)
            {
            case Lifetime.Root: return(GetOrCreate(_root._services, _root._disposables));

            case Lifetime.Self: return(GetOrCreate(_services, _disposables));

            default:
            {
                var service = registry.Factory(this, genericArguments);
                if (service is IDisposable disposable && disposable != this)
                {
                    _disposables.Add(disposable);
                }
                return(service);
            }
            }

            object GetOrCreate(ConcurrentDictionary <Key, object> services, ConcurrentBag <IDisposable> disposables)
            {
                if (services.TryGetValue(key, out var service))
                {
                    return(service);
                }
                service       = registry.Factory(this, genericArguments);
                services[key] = service;
                if (service is IDisposable disposable)
                {
                    disposables.Add(disposable);
                }
                return(service);
            }
        }