-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathServiceScopeFactory.cs
More file actions
58 lines (54 loc) · 2.26 KB
/
ServiceScopeFactory.cs
File metadata and controls
58 lines (54 loc) · 2.26 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
// =================================================================================================================================
// Copyright (c) RapidField LLC. Licensed under the MIT License. See LICENSE.txt in the project root for license information.
// =================================================================================================================================
using Microsoft.Extensions.DependencyInjection;
using RapidField.SolidInstruments.Core.ArgumentValidation;
using System;
using System.Diagnostics;
namespace RapidField.SolidInstruments.InversionOfControl
{
/// <summary>
/// Represents a factory that produces <see cref="IServiceScope" /> instances.
/// </summary>
public sealed class ServiceScopeFactory : IServiceScopeFactory
{
/// <summary>
/// Initializes a new instance of the <see cref="ServiceScopeFactory" /> class.
/// </summary>
/// <param name="dependencyScope">
/// The scope that is abstracted by the service scope.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="dependencyScope" /> is <see langword="null" />.
/// </exception>
public ServiceScopeFactory(IDependencyScope dependencyScope)
{
DependencyScope = dependencyScope.RejectIf().IsNull(nameof(dependencyScope)).TargetArgument;
}
/// <summary>
/// Creates a new service scope.
/// </summary>
/// <returns>
/// A new service scope.
/// </returns>
/// <exception cref="CreateDependencyScopeException">
/// An exception was raised while attempting to create the scope.
/// </exception>
public IServiceScope CreateScope()
{
try
{
return new ServiceScope(DependencyScope.CreateChildScope());
}
catch (ObjectDisposedException exception)
{
throw new CreateDependencyScopeException(exception);
}
}
/// <summary>
/// Represents the parent scope that produces the derivative scopes.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly IDependencyScope DependencyScope;
}
}