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

Sholtee / injector / 2208

29 Sep 2023 02:45PM UTC coverage: 93.661% (-0.3%) from 93.921%
2208

push

appveyor

Sholtee
introduce non-string service name support

1374 of 1467 relevant lines covered (93.66%)

2.81 hits per line

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

80.0
/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="iface">The service interface.</param>
20
        /// <param name="name">The (optional) service name.</param>
21
        /// <returns>The service descriptor.</returns>
22
        /// <exception cref="ArgumentNullException">Some of the passed arguments is null.</exception>
23
        /// <remarks>This method uses linear search so should be avoided in perfomance critical places.</remarks>
24
        public static AbstractServiceEntry? TryFind(this IServiceCollection self, Type iface, object? name)
25
        {
3✔
26
            if (self is null)
3✔
27
                throw new ArgumentNullException(nameof(self));
×
28

29
            if (iface is null)
3✔
30
                throw new ArgumentNullException(nameof(iface));
×
31

32
            return self.SingleOrDefault(svc => svc.Interface == iface && svc.Name == name);
3✔
33
        }
3✔
34

35
        /// <summary>
36
        /// Tries to find a service descriptor (<see cref="AbstractServiceEntry"/>) in the given collection.
37
        /// </summary>
38
        /// <param name="self">The target <see cref="IServiceCollection"/>.</param>
39
        /// <param name="iface">The service interface.</param>
40
        /// <returns>The service descriptor.</returns>
41
        /// <exception cref="ArgumentNullException">Some of the passed arguments is null.</exception>
42
        /// <remarks>This method uses linear search so should be avoided in perfomance critical places.</remarks>
43
        public static AbstractServiceEntry? TryFind(this IServiceCollection self, Type iface) =>
44
            self.TryFind(iface, null);
×
45

46
        /// <summary>
47
        /// Tries to find a service descriptor (<see cref="AbstractServiceEntry"/>) in the given collection.
48
        /// </summary>
49
        /// <param name="self">The target <see cref="IServiceCollection"/>.</param>
50
        /// <param name="name">The (optional) service name.</param>
51
        /// <returns>The service descriptor.</returns>
52
        /// <exception cref="ArgumentNullException">Some of the passed arguments is null.</exception>
53
        /// <remarks>This method uses linear search so should be avoided in perfomance critical places.</remarks>
54
        public static AbstractServiceEntry? TryFind<TInterface>(this IServiceCollection self, object? name) where TInterface : class =>
55
            self.TryFind(typeof(TInterface), name);
×
56

57
        /// <summary>
58
        /// Tries to find a service descriptor (<see cref="AbstractServiceEntry"/>) in the given collection.
59
        /// </summary>
60
        /// <param name="self">The target <see cref="IServiceCollection"/>.</param>
61
        /// <returns>The service descriptor.</returns>
62
        /// <exception cref="ArgumentNullException">Some of the passed arguments is null.</exception>
63
        /// <exception cref="ServiceNotFoundException">When the requested service could not be found.</exception>
64
        /// <remarks>This method uses linear search so should be avoided in perfomance critical places.</remarks>
65
        public static AbstractServiceEntry? TryFind<TInterface>(this IServiceCollection self) where TInterface : class =>
66
            self.TryFind<TInterface>(null);
×
67

68
        /// <summary>
69
        /// Tries to find a service descriptor (<see cref="AbstractServiceEntry"/>) in the given collection.
70
        /// </summary>
71
        /// <param name="self">The target <see cref="IServiceCollection"/>.</param>
72
        /// <param name="iface">The service interface.</param>
73
        /// <param name="name">The (optional) service name.</param>
74
        /// <returns>The service descriptor.</returns>
75
        /// <exception cref="ArgumentNullException">Some of the passed arguments is null.</exception>
76
        /// <exception cref="ServiceNotFoundException">The requested service could not be found.</exception>
77
        /// <remarks>This method uses linear search so should be avoided in perfomance critical places.</remarks>
78
        public static AbstractServiceEntry Find(this IServiceCollection self, Type iface, object? name)
79
        {
3✔
80
            if (self is null)
3✔
81
                throw new ArgumentNullException(nameof(self));
3✔
82

83
            if (iface is null)
3✔
84
                throw new ArgumentNullException(nameof(iface));
3✔
85

86
            AbstractServiceEntry? entry = self.TryFind(iface, name);
3✔
87
            if (entry is null)
3✔
88
            {
3✔
89
                MissingServiceEntry missingService = new(iface, name);
3✔
90
                throw new ServiceNotFoundException
3✔
91
                (
92
                    string.Format
93
                    (
94
                        Resources.Culture,
95
                        Resources.SERVICE_NOT_FOUND,
96
                        missingService.ToString(shortForm: true)
97
                    ),
98
                    null,
99
                    missingService
100
                );
101
            }
102
            return entry;
3✔
103
        }
3✔
104

105
        /// <summary>
106
        /// Tries to find a service descriptor (<see cref="AbstractServiceEntry"/>) in the given collection.
107
        /// </summary>
108
        /// <param name="self">The target <see cref="IServiceCollection"/>.</param>
109
        /// <param name="iface">The service interface.</param>
110
        /// <returns>The service descriptor.</returns>
111
        /// <exception cref="ArgumentNullException">Some of the passed arguments is null.</exception>
112
        /// <exception cref="ServiceNotFoundException">When the requested service could not be found.</exception>
113
        /// <remarks>This method uses linear search so should be avoided in perfomance critical places.</remarks>
114
        public static AbstractServiceEntry Find(this IServiceCollection self, Type iface) =>
115
            self.Find(iface, null);
3✔
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
        /// <param name="name">The (optional) service name.</param>
122
        /// <returns>The service descriptor.</returns>
123
        /// <exception cref="ArgumentNullException">Some of the passed arguments is null.</exception>
124
        /// <exception cref="ServiceNotFoundException">When the requested service could not be found.</exception>
125
        /// <remarks>This method uses linear search so should be avoided in perfomance critical places.</remarks>
126
        public static AbstractServiceEntry Find<TInterface>(this IServiceCollection self, object? name) where TInterface : class =>
127
            self.Find(typeof(TInterface), name);
3✔
128

129
        /// <summary>
130
        /// Tries to find a service descriptor (<see cref="AbstractServiceEntry"/>) in the given collection.
131
        /// </summary>
132
        /// <param name="self">The target <see cref="IServiceCollection"/>.</param>
133
        /// <returns>The service descriptor.</returns>
134
        /// <exception cref="ArgumentNullException">Some of the passed arguments is null.</exception>
135
        /// <exception cref="ServiceNotFoundException">When the requested service could not be found.</exception>
136
        /// <remarks>This method uses linear search so should be avoided in perfomance critical places.</remarks>
137
        public static AbstractServiceEntry Find<TInterface>(this IServiceCollection self) where TInterface : class =>
138
            self.Find<TInterface>(null);
3✔
139
    }
140
}
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