Beispiel #1
0
        public IContainerBuilder As([NotNull] Type type)
        {
            if (type is null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (_baseStrategy != null && IsSingleTon)
            {
                _xStrategies[type] = _baseStrategy;
                return(this);
            }

            _xStrategies[type] = new XStrategy
            {
                OnlyOne  = IsSingleTon,
                Strategy = StrategyExpression
            };


            if (IsSingleTon)
            {
                _baseStrategy = _xStrategies[type];
            }


            return(this);
        }
Beispiel #2
0
        private bool RegisterType(Type itemType, StrategyExpression factory, bool onlyOne = false)
        {
            if (_registeredTypes.TryGetValue(itemType, out _))
            {
                return(false);
            }

            _registeredTypes[itemType] = new XStrategy
            {
                Strategy = factory,
                OnlyOne  = onlyOne
            };

            return(true);
        }
Beispiel #3
0
        protected object GetObject(Type type, XStrategy factory, ILifetime lifetime,
                                   ResolveMode resolveMode = ResolveMode.Global)
        {
            if (factory.OnlyOne)
            {
                return(factory.Instance ??= factory.Strategy(lifetime));
            }


            if (resolveMode == ResolveMode.Global)
            {
                return(_instanceLifetime.GetOrAdd(type, _ => factory.Strategy(lifetime)));
            }

            return(factory.Strategy(lifetime));
        }