Beispiel #1
0
        protected virtual object GetService(Type serviceType)
        {
            serviceType.EnsureNotNull(nameof(serviceType)).EnsureClassOrInterface();
            //
            var invoker = default(P_ResolveDependencyInvokerBase);

            if (!__ResolveDependencyInvokeRepoSpinLock.Invoke(() => __ResolveDependencyInvokers.TryGetValue(key: serviceType, value: out invoker)))
            {
                invoker = ActivationUtilities.RequireConstructor <P_ResolveDependencyInvokerBase>(typeof(P_ResolveDependencyInvoker <>).MakeGenericType(typeArguments: serviceType))();
                __ResolveDependencyInvokeRepoSpinLock
                .Invoke(
                    action:
                    () => {
                    try {
                        __ResolveDependencyInvokers.Add(key: serviceType, value: invoker);
                    }
                    catch (ArgumentException) {
                        invoker = __ResolveDependencyInvokers[serviceType];
                    }
                });
            }
            object dependency;

            using (var ctx = new DependencyResolutionContext(scope: this, specs: new DependencyResolutionSpecs(dependencyType: serviceType, ensureResolution: false), ownsSpecs: true))
                invoker.Invoke(scope: this, ctx: ctx, dependency: out dependency);
            return(dependency);
        }
Beispiel #2
0
        public static IQueryable <T> GetEmptyQueryable <T>()
        {
            var typeOfT = typeof(T);
            var fromRepositoryResult = default(object);

            if (__EmptyQueryableRepositorySpinLock.Invoke(() => __EmptyQueryableRepository.TryGetValue(typeOfT, out fromRepositoryResult)))
            {
                return((IQueryable <T>)fromRepositoryResult);
            }
            else
            {
                var newEmptyQueryable = new P_EmptyQueryable <T>();
                return
                    (__EmptyQueryableRepositorySpinLock
                     .Invoke(
                         () => {
                    if (__EmptyQueryableRepository.TryGetValue(typeOfT, out fromRepositoryResult))
                    {
                        return (IQueryable <T>)fromRepositoryResult;
                    }
                    else
                    {
                        __EmptyQueryableRepository.Add(typeOfT, newEmptyQueryable);
                        return newEmptyQueryable;
                    }
                }));
            }
        }
Beispiel #3
0
        public static EffectiveAnnotations GetAnnotations(Type annotatedType)
        {
            annotatedType.EnsureNotNull(nameof(annotatedType));
            //
            var reader = default(AnnotationReader);

            if (!__AnnotationCacheSpinLock.Invoke(() => __AnnotationCache.TryGetValue(annotatedType, out reader)))
            {
                var newReader = new AnnotationReader(annotatedType: annotatedType);
                reader =
                    __AnnotationCacheSpinLock
                    .Invoke(
                        func:
                        () => {
                    if (__AnnotationCache.ContainsKey(key: annotatedType))
                    {
                        return(__AnnotationCache[annotatedType]);
                    }
                    else
                    {
                        __AnnotationCache.Add(key: annotatedType, value: newReader);
                        return(newReader);
                    }
                });
            }
            return(reader.Read());
        }
Beispiel #4
0
        public static long GetCreateWithoutDisposeCount()
        {
#if DEBUG
            //var arr = __CountersSpinLock.Invoke(() => __Counters.OrderByDescending(locItem => locItem.Value.CreateWithoutDisposeCount).Select(locItem => (T: locItem.Key, V: locItem.Value.CreateWithoutDisposeCount)).Take(20).ToArray());
            return(__CountersSpinLock.Invoke(() => __Counters.Values.Sum(locItem => locItem.CreateWithoutDisposeCount)));
#else
            return(__Counters.CreateWithoutDisposeCount);
#endif
        }
Beispiel #5
0
        static bool P_TryGetFromCache(string metadataPathNameString, out MetadataPathName result)
        {
            metadataPathNameString.EnsureNotNull(nameof(metadataPathNameString));
            //
            MetadataPathName locResult = null;

            if (__CacheSpinLock.Invoke(() => __Cache.TryGetValue(metadataPathNameString, out locResult)))
            {
                result = locResult;
                return(true);
            }
            else
            {
                result = null;
                return(false);
            }
        }
Beispiel #6
0
        public static TValue GetOrDefault <TKey, TValue>(this IDictionary <TKey, TValue> dictionary, PrimitiveSpinLock spinLock, TKey key)
        {
            dictionary.EnsureNotNull(nameof(dictionary));
            spinLock.EnsureNotNull(nameof(spinLock));
            //
            var value = default(TValue);

            return(spinLock.Invoke(() => dictionary.TryGetValue(key, out value)) ? value : default);
Beispiel #7
0
 // TODO: Put exception messages into the resources.
 //
 public static void RegisterTranslator(Type exceptionType, IExceptionInfoTranslator parser)
 {
     exceptionType
     .EnsureNotNull(nameof(exceptionType))
     .EnsureCompatible(typeof(Exception));
     parser.EnsureNotNull(nameof(parser));
     //
     __SpinLock
     .Invoke(
         () => {
         if (__RegisteredTranslatorsRepository.ContainsKey(exceptionType))
         {
             __RegisteredTranslatorsRepository[exceptionType] = parser;
         }
         else if (__RegisteredTranslatorsRepository.Count >= MaxAllowedCountOfRegisteredTranslators)
         {
             throw new InvalidOperationException("It is impossible to register an exception info parser. The count of registered parsers has reached the max. allowed limit.");
         }
         else
         {
             __RegisteredTranslatorsRepository.Add(exceptionType, parser);
         }
     });
 }
Beispiel #8
0
 static void P_RegisterForCollection(Leasable item)
 {
     if (item is null)
     {
         throw new ArgumentNullException(paramName: nameof(item));
     }
     //
     if (!item.PreferredSlidingTtl.IsInfinite)
     {
         var reference = new WeakReference <Leasable>(target: item, trackResurrection: false);
         var repoSize  = __CollectionRepoSpinLock.Invoke(() => { __CollectionRepo.Add(reference); return(__CollectionRepo.Count); });
         if (repoSize == 1)
         {
             P_ScheduleNextRepoCollection(afterMillisecondsDelay: DefaultMaxCollectionInterval.Milliseconds);
         }
     }
 }
Beispiel #9
0
        public static IArgsTuple <TArg1> Create <TArg1>(TArg1 arg1)
        {
            var typesKey   = new TypeTuple1(arg1?.GetType() ?? typeof(TArg1));
            var createFunc = default(P_CreateFuncBase);

            if (!__CreateFuncsSpinLock.Invoke(() => __CreateFuncs1.TryGetValue(typesKey, out createFunc)))
            {
                createFunc = ActivationUtilities.RequireConstructor <P_CreateFuncBase>(
                    typeof(P_CreateFunc <>)
                    .MakeGenericType(typesKey.Item1))();
                createFunc = __CreateFuncs1.GetOrAdd(spinLock: __CreateFuncsSpinLock, key: typesKey, factory: key => createFunc);
            }
            return(createFunc.Create(arg1));
        }
Beispiel #10
0
        // TODO: Put exception messages into the resources.
        //
        public static Type GetType(string typeName, bool throwOnError)
        {
            typeName.Arg(nameof(typeName)).EnsureNotNullOrWhiteSpace();
            //
            var result = default(Type);

            if (__TypeByNameRepositorySpinLock.Invoke(() => __TypeByNameRepository.TryGetValue(key: typeName, value: out result)))
            {
                return(result);
            }
            else
            {
                var platformGetTypeException = default(Exception);
                try { result = Type.GetType(typeName: typeName, throwOnError: true); }
                catch (Exception exception) {
                    result = null;
                    platformGetTypeException = exception;
                }
                if (platformGetTypeException is null)
                {
                    return(P_GetOrAddIntoTypeByNameRepository(typeName: typeName, type: result));
                }
Beispiel #11
0
        public Task <TToResult> ConvertTask <TToResult>()
        {
            var toResultType = typeof(TToResult);

            if (__ResultType == toResultType)
            {
                return((Task <TToResult>)(Task) _innerTask);
            }
            else
            {
                Task converted = default;
                if (!_convertedTasksSpinLock.Invoke(() => _convertedTasks.TryGetValue(key: toResultType, value: out converted)))
                {
                    var taskProxy      = new TaskCompletionSource <TToResult>(creationOptions: TaskCreationOptions.None);
                    var registeredTask =
                        _convertedTasksSpinLock
                        .Invoke(
                            func:
                            () => {
                        try {
                            _convertedTasks.Add(key: toResultType, value: taskProxy.Task);
                            return(taskProxy.Task);
                        }
                        catch (ArgumentException) {
                            return(_convertedTasks[key: toResultType]);
                        }
                    });
                    if (ReferenceEquals(taskProxy.Task, registeredTask))
                    {
                        _innerTask
                        .ContinueWith(
                            continuationAction:
                            locInnerTask => {
                            if (locInnerTask.IsCanceled)
                            {
                                taskProxy.SetCanceled();
                            }
                            else if (locInnerTask.IsFaulted)
                            {
                                TaskUtilitiesInternalCoreL1.SetException(task: locInnerTask, setter: locException => taskProxy.SetException(locException));
                            }
                            else
                            {
                                try {
                                    taskProxy.SetResult(result: locInnerTask.Result.Cast <TResult, TToResult>());
                                }
                                catch (Exception locException) {
                                    taskProxy.SetException(exception: locException);
                                }
                            }
                        },
                            continuationOptions: TaskContinuationOptions.ExecuteSynchronously);
                    }
                    else
                    {
                        taskProxy.TrySetCanceled();
                    }
                    converted = registeredTask;
                }
                return((Task <TToResult>)converted);
            }
        }