Esempio n. 1
0
        /// <inheritdoc />
        public void ApplyLifetimeOnExpression(Scoped containerScope, ExpressionContext context)
        {
            MethodInfo method = CreateScopedExpressionMethod.MakeGenericMethod(context.Expression.Type);

            context.Expression = (Expression)method.Invoke(null, new object[] { context.Expression });
            context.ScopedExpressions.Clear();
        }
Esempio n. 2
0
        /// <inheritdoc />
        public void ApplyLifetimeOnExpression(Scoped containerScope, ExpressionContext context)
        {
            object singletonInstance = GetSingleton(containerScope, context.Expression);

            context.Expression = Expression.Constant(singletonInstance, context.Expression.Type);
            context.ScopedExpressions.Clear();
        }
Esempio n. 3
0
 /// <summary>
 /// Creates a new container, consuming the provided builder.
 /// </summary>
 /// <param name="builder"></param>
 public Container(ContainerBuilder builder)
 {
     ContainerScope  = new Scoped(this);
     Registrations   = builder.Registrations;
     Settings        = builder.Settings;
     DependencyGraph = new InstanceFactoryResolver(builder.Registrations, ContainerScope, Settings, null);
 }
Esempio n. 4
0
 /// <summary>
 /// Creates a new child container using the provided builder.
 /// </summary>
 /// <param name="parentContainer"></param>
 /// <param name="builder"></param>
 private Container(Container parentContainer, ContainerBuilder builder)
 {
     Settings         = parentContainer.Settings;
     _parentContainer = parentContainer;
     ContainerScope   = new Scoped(this);
     Registrations    = builder.Registrations;
     DependencyGraph  = new InstanceFactoryResolver(builder.Registrations, ContainerScope, Settings, parentContainer.DependencyGraph);
 }
Esempio n. 5
0
        private Container(Container parentContainer, Action <ContainerBuilder>?builder)
        {
            var context = new ContainerBuilder(this);

            builder?.Invoke(context);
            _options         = parentContainer._options;
            _containerScope  = new Scoped(this);
            Registrations    = context.Registrations;
            _dependencyGraph = new DependencyGraph(context.Registrations, _containerScope, _options, parentContainer._dependencyGraph);
        }
Esempio n. 6
0
        /// <summary>
        /// Creates a new container using the provided builder.
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="options"></param>
        public Container(Action <ContainerBuilder>?builder = null, SingularitySettings?options = null)
        {
            var context = new ContainerBuilder(this);

            builder?.Invoke(context);
            _options         = options ?? SingularitySettings.Default;
            _containerScope  = new Scoped(this);
            Registrations    = context.Registrations;
            _dependencyGraph = new DependencyGraph(context.Registrations, _containerScope, _options);
        }
Esempio n. 7
0
 internal void LateInjectAll <T>(IEnumerable <T> instances, Scoped scope)
 {
     foreach (T instance in instances)
     {
         if (instance != null)
         {
             LateInject(instance, scope);
         }
     }
 }
Esempio n. 8
0
        internal object?GetInstanceOrDefault(Type type, Scoped scope)
        {
            Func <Scoped, object?>?func = _getInstanceCache.GetOrDefault(type);

            if (func == null)
            {
                func = DependencyGraph.TryResolve(type)?.Factory ?? (s => null !);
                _getInstanceCache.Add(type, func);
            }
            return(func(scope));
        }
Esempio n. 9
0
        internal object GetInstance(Type type, Scoped scope)
        {
            Func <Scoped, object> func = _getInstanceCache.Get(type);

            if (func == null)
            {
                func = _dependencyGraph.GetResolvedFactory(type) !;
                _getInstanceCache.Add(type, func);
            }
            return(func(scope));
        }
Esempio n. 10
0
        internal void LateInject(object instance, Scoped scope)
        {
            Type type = instance.GetType();
            Action <Scoped, object>?action = _injectionCache.GetOrDefault(type);

            if (action == null)
            {
                action = GenerateLateInjector(type);
                _injectionCache.Add(type, action);
            }
            action(scope, instance);
        }
Esempio n. 11
0
        private static object GetSingleton(Scoped containerScope, Expression expression)
        {
            switch (expression)
            {
            case ConstantExpression constantExpression:
                return(constantExpression.Value);

            case NewExpression newExpression:
                if (newExpression.Arguments.Count == 0)
                {
                    return(newExpression.Constructor.Invoke(null));
                }
                else
                {
                    return(((Func <Scoped, object>)Expression.Lambda(expression, ExpressionGenerator.ScopeParameter).CompileFast())(containerScope));
                }

            default:
                return(((Func <Scoped, object>)Expression.Lambda(expression, ExpressionGenerator.ScopeParameter).CompileFast())(containerScope));
            }
        }
Esempio n. 12
0
 /// <inheritdoc />
 public void ApplyLifetimeOnExpression(Scoped containerScope, ExpressionContext context)
 {
     //No caching for transients.
 }
Esempio n. 13
0
 private T?GetInstanceOrDefault <T>(Scoped scope) where T : class => (T?)GetInstanceOrDefault(typeof(T), scope);
Esempio n. 14
0
 private T GetInstance <T>(Scoped scope) where T : class => (T)GetInstance(typeof(T), scope);
Esempio n. 15
0
 /// <inheritdoc />
 public void ApplyLifetimeOnExpression(Scoped containerScope, ExpressionContext context)
 {
     throw new NotImplementedException(nameof(PerGraph));
 }
 /// <summary>
 /// Creates a new service provider with the provided <see cref="Scoped"/>.
 /// </summary>
 /// <param name="scope"></param>
 public SingularityServiceScope(Scoped scope)
 {
     _scope = scope;
 }
Esempio n. 17
0
 internal T GetInstance <T>(Scoped scope) where T : class => (T)GetInstance(typeof(T), scope);