Exemplo n.º 1
0
        public ResolverPipeline(RegistrationStore registrationStore, IDependencyResolver[] resolvers, Scoped containerScope, SingularitySettings settings, IResolverPipeline?parentPipeline)
        {
            _settings         = settings ?? throw new ArgumentNullException(nameof(settings));
            _resolvers        = resolvers ?? throw new ArgumentNullException(nameof(resolvers));
            _parentPipeline   = parentPipeline;
            SyncRoot          = parentPipeline?.SyncRoot ?? new object();
            _containerScope   = containerScope ?? throw new ArgumentNullException(nameof(containerScope));
            RegistrationStore = registrationStore ?? throw new ArgumentNullException(nameof(registrationStore));

            if (parentPipeline != null)
            {
                CheckChildRegistrations(parentPipeline, registrationStore.Registrations, SyncRoot);
            }
        }
Exemplo n.º 2
0
        public ResolverPipeline(RegistrationStore registrationStore, Scoped containerScope, SingularitySettings settings, ResolverPipeline?parentPipeline)
        {
            _settings  = settings ?? throw new ArgumentNullException(nameof(settings));
            _resolvers = new IDependencyResolver[]
            {
                new EnumerableDependencyResolver(),
                new ExpressionDependencyResolver(),
                new LazyDependencyResolver(),
                new FactoryDependencyResolver(),
                new ConcreteDependencyResolver(),
                new OpenGenericResolver()
            };
            _parentPipeline   = parentPipeline;
            SyncRoot          = parentPipeline?.SyncRoot ?? new object();
            _containerScope   = containerScope ?? throw new ArgumentNullException(nameof(containerScope));
            RegistrationStore = registrationStore ?? throw new ArgumentNullException(nameof(registrationStore));

            if (parentPipeline != null)
            {
                CheckChildRegistrations(parentPipeline.RegistrationStore.Registrations, registrationStore.Registrations, SyncRoot);
            }
        }
Exemplo n.º 3
0
        public DependencyGraph(RegistrationStore registrations, Scoped scope, SingularitySettings settings, DependencyGraph?parentDependencyGraph = null)
        {
            var resolvers = new IDependencyResolver[] { new EnumerableDependencyResolver(), new ExpressionDependencyResolver(), new LazyDependencyResolver(), new FactoryDependencyResolver(), new ConcreteDependencyResolver(), new OpenGenericResolver() };

            _resolverPipeline = new ResolverPipeline(registrations, resolvers, scope, settings, parentDependencyGraph?._resolverPipeline);
        }
        public InstanceFactoryResolver(RegistrationStore registrationStore, Scoped containerScope, SingularitySettings settings, InstanceFactoryResolver?parentPipeline)
        {
            Settings          = settings ?? throw new ArgumentNullException(nameof(settings));
            _resolvers        = Settings.ServiceBindingGenerators.ToArray();
            _parentPipeline   = parentPipeline;
            SyncRoot          = parentPipeline?.SyncRoot ?? new object();
            _containerScope   = containerScope ?? throw new ArgumentNullException(nameof(containerScope));
            RegistrationStore = registrationStore ?? throw new ArgumentNullException(nameof(registrationStore));

            if (parentPipeline != null)
            {
                CheckChildRegistrations(parentPipeline.RegistrationStore.Registrations, registrationStore.Registrations, SyncRoot);
            }
        }
Exemplo n.º 5
0
        public ReadOnlyExpressionContext GenerateBaseExpression(ServiceBinding serviceBinding, InstanceFactory[] children, Scoped containerScope, SingularitySettings settings)
        {
            var context = new ExpressionContext(serviceBinding.Expression);

            if (serviceBinding.Expression is AbstractBindingExpression)
            {
                context.Expression = Expression.Constant(serviceBinding);
                return(context);
            }
            context.Expression = serviceBinding.Expression !is LambdaExpression lambdaExpression ? lambdaExpression.Body : serviceBinding.Expression;
            var parameterExpressionVisitor = new ParameterExpressionVisitor(context, children !);

            context.Expression = parameterExpressionVisitor.Visit(context.Expression);

            if (serviceBinding.NeedsDispose == DisposeBehavior.Always || settings.AutoDispose && serviceBinding.NeedsDispose != DisposeBehavior.Never && typeof(IDisposable).IsAssignableFrom(context.Expression.Type))
            {
                MethodInfo           method = Scoped.AddDisposableMethod.MakeGenericMethod(context.Expression.Type);
                MethodCallExpression methodCallExpression = Expression.Call(ScopeParameter, method, context.Expression);
                context.Expression = methodCallExpression;
            }

            if (serviceBinding.Finalizer != null)
            {
                MethodInfo method = Scoped.AddFinalizerMethod.MakeGenericMethod(context.Expression.Type);
                context.Expression = Expression.Call(ScopeParameter, method, context.Expression, Expression.Constant(serviceBinding));
            }

            ApplyCaching(serviceBinding.Lifetime, containerScope, context);
            return(context);
        }
Exemplo n.º 6
0
 private bool ShouldBeAutoDisposed(ServiceBinding serviceBinding, ExpressionContext context, SingularitySettings settings)
 {
     return(serviceBinding.NeedsDispose == ServiceAutoDispose.Always ||
            serviceBinding.NeedsDispose != ServiceAutoDispose.Never && typeof(IDisposable).IsAssignableFrom(context.Expression.Type) && settings.AutoDisposeLifetimes.Contains(serviceBinding.Lifetime.GetType()));
 }
Exemplo n.º 7
0
        public ReadOnlyExpressionContext GenerateBaseExpression(ServiceBinding serviceBinding, InstanceFactory[] children, Scoped containerScope, SingularitySettings settings)
        {
            var context = new ExpressionContext(serviceBinding.Expression);

            context.Expression = serviceBinding.Expression !is LambdaExpression lambdaExpression ? lambdaExpression.Body : serviceBinding.Expression ?? throw new ArgumentNullException(nameof(serviceBinding.Expression));
            var parameterExpressionVisitor = new ParameterExpressionVisitor(context, children !);

            context.Expression = parameterExpressionVisitor.Visit(context.Expression);

            if (ShouldBeAutoDisposed(serviceBinding, context, settings))
            {
                MethodInfo           method = Scoped.AddDisposableMethod.MakeGenericMethod(context.Expression.Type);
                MethodCallExpression methodCallExpression = Expression.Call(ScopeParameter, method, context.Expression);
                context.Expression = methodCallExpression;
            }

            if (serviceBinding.Finalizer != null)
            {
                MethodInfo method = Scoped.AddFinalizerMethod.MakeGenericMethod(context.Expression.Type);
                context.Expression = Expression.Call(ScopeParameter, method, context.Expression, Expression.Constant(serviceBinding));
            }

            serviceBinding.Lifetime.ApplyLifetimeOnExpression(containerScope, context);
            return(context);
        }
Exemplo n.º 8
0
        public Expression GenerateBaseExpression(Binding binding, InstanceFactory[] children, Scoped containerScope, SingularitySettings settings)
        {
            Expression expression = binding.Expression !is LambdaExpression lambdaExpression ? lambdaExpression.Body : binding.Expression;
            var        parameterExpressionVisitor = new ParameterExpressionVisitor(children !);

            expression = parameterExpressionVisitor.Visit(expression);

            if (binding.NeedsDispose == DisposeBehavior.Always || settings.AutoDispose && binding.NeedsDispose != DisposeBehavior.Never && typeof(IDisposable).IsAssignableFrom(expression.Type))
            {
                MethodInfo method = Scoped.AddDisposableMethod.MakeGenericMethod(expression.Type);
                expression = Expression.Call(ScopeParameter, method, expression);
            }

            if (binding.Finalizer != null)
            {
                MethodInfo method = Scoped.AddFinalizerMethod.MakeGenericMethod(expression.Type);
                expression = Expression.Call(ScopeParameter, method, expression, Expression.Constant(binding));
            }

            return(ApplyCaching(binding.Lifetime, containerScope, expression));
        }