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

Sholtee / injector / 2253

28 Dec 2023 07:59AM UTC coverage: 90.757% (+0.03%) from 90.723%
2253

push

appveyor

Sholtee
introduce required property support

2170 of 2391 relevant lines covered (90.76%)

0.91 hits per line

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

88.24
/SRC/Injector/Private/Extensions/ServiceEntryExtensions.cs
1
/********************************************************************************
2
* ServiceEntryExtensions.cs                                                     *
3
*                                                                               *
4
* Author: Denes Solti                                                           *
5
********************************************************************************/
6
using System;
7
using System.Collections.Generic;
8
using System.Linq;
9
using System.Linq.Expressions;
10
using System.Reflection;
11

12
namespace Solti.Utils.DI.Internals
13
{
14
    using Interfaces;
15
    using static Properties.Resources;
16

17
    internal static class ServiceEntryExtensions
18
    {
19
        internal static IEnumerable<Expression<DecoratorDelegate>> ResolveDecorators(this AbstractServiceEntry self)
20
        {
1✔
21
            ServiceActivator serviceActivator = new(self.Options);
1✔
22

23
            return self.Implementation is not null
1✔
24
                //
1✔
25
                // Return interceptors targeting the service implementation first
1✔
26
                //
1✔
27

1✔
28
                ? ResolveDecorators(self.Implementation, self.Type)
1✔
29
                : ResolveDecorators(self.Type);
1✔
30

31
            IEnumerable<Expression<DecoratorDelegate>> ResolveDecorators(params Type[] targets)
32
            {
1✔
33
                foreach (Type target in targets)
1✔
34
                {
1✔
35
                    IEnumerable<Expression<CreateInterceptorDelegate>> delegates = target
1✔
36
                        .GetCustomAttributes<AspectAttribute>(inherit: true)
1✔
37
                        .Select
1✔
38
                        (
1✔
39
                            aspect => aspect.Factory ?? serviceActivator.ResolveInterceptorFactory
1✔
40
                            (
1✔
41
                                aspect.Interceptor ?? throw new InvalidOperationException
1✔
42
                                (
1✔
43
                                    string.Format(Culture, NOT_NULL, nameof(aspect.Interceptor))
1✔
44
                                ),
1✔
45
                                aspect.ExplicitArgs
1✔
46
                            )
1✔
47
                        );
1✔
48
                    if (delegates.Any())
1✔
49
                        //
50
                        // Bulk all the aspects into a single decorator
51
                        //
52

53
                        yield return serviceActivator.ResolveProxyDecorator
1✔
54
                        (
1✔
55
                            self.Type,
1✔
56
                            target,
1✔
57
                            delegates
1✔
58
                        );
1✔
59
                }
1✔
60
            }
1✔
61
        }
1✔
62

63
        internal static void ApplyInterceptors(this AbstractServiceEntry self, IEnumerable<(Type Interceptor, object? ExplicitArgs)> interceptors)
64
        {
1✔
65
            ServiceActivator serviceActivator = new(self.Options);
1✔
66

67
            self.Decorate
1✔
68
            (
1✔
69
                serviceActivator.ResolveProxyDecorator
1✔
70
                (
1✔
71
                    self.Type,
1✔
72

1✔
73
                    //
1✔
74
                    // Proxies registered by this way always target the service interface.
1✔
75
                    //
1✔
76

1✔
77
                    self.Type,
1✔
78
                    interceptors.Select
1✔
79
                    (
1✔
80
                        i => serviceActivator.ResolveInterceptorFactory
×
81
                        (
×
82
                            i.Interceptor ?? throw new InvalidOperationException
×
83
                            (
×
84
                                string.Format(Culture, NOT_NULL, nameof(i.Interceptor))
×
85
                            ),
×
86
                            i.ExplicitArgs
×
87
                        )
×
88
                    )
1✔
89
                )
1✔
90
            );
1✔
91
        }
1✔
92

93
        /// <summary>
94
        /// Applies the aspects defined on service interface and implementation even if the <see cref="ServiceEntryFeatures.SupportsAspects"/> is not defined in <see cref="AbstractServiceEntry.Features"/>.
95
        /// </summary>
96
        internal static void ApplyAspects(this AbstractServiceEntry self)  // Since implementation targeting aspects must be registered first, this method can be called only once,
97
        {                                                                  // right after the entry construction -> do NOT expose it!
1✔
98
            foreach (Expression<DecoratorDelegate> decorator in self.ResolveDecorators())
1✔
99
            {
1✔
100
                self.Decorate(decorator);
1✔
101
            }
1✔
102
        }
1✔
103
    }
104
}
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