• 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

93.55
/SRC/Interfaces/Public/Extensions/IServiceCollectionBasicExtensions/Find.cs
1
/********************************************************************************
2
* Find.cs                                                                       *
3
*                                                                               *
4
* Author: Denes Solti                                                           *
5
********************************************************************************/
6
using System;
7
using System.Linq;
8

9
namespace Solti.Utils.DI.Interfaces
10
{
11
    using Properties;
12

13
    public static partial class IServiceCollectionBasicExtensions
14
    {
15
        /// <summary>
16
        /// Tries to find a service descriptor (<see cref="AbstractServiceEntry"/>) in the given collection.
17
        /// </summary>
18
        /// <param name="self">The target <see cref="IServiceCollection"/>.</param>
19
        /// <param name="type">The service type.</param>
20
        /// <param name="key">The (optional) service key (usually a name).</param>
21
        /// <returns>The service descriptor.</returns>
22
        /// <exception cref="ArgumentNullException">Some of the passed arguments is null.</exception>
23
        public static AbstractServiceEntry? TryFind(this IServiceCollection self, Type type, object? key)
24
        {
1✔
25
            if (self is null)
1✔
26
                throw new ArgumentNullException(nameof(self));
1✔
27

28
            if (type is null)
1✔
29
                throw new ArgumentNullException(nameof(type));
1✔
30

31
            return self.TryFind(new ServiceId(type, key));
1✔
32
        }
1✔
33

34
        /// <summary>
35
        /// Tries to find a service descriptor (<see cref="AbstractServiceEntry"/>) in the given collection.
36
        /// </summary>
37
        /// <param name="self">The target <see cref="IServiceCollection"/>.</param>
38
        /// <param name="type">The service type.</param>
39
        /// <returns>The service descriptor.</returns>
40
        /// <exception cref="ArgumentNullException">Some of the passed arguments is null.</exception>
41
        public static AbstractServiceEntry? TryFind(this IServiceCollection self, Type type) =>
42
            self.TryFind(type, null);
×
43

44
        /// <summary>
45
        /// Tries to find a service descriptor (<see cref="AbstractServiceEntry"/>) in the given collection.
46
        /// </summary>
47
        /// <param name="self">The target <see cref="IServiceCollection"/>.</param>
48
        /// <param name="key">The (optional) service key (usually a name).</param>
49
        /// <returns>The service descriptor.</returns>
50
        /// <exception cref="ArgumentNullException">Some of the passed arguments is null.</exception>
51
        public static AbstractServiceEntry? TryFind<TType>(this IServiceCollection self, object? key) where TType : class =>
52
            self.TryFind(typeof(TType), key);
1✔
53

54
        /// <summary>
55
        /// Tries to find a service descriptor (<see cref="AbstractServiceEntry"/>) in the given collection.
56
        /// </summary>
57
        /// <param name="self">The target <see cref="IServiceCollection"/>.</param>
58
        /// <returns>The service descriptor.</returns>
59
        /// <exception cref="ArgumentNullException">Some of the passed arguments is null.</exception>
60
        /// <exception cref="ServiceNotFoundException">When the requested service could not be found.</exception>
61
        public static AbstractServiceEntry? TryFind<TType>(this IServiceCollection self) where TType : class =>
62
            self.TryFind<TType>(null);
1✔
63

64
        /// <summary>
65
        /// Tries to find a service descriptor (<see cref="AbstractServiceEntry"/>) in the given collection.
66
        /// </summary>
67
        /// <param name="self">The target <see cref="IServiceCollection"/>.</param>
68
        /// <param name="type">The service type.</param>
69
        /// <param name="key">The (optional) service key (usually a name).</param>
70
        /// <returns>The service descriptor.</returns>
71
        /// <exception cref="ArgumentNullException">Some of the passed arguments is null.</exception>
72
        /// <exception cref="ServiceNotFoundException">The requested service could not be found.</exception>
73
        public static AbstractServiceEntry Find(this IServiceCollection self, Type type, object? key)
74
        {
1✔
75
            if (self is null)
1✔
76
                throw new ArgumentNullException(nameof(self));
1✔
77

78
            if (type is null)
1✔
79
                throw new ArgumentNullException(nameof(type));
1✔
80

81
            ServiceId serviceId = new(type, key);
1✔
82
            return self.TryFind(serviceId) ??  throw new ServiceNotFoundException
1✔
83
            (
1✔
84
                string.Format
1✔
85
                (
1✔
86
                    Resources.Culture,
1✔
87
                    Resources.SERVICE_NOT_FOUND,
1✔
88
                    serviceId
1✔
89
                ),
1✔
90
                null,
1✔
91
                serviceId
1✔
92
            );
1✔
93
        }
1✔
94

95
        /// <summary>
96
        /// Tries to find a service descriptor (<see cref="AbstractServiceEntry"/>) in the given collection.
97
        /// </summary>
98
        /// <param name="self">The target <see cref="IServiceCollection"/>.</param>
99
        /// <param name="type">The service type.</param>
100
        /// <returns>The service descriptor.</returns>
101
        /// <exception cref="ArgumentNullException">Some of the passed arguments is null.</exception>
102
        /// <exception cref="ServiceNotFoundException">When the requested service could not be found.</exception>
103
        public static AbstractServiceEntry Find(this IServiceCollection self, Type type) =>
104
            self.Find(type, null);
×
105

106
        /// <summary>
107
        /// Tries to find a service descriptor (<see cref="AbstractServiceEntry"/>) in the given collection.
108
        /// </summary>
109
        /// <param name="self">The target <see cref="IServiceCollection"/>.</param>
110
        /// <param name="key">The (optional) service key (usually a name).</param>
111
        /// <returns>The service descriptor.</returns>
112
        /// <exception cref="ArgumentNullException">Some of the passed arguments is null.</exception>
113
        /// <exception cref="ServiceNotFoundException">When the requested service could not be found.</exception>
114
        public static AbstractServiceEntry Find<TType>(this IServiceCollection self, object? key) where TType : class =>
115
            self.Find(typeof(TType), key);
1✔
116

117
        /// <summary>
118
        /// Tries to find a service descriptor (<see cref="AbstractServiceEntry"/>) in the given collection.
119
        /// </summary>
120
        /// <param name="self">The target <see cref="IServiceCollection"/>.</param>
121
        /// <returns>The service descriptor.</returns>
122
        /// <exception cref="ArgumentNullException">Some of the passed arguments is null.</exception>
123
        /// <exception cref="ServiceNotFoundException">When the requested service could not be found.</exception>
124
        public static AbstractServiceEntry Find<TType>(this IServiceCollection self) where TType : class =>
125
            self.Find<TType>(null);
1✔
126
    }
127
}
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

© 2026 Coveralls, Inc