/// <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(); }
/// <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(); }
/// <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); }
/// <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); }
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); }
/// <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); }
internal void LateInjectAll <T>(IEnumerable <T> instances, Scoped scope) { foreach (T instance in instances) { if (instance != null) { LateInject(instance, scope); } } }
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)); }
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)); }
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); }
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)); } }
/// <inheritdoc /> public void ApplyLifetimeOnExpression(Scoped containerScope, ExpressionContext context) { //No caching for transients. }
private T?GetInstanceOrDefault <T>(Scoped scope) where T : class => (T?)GetInstanceOrDefault(typeof(T), scope);
private T GetInstance <T>(Scoped scope) where T : class => (T)GetInstance(typeof(T), scope);
/// <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; }
internal T GetInstance <T>(Scoped scope) where T : class => (T)GetInstance(typeof(T), scope);