-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathObjectContainerConfigurationDefinitions.cs
More file actions
75 lines (68 loc) · 3.38 KB
/
ObjectContainerConfigurationDefinitions.cs
File metadata and controls
75 lines (68 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// =================================================================================================================================
// Copyright (c) RapidField LLC. Licensed under the MIT License. See LICENSE.txt in the project root for license information.
// =================================================================================================================================
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
namespace RapidField.SolidInstruments.ObjectComposition
{
/// <summary>
/// Represents a collection of definitions that control the behavior of an <see cref="ObjectContainer" /> when resolving
/// requested objects.
/// </summary>
/// <remarks>
/// <see cref="ObjectContainerConfigurationDefinitions" /> is the default implementation of
/// <see cref="IObjectContainerConfigurationDefinitions" />.
/// </remarks>
public sealed class ObjectContainerConfigurationDefinitions : IObjectContainerConfigurationDefinitions
{
/// <summary>
/// Initializes a new instance of the <see cref="ObjectContainerConfigurationDefinitions" /> class.
/// </summary>
[DebuggerHidden]
internal ObjectContainerConfigurationDefinitions()
{
Registrations = new ConcurrentDictionary<Type, IObjectContainerDefinition>();
}
/// <summary>
/// Registers the specified product type with the associated <see cref="IObjectContainer" />.
/// </summary>
/// <typeparam name="TRequest">
/// The request type that identifies the registration.
/// </typeparam>
/// <typeparam name="TProduct">
/// The type that is produced by the container as a result of a request for <typeparamref name="TRequest" />.
/// </typeparam>
/// <exception cref="ArgumentException">
/// A definition already exists for <typeparamref name="TRequest" />.
/// </exception>
public IObjectContainerConfigurationDefinitions Add<TRequest, TProduct>()
where TRequest : class
where TProduct : class, TRequest
{
var requestType = typeof(TRequest);
var productType = typeof(TProduct);
if (Registrations.TryAdd(requestType, new ObjectContainerDefinition(requestType, productType)))
{
return this;
}
throw new ArgumentException($"A definition already exists for the specified request type, {requestType.FullName}.", nameof(TRequest));
}
/// <summary>
/// Registers the specified product type with the associated <see cref="IObjectContainer" />.
/// </summary>
/// <typeparam name="TProduct">
/// The type that is registered for production by the container.
/// </typeparam>
/// <exception cref="ArgumentException">
/// A definition already exists for <typeparamref name="TProduct" />.
/// </exception>
public IObjectContainerConfigurationDefinitions Add<TProduct>()
where TProduct : class => Add<TProduct, TProduct>();
/// <summary>
/// Represents a collection of request-product type pairs that constitute the definitions.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
internal readonly ConcurrentDictionary<Type, IObjectContainerDefinition> Registrations;
}
}