Skip to content

Commit 0bfaefa

Browse files
committed
Introduces new node selection
1 parent ea5c314 commit 0bfaefa

3 files changed

Lines changed: 196 additions & 3 deletions

File tree

Orm/Xtensive.Orm/Orm/Domain.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// Copyright (C) 2003-2010 Xtensive LLC.
2-
// All rights reserved.
3-
// For conditions of distribution and use, see license.
1+
// Copyright (C) 2007-2020 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
44
// Created by: Dmitri Maximov
55
// Created: 2007.08.03
66

@@ -15,6 +15,7 @@
1515
using Xtensive.Core;
1616
using Xtensive.IoC;
1717
using Xtensive.Orm.Configuration;
18+
using Xtensive.Orm.Interfaces;
1819
using Xtensive.Orm.Internals;
1920
using Xtensive.Orm.Internals.Prefetch;
2021
using Xtensive.Orm.Linq;
@@ -104,6 +105,25 @@ public static Domain Demand()
104105
/// </summary>
105106
public StorageNodeManager StorageNodeManager { get; private set; }
106107

108+
/// <summary>
109+
/// Selects storage node.
110+
/// </summary>
111+
/// <param name="storageNodeId">Node identifier.</param>
112+
/// <returns>Node selection allowing storage node dependant operations.</returns>
113+
/// /// <sample><code>
114+
/// using (var session = Domain.SelectStorageNode(nodeId).OpenSession()) {
115+
/// // work with persistent objects here.
116+
/// }
117+
/// </code></sample>
118+
/// <exception cref="ArgumentException"><see cref="StorageNode"/> with given identifier does not exist.</exception>
119+
public ISelectedStorageNode SelectStorageNode([NotNull]string storageNodeId)
120+
{
121+
var node = StorageNodeManager.GetNode(storageNodeId);
122+
if (node == null) {
123+
throw new ArgumentException(string.Format(Strings.ExStorageNodeWithIdXIsNotFound, storageNodeId));
124+
}
125+
return new SelectedStorageNode(this, node);
126+
}
107127

108128
#region Private / internal members
109129

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
using System.Threading;
2+
using System.Threading.Tasks;
3+
using Xtensive.Orm;
4+
using Xtensive.Orm.Configuration;
5+
6+
namespace Xtensive.Orm.Interfaces
7+
{
8+
/// <summary>
9+
/// Defindes <see cref="StorageNode"/>-dependant operations.
10+
/// </summary>
11+
public interface ISelectedStorageNode
12+
{
13+
/// <summary>
14+
/// Opens new <see cref="Session"/> with default <see cref="SessionConfiguration"/>.
15+
/// </summary>
16+
/// <returns>
17+
/// New <see cref="Session"/> object.
18+
/// </returns>
19+
/// <sample><code>
20+
/// using (var session = domain.SelectStorageNode(nodeId).OpenSession()) {
21+
/// // work with persistent objects here.
22+
/// }
23+
/// </code></sample>
24+
/// <seealso cref="Session"/>
25+
Session OpenSession();
26+
27+
/// <summary>
28+
/// Opens new <see cref="Session"/> of specified <see cref="SessionType"/>.
29+
/// </summary>
30+
/// <param name="type">The type of session.</param>
31+
/// <returns>
32+
/// New <see cref="Session"/> object.
33+
/// </returns>
34+
/// <sample><code>
35+
/// using (var session = domain.SelectStorageNode(nodeId).OpenSession(sessionType)) {
36+
/// // work with persistent objects here.
37+
/// }
38+
/// </code></sample>
39+
Session OpenSession(SessionType type);
40+
41+
/// <summary>
42+
/// Opens new <see cref="Session"/> with specified <see cref="SessionConfiguration"/>.
43+
/// </summary>
44+
/// <param name="configuration">The session configuration.</param>
45+
/// <returns>
46+
/// New <see cref="Session"/> object.
47+
/// </returns>
48+
/// <sample><code>
49+
/// using (var session = domain.SelectStorageNode(nodeId).OpenSession(configuration)) {
50+
/// // work with persistent objects here
51+
/// }
52+
/// </code></sample>
53+
/// <seealso cref="Session"/>
54+
Session OpenSession(SessionConfiguration configuration);
55+
56+
/// <summary>
57+
/// Asynchronously opens new <see cref="Session"/> with default <see cref="SessionConfiguration"/>.
58+
/// </summary>
59+
/// <returns>A task representing the asynchronous operation.</returns>
60+
/// <sample><code>
61+
/// /// var ctSource = new CancellationTokenSource();
62+
/// using (var session = await domain.SelectStorageNode(nodeId).OpenSessionAsync(ctSource.Token)) {
63+
/// // work with persistent objects here.
64+
/// }
65+
/// </code></sample>
66+
/// <seealso cref="Session"/>
67+
Task<Session> OpenSessionAsync(CancellationToken cancellationToken = default);
68+
69+
/// <summary>
70+
/// Asynchronously opens new <see cref="Session"/> of specified <see cref="SessionType"/> asynchronously.
71+
/// </summary>
72+
/// <param name="type">The type of session.</param>
73+
/// <returns>A task representing the asynchronous operation.</returns>
74+
/// <sample><code>
75+
/// var ctSource = new CancellationTokenSource();
76+
/// using (var session = await domain.SelectStorageNode(nodeId).OpenSessionAsync(sessionType, ctSource.Token)) {
77+
/// // work with persistent objects here.
78+
/// }
79+
/// </code></sample>
80+
Task<Session> OpenSessionAsync(SessionType type, CancellationToken cancellationToken = default);
81+
82+
/// <summary>
83+
/// Opens new <see cref="Session"/> with specified <see cref="SessionConfiguration"/> asynchronously.
84+
/// </summary>
85+
/// <param name="configuration">The session configuration.</param>
86+
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
87+
/// <returns>A task representing the asynchronous operation.</returns>
88+
/// <sample><code>
89+
/// var ctSource = new CancellationTokenSource();
90+
/// using (var session = await domain.SelectStorageNode(nodeId).OpenSessionAsync(configuration, ctSource.Token)) {
91+
/// // work with persistent objects here
92+
/// }
93+
/// </code></sample>
94+
/// <seealso cref="Session"/>
95+
Task<Session> OpenSessionAsync(SessionConfiguration configuration, CancellationToken cancellationToken = default);
96+
}
97+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
using Xtensive.Core;
7+
using Xtensive.Orm;
8+
using Xtensive.Orm.Configuration;
9+
using Xtensive.Orm.Interfaces;
10+
11+
namespace Xtensive.Orm.Internals
12+
{
13+
internal sealed class SelectedStorageNode : ISelectedStorageNode
14+
{
15+
private readonly Domain domain;
16+
private readonly StorageNode storageNode;
17+
18+
/// <inheritdoc/>
19+
public Session OpenSession()
20+
{
21+
return OpenSession(domain.Configuration.Sessions.Default);
22+
}
23+
24+
/// <inheritdoc/>
25+
public Session OpenSession(SessionType type)
26+
{
27+
return type switch {
28+
SessionType.User => OpenSession(domain.Configuration.Sessions.Default),
29+
SessionType.System => OpenSession(domain.Configuration.Sessions.System),
30+
SessionType.KeyGenerator => OpenSession(domain.Configuration.Sessions.KeyGenerator),
31+
SessionType.Service => OpenSession(domain.Configuration.Sessions.Service),
32+
_ => throw new ArgumentOutOfRangeException("type"),
33+
};
34+
}
35+
36+
/// <inheritdoc/>
37+
public Session OpenSession(SessionConfiguration configuration)
38+
{
39+
ArgumentValidator.EnsureArgumentNotNull(configuration, nameof(configuration));
40+
return domain.OpenSessionInternal(configuration, storageNode, configuration.Supports(SessionOptions.AutoActivation));
41+
}
42+
43+
/// <inheritdoc/>
44+
public Task<Session> OpenSessionAsync(CancellationToken cancellationToken = default) =>
45+
OpenSessionAsync(domain.Configuration.Sessions.Default, cancellationToken);
46+
47+
/// <inheritdoc/>
48+
public Task<Session> OpenSessionAsync(SessionType type, CancellationToken cancellationToken = default)
49+
{
50+
return type switch {
51+
SessionType.User => OpenSessionAsync(domain.Configuration.Sessions.Default),
52+
SessionType.System => OpenSessionAsync(domain.Configuration.Sessions.System),
53+
SessionType.KeyGenerator => OpenSessionAsync(domain.Configuration.Sessions.KeyGenerator),
54+
SessionType.Service => OpenSessionAsync(domain.Configuration.Sessions.Service),
55+
_ => throw new ArgumentOutOfRangeException("type"),
56+
};
57+
}
58+
59+
/// <inheritdoc/>
60+
public Task<Session> OpenSessionAsync(SessionConfiguration configuration, CancellationToken cancellationToken = default)
61+
{
62+
ArgumentValidator.EnsureArgumentNotNull(configuration, nameof(configuration));
63+
64+
return domain.OpenSessionInternalAsync(configuration,
65+
storageNode,
66+
configuration.Supports(SessionOptions.AllowSwitching),
67+
cancellationToken);
68+
}
69+
70+
public SelectedStorageNode(Domain domain, StorageNode storageNode)
71+
{
72+
this.domain = domain;
73+
this.storageNode = storageNode;
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)