• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Sholtee / injector / 2251

27 Dec 2023 10:00AM UTC coverage: 90.723% (-2.0%) from 92.704%
2251

push

appveyor

Sholtee
Merge branch 'drop_opencover'

2171 of 2393 relevant lines covered (90.72%)

0.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

57.14
/SRC/Injector/Private/Expressions/DependencyResolvers/LazyDependencyResolver.cs
1
/********************************************************************************
2
* LazyDependencyResolver.cs                                                     *
3
*                                                                               *
4
* Author: Denes Solti                                                           *
5
********************************************************************************/
6
using System;
7
using System.Linq.Expressions;
8
using System.Reflection;
9

10
namespace Solti.Utils.DI.Internals
11
{
12
    using Interfaces;
13
    using Primitives;
14

15
    internal sealed class LazyDependencyResolver : RegularLazyDependencyResolver
16
    {
17
        protected override MethodInfo CreateLazy { get; } = MethodInfoExtractor.Extract(static () => CreateLazyImpl<object>(null!, null)).GetGenericMethodDefinition();
1✔
18

19
        protected override MethodInfo CreateLazyOpt { get; } = MethodInfoExtractor.Extract(static () => CreateLazyOptImpl<object>(null!, null)).GetGenericMethodDefinition();
1✔
20

21
        private static class Factories<TService>
22
        {
23
            //
24
            // Converting methods to delegates may take a while, so do it only once
25
            //
26

27
            public static readonly Func<(IInjector Injector, string? Name), TService> Factory =
1✔
28
                static ctx => (TService) ctx.Injector.Get(typeof(TService), ctx.Name);
1✔
29
            public static readonly Func<(IInjector Injector, string? Name), TService> FactoryOpt =
1✔
30
                static ctx => (TService) ctx.Injector.TryGet(typeof(TService), ctx.Name)!;
×
31
        }
32

33
        private static ILazy<TService> CreateLazyImpl<TService>(IInjector injector, string? name) =>
34
            new LazyHavingContext<TService, (IInjector, string?)>
1✔
35
            (
1✔
36
                Factories<TService>.Factory,
1✔
37
                (injector, name)
1✔
38
            );
1✔
39

40
        private static ILazy<TService> CreateLazyOptImpl<TService>(IInjector injector, string? name) =>
41
            new LazyHavingContext<TService, (IInjector, string?)>
×
42
            (
×
43
                Factories<TService>.FactoryOpt,
×
44
                (injector, name)
×
45
            );
×
46

47
        public override object Id { get; } = nameof(LazyDependencyResolver);
1✔
48

49
        public override Expression Resolve(ParameterExpression injector, DependencyDescriptor dependency, object? userData, object? context, CallNextDelegate<object?, Expression> next)
50
        {
1✔
51
            Type? type = ParseDependency(dependency, typeof(ILazy<>));
1✔
52
            if (type is null)
1✔
53
                return next(context);
1✔
54

55
            return ResolveLazyService
×
56
            (
×
57
                injector,
×
58
                type,
×
59
                dependency.Options
×
60
            );
×
61
        }
1✔
62
    }
63
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc