• 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

83.33
/SRC/Injector/Public/ServiceCollection.cs
1
/********************************************************************************
2
* ServiceCollection.cs                                                          *
3
*                                                                               *
4
* Author: Denes Solti                                                           *
5
********************************************************************************/
6
using System;
7
using System.Collections;
8
using System.Collections.Generic;
9

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

15
    /// <summary>
16
    /// Implements the <see cref="IServiceCollection"/> interface.
17
    /// </summary>
18
    public sealed class ServiceCollection : IServiceCollection
19
    {
20
        private readonly Dictionary<IServiceId, AbstractServiceEntry> FEntries = new(IServiceId.Comparer.Instance);
1✔
21

22
        private void CheckNotReadOnly()
23
        {
1✔
24
            if (IsReadOnly)
1✔
25
                throw new InvalidOperationException(Resources.COLLECTION_IS_READONLY);
1✔
26
        }
1✔
27

28
        /// <summary>
29
        /// Number of entries in this list.
30
        /// </summary>
31
        public int Count => FEntries.Count;
1✔
32

33
        /// <summary>
34
        /// This list is
35
        /// </summary>
36
        public bool IsReadOnly { get; private set; }
1✔
37

38
        /// <summary>
39
        /// Returns true if service overriding is supported.
40
        /// </summary>
41
        public bool SupportsOverride { get; }
1✔
42

43
        /// <summary>
44
        /// Adds a new entry to this collection.
45
        /// </summary>
46
        /// <exception cref="ServiceAlreadyRegisteredException">If <see cref="SupportsOverride"/> is false and this list already contains an entry with the given id</exception>
47
        public void Add(AbstractServiceEntry item)
48
        {
1✔
49
            CheckNotReadOnly();
1✔
50

51
            if (item is null)
1✔
52
                throw new ArgumentNullException(nameof(item));
1✔
53

54
            if (!SupportsOverride && FEntries.ContainsKey(item))
1✔
55
                throw new ServiceAlreadyRegisteredException(Resources.SERVICE_ALREADY_REGISTERED, item);
1✔
56

57
            FEntries[item] = item;
1✔
58
        }
1✔
59

60
        /// <summary>
61
        /// Check entry existance by id.
62
        /// </summary>
63
        public bool Contains(IServiceId id) => FEntries.ContainsKey(id ?? throw new ArgumentNullException(nameof(id)));
1✔
64

65
        /// <summary>
66
        /// Drops entry by id.
67
        /// </summary>
68
        public bool Remove(IServiceId id)
69
        {
1✔
70
            CheckNotReadOnly();
1✔
71
            return FEntries.Remove(id ?? throw new ArgumentNullException(nameof(id)));
1✔
72
        }
1✔
73

74
        /// <summary>
75
        /// Tries to find an entry by id.
76
        /// </summary>
77
        public AbstractServiceEntry? TryFind(IServiceId id)
78
        {
1✔
79
            FEntries.TryGetValue(id ?? throw new ArgumentNullException(nameof(id)), out AbstractServiceEntry? result);
1✔
80
            return result;
1✔
81
        }
1✔
82

83
        /// <summary>
84
        /// Clears this list.
85
        /// </summary>
86
        public void Clear()
87
        {
1✔
88
            CheckNotReadOnly();
1✔
89
            FEntries.Clear();
1✔
90
        }
1✔
91

92
        /// <summary>
93
        /// Check entry existance by reference.
94
        /// </summary>
95
        public bool Contains(AbstractServiceEntry item) =>
96
            FEntries.TryGetValue(item ?? throw new ArgumentNullException(nameof(item)), out AbstractServiceEntry stored) && 
×
97
            item == stored;
×
98

99
        /// <summary>
100
        /// Copies the contained entries to an array.
101
        /// </summary>
102
        public void CopyTo(AbstractServiceEntry[] array, int arrayIndex) => FEntries.Values.CopyTo
×
103
        (
×
104
            array ?? throw new ArgumentNullException(nameof(array)),
×
105
            arrayIndex
×
106
        );
×
107

108
        /// <summary>
109
        /// Drops entry by reference.
110
        /// </summary>
111
        public bool Remove(AbstractServiceEntry item)
112
        {
1✔
113
            CheckNotReadOnly();
1✔
114

115
            if (Contains(item))
1✔
116
            {
1✔
117
                FEntries.Remove(item);
1✔
118
                return true;
1✔
119
            }
120
            return false;
1✔
121
        }
1✔
122

123
        /// <summary>
124
        /// Enumerates the entries in this collection.
125
        /// </summary>
126
        public IEnumerator<AbstractServiceEntry> GetEnumerator() => FEntries.Values.GetEnumerator();
1✔
127

128
        /// <summary>
129
        /// Makes this collection read only.
130
        /// </summary>
131
        public void MakeReadOnly() => IsReadOnly = true;
1✔
132

133
        IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
×
134

135
        /// <summary>
136
        /// Creates a new <see cref="ServiceCollection"/> instance.
137
        /// </summary>
138
        public ServiceCollection(bool supportsOverride = false) => SupportsOverride = supportsOverride;
1✔
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

© 2025 Coveralls, Inc