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

Sholtee / injector / 2221

01 Dec 2023 07:49AM UTC coverage: 93.276% (-0.6%) from 93.921%
2221

push

appveyor

Sholtee
Merge branch 'v10'

1415 of 1517 relevant lines covered (93.28%)

3.69 hits per line

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

83.33
/SRC/Injector/Public/Extensions/IServiceCollectionAdvancedExtensions/Instance.cs
1
/********************************************************************************
2
* Instance.cs                                                                   *
3
*                                                                               *
4
* Author: Denes Solti                                                           *
5
********************************************************************************/
6
using System;
7

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

12
    public static partial class IServiceCollectionAdvancedExtensions
13
    {
14
        /// <summary>
15
        /// Registers a pre-created instance. Useful to creating "constant" values (e.g. command-line arguments).
16
        /// </summary>
17
        /// <param name="self">The target <see cref="IServiceCollection"/>.</param>
18
        /// <param name="type">The service type to be registered. It can not be null and can be registered only once (with the given <paramref name="key"/>).</param>
19
        /// <param name="key">The (optional) service key (usually a name).</param>
20
        /// <param name="instance">The pre-created instance to be registered. It can not be null and must implement the <paramref name="type"/> interface.</param>
21
        /// <param name="options">Options to be assigned to the service being registered.</param>
22
        public static IServiceCollection Instance(this IServiceCollection self, Type type, object? key, object instance, ServiceOptions? options = null)
23
        {
4✔
24
            if (self is null)
4✔
25
                throw new ArgumentNullException(nameof(self));
4✔
26

27
            if (type is null)
4✔
28
                throw new ArgumentNullException(nameof(type));
×
29

30
            if (instance is null)
4✔
31
                throw new ArgumentNullException(nameof(instance));
4✔
32

33
            return self.Register
4✔
34
            (
35
                //
36
                // Further validations are done by the created InstanceServiceEntry
37
                //
38

39
                Lifetime.Instance.CreateFrom(type, key, instance, options ?? ServiceOptions.Default)
40
            );
41
        }
4✔
42

43
        /// <summary>
44
        /// Registers a pre-created instance. Useful to creating "constant" values (e.g. command-line arguments).
45
        /// </summary>
46
        /// <param name="self">The target <see cref="IServiceCollection"/>.</param>
47
        /// <param name="type">The service type to be registered. It can not be null and can be registered only once.</param>
48
        /// <param name="instance">The pre-created instance to be registered. It can not be null and must implement the <paramref name="type"/> interface.</param>
49
        /// <param name="options">Options to be assigned to the service being registered.</param>
50
        public static IServiceCollection Instance(this IServiceCollection self, Type type, object instance, ServiceOptions? options = null)
51
            => self.Instance(type, null, instance, options);
×
52

53
        /// <summary>
54
        /// Registers a pre-created instance. Useful when creating "constant" values (e.g. from command-line arguments).
55
        /// </summary>
56
        /// <typeparam name="TType">The service type to be registered. It can be registered only once (with the given <paramref name="key"/>).</typeparam>
57
        /// <param name="self">The target <see cref="IServiceCollection"/>.</param>
58
        /// <param name="key">The (optional) service key (usually a name).</param>
59
        /// <param name="instance">The pre-created instance to be registered.</param>
60
        /// <param name="options">Options to be assigned to the service being registered.</param>
61
        public static IServiceCollection Instance<TType>(this IServiceCollection self, object? key, TType instance, ServiceOptions? options = null) where TType: class 
62
            => self.Instance(typeof(TType), key, instance, options);
4✔
63

64
        /// <summary>
65
        /// Registers a pre-created instance. Useful when creating "constant" values (e.g. from command-line arguments).
66
        /// </summary>
67
        /// <typeparam name="TType">The service type to be registered. It can be registered only once.</typeparam>
68
        /// <param name="self">The target <see cref="IServiceCollection"/>.</param>
69
        /// <param name="instance">The pre-created instance to be registered.</param>
70
        /// <param name="options">Options to be assigned to the service being registered.</param>
71
        public static IServiceCollection Instance<TType>(this IServiceCollection self, TType instance, ServiceOptions? options = null) where TType: class
72
            => self.Instance(key: null, instance, options);
4✔
73
    }
74
}
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