Skip to content

Commit cf53ce9

Browse files
committed
StorageNode and Domain become ISessionSource-es
1 parent be9dbc1 commit cf53ce9

6 files changed

Lines changed: 104 additions & 178 deletions

File tree

Orm/Xtensive.Orm/Orm/Domain.cs

Lines changed: 6 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace Xtensive.Orm
3535
/// <code lang="cs" source="..\Xtensive.Orm\Xtensive.Orm.Manual\DomainAndSession\DomainAndSessionSample.cs" region="Domain sample"></code>
3636
/// </sample>
3737
[UsedImplicitly(ImplicitUseTargetFlags.WithMembers)]
38-
public sealed class Domain : IDisposable, IHasExtensions
38+
public sealed class Domain : IDisposable, IHasExtensions, ISessionSource
3939
{
4040
private readonly object disposeGuard = new object();
4141
private readonly object singleConnectionGuard = new object();
@@ -116,13 +116,13 @@ public static Domain Demand()
116116
/// }
117117
/// </code></sample>
118118
/// <exception cref="ArgumentException"><see cref="StorageNode"/> with given identifier does not exist.</exception>
119-
public ISelectedStorageNode SelectStorageNode([NotNull]string storageNodeId)
119+
public ISessionSource SelectStorageNode([NotNull]string storageNodeId)
120120
{
121121
var node = StorageNodeManager.GetNode(storageNodeId);
122122
if (node == null) {
123123
throw new ArgumentException(string.Format(Strings.ExStorageNodeWithIdXIsNotFound, storageNodeId));
124124
}
125-
return new SelectedStorageNode(this, node);
125+
return node;
126126
}
127127

128128
#region Private / internal members
@@ -284,22 +284,6 @@ internal Session OpenSessionInternal(SessionConfiguration configuration, Storage
284284
return session;
285285
}
286286

287-
/// <summary>
288-
/// Opens new <see cref="Session"/> with default <see cref="SessionConfiguration"/> asynchronously.
289-
/// </summary>
290-
/// <returns>A task representing the asynchronous operation.</returns>
291-
/// <sample><code>
292-
/// using (var session = await Domain.OpenSessionAsync()) {
293-
/// // work with persistent objects here.
294-
/// }
295-
/// </code></sample>
296-
/// <seealso cref="Session"/>
297-
public Task<Session> OpenSessionAsync()
298-
{
299-
var configuration = Configuration.Sessions.Default;
300-
return OpenSessionAsync(configuration, CancellationToken.None);
301-
}
302-
303287
/// <summary>
304288
/// Opens new <see cref="Session"/> with default <see cref="SessionConfiguration"/> asynchronously.
305289
/// </summary>
@@ -312,27 +296,12 @@ public Task<Session> OpenSessionAsync()
312296
/// }
313297
/// </code></sample>
314298
/// <seealso cref="Session"/>
315-
public Task<Session> OpenSessionAsync(CancellationToken cancellationToken)
299+
public Task<Session> OpenSessionAsync(CancellationToken cancellationToken = default)
316300
{
317301
var configuration = Configuration.Sessions.Default;
318302
return OpenSessionAsync(configuration, cancellationToken);
319303
}
320304

321-
/// <summary>
322-
/// Opens new <see cref="Session"/> of specified <see cref="SessionType"/> asynchronously.
323-
/// </summary>
324-
/// <param name="type">The type of session.</param>
325-
/// <returns>A task representing the asynchronous operation.</returns>
326-
/// <sample><code>
327-
/// using (var session = await domain.OpenSessionAsync(sessionType)) {
328-
/// // work with persistent objects here.
329-
/// }
330-
/// </code></sample>
331-
public Task<Session> OpenSessionAsync(SessionType type)
332-
{
333-
return OpenSessionAsync(type, CancellationToken.None);
334-
}
335-
336305
/// <summary>
337306
/// Opens new <see cref="Session"/> of specified <see cref="SessionType"/> asynchronously.
338307
/// </summary>
@@ -345,7 +314,7 @@ public Task<Session> OpenSessionAsync(SessionType type)
345314
/// // work with persistent objects here.
346315
/// }
347316
/// </code></sample>
348-
public Task<Session> OpenSessionAsync(SessionType type, CancellationToken cancellationToken)
317+
public Task<Session> OpenSessionAsync(SessionType type, CancellationToken cancellationToken = default)
349318
{
350319
cancellationToken.ThrowIfCancellationRequested();
351320
switch (type) {
@@ -362,22 +331,6 @@ public Task<Session> OpenSessionAsync(SessionType type, CancellationToken cancel
362331
}
363332
}
364333

365-
/// <summary>
366-
/// Opens new <see cref="Session"/> with specified <see cref="SessionConfiguration"/> asynchronously.
367-
/// </summary>
368-
/// <param name="configuration">The session configuration.</param>
369-
/// <returns>A task representing the asynchronous operation.</returns>
370-
/// <sample><code>
371-
/// using (var session = await domain.OpenSessionAsync(configuration)) {
372-
/// // work with persistent objects here
373-
/// }
374-
/// </code></sample>
375-
/// <seealso cref="Session"/>
376-
public Task<Session> OpenSessionAsync(SessionConfiguration configuration)
377-
{
378-
return OpenSessionAsync(configuration, CancellationToken.None);
379-
}
380-
381334
/// <summary>
382335
/// Opens new <see cref="Session"/> with specified <see cref="SessionConfiguration"/> asynchronously.
383336
/// </summary>
@@ -391,7 +344,7 @@ public Task<Session> OpenSessionAsync(SessionConfiguration configuration)
391344
/// }
392345
/// </code></sample>
393346
/// <seealso cref="Session"/>
394-
public Task<Session> OpenSessionAsync(SessionConfiguration configuration, CancellationToken cancellationToken)
347+
public Task<Session> OpenSessionAsync(SessionConfiguration configuration, CancellationToken cancellationToken = default)
395348
{
396349
return OpenSessionInternalAsync(configuration, null,
397350
configuration.Supports(SessionOptions.AutoActivation), cancellationToken);

Orm/Xtensive.Orm/Orm/Interfaces/ISelectedStorageNode.cs renamed to Orm/Xtensive.Orm/Orm/Interfaces/ISessionSource.cs

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,16 @@
66
namespace Xtensive.Orm.Interfaces
77
{
88
/// <summary>
9-
/// Defines <see cref="StorageNode"/>-dependant operations.
9+
/// Contract for types being able to open <see cref="Session"/>.
1010
/// </summary>
11-
public interface ISelectedStorageNode
11+
public interface ISessionSource
1212
{
1313
/// <summary>
1414
/// Opens new <see cref="Session"/> with default <see cref="SessionConfiguration"/>.
1515
/// </summary>
1616
/// <returns>
1717
/// New <see cref="Session"/> object.
1818
/// </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"/>
2519
Session OpenSession();
2620

2721
/// <summary>
@@ -31,11 +25,6 @@ public interface ISelectedStorageNode
3125
/// <returns>
3226
/// New <see cref="Session"/> object.
3327
/// </returns>
34-
/// <sample><code>
35-
/// using (var session = domain.SelectStorageNode(nodeId).OpenSession(sessionType)) {
36-
/// // work with persistent objects here.
37-
/// }
38-
/// </code></sample>
3928
Session OpenSession(SessionType type);
4029

4130
/// <summary>
@@ -45,38 +34,20 @@ public interface ISelectedStorageNode
4534
/// <returns>
4635
/// New <see cref="Session"/> object.
4736
/// </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"/>
5437
Session OpenSession(SessionConfiguration configuration);
5538

5639
/// <summary>
5740
/// Asynchronously opens new <see cref="Session"/> with default <see cref="SessionConfiguration"/>.
5841
/// </summary>
5942
/// <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"/>
6743
Task<Session> OpenSessionAsync(CancellationToken cancellationToken = default);
6844

6945
/// <summary>
7046
/// Asynchronously opens new <see cref="Session"/> of specified <see cref="SessionType"/> asynchronously.
7147
/// </summary>
7248
/// <param name="type">The type of session.</param>
49+
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
7350
/// <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>
8051
Task<Session> OpenSessionAsync(SessionType type, CancellationToken cancellationToken = default);
8152

8253
/// <summary>
@@ -85,13 +56,6 @@ public interface ISelectedStorageNode
8556
/// <param name="configuration">The session configuration.</param>
8657
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
8758
/// <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"/>
9559
Task<Session> OpenSessionAsync(SessionConfiguration configuration, CancellationToken cancellationToken = default);
9660
}
9761
}

Orm/Xtensive.Orm/Orm/Internals/SelectedStorageNode.cs

Lines changed: 0 additions & 76 deletions
This file was deleted.

Orm/Xtensive.Orm/Orm/StorageNode.cs

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
// Copyright (C) 2014 Xtensive LLC.
1+
// Copyright (C) 2014 Xtensive LLC.
22
// All rights reserved.
33
// For conditions of distribution and use, see license.
44
// Created by: Denis Krjuchkov
55
// Created: 2014.03.13
66

7+
using System;
78
using System.Collections.Concurrent;
89
using System.Collections.Generic;
10+
using System.Threading;
11+
using System.Threading.Tasks;
912
using Xtensive.Core;
1013
using Xtensive.Orm.Configuration;
14+
using Xtensive.Orm.Interfaces;
1115
using Xtensive.Orm.Model;
1216
using Xtensive.Orm.Providers;
1317

@@ -16,8 +20,10 @@ namespace Xtensive.Orm
1620
/// <summary>
1721
/// Storage node.
1822
/// </summary>
19-
public sealed class StorageNode
23+
public sealed class StorageNode : ISessionSource
2024
{
25+
private readonly Domain domain;
26+
2127
/// <summary>
2228
/// Gets node identifier.
2329
/// </summary>
@@ -44,15 +50,68 @@ public sealed class StorageNode
4450

4551
internal ConcurrentDictionary<PersistRequestBuilderTask, ICollection<PersistRequest>> PersistRequestCache { get; private set; }
4652

53+
/// <inheritdoc/>
54+
public Session OpenSession()
55+
{
56+
return OpenSession(domain.Configuration.Sessions.Default);
57+
}
58+
59+
/// <inheritdoc/>
60+
public Session OpenSession(SessionType type)
61+
{
62+
return type switch {
63+
SessionType.User => OpenSession(domain.Configuration.Sessions.Default),
64+
SessionType.System => OpenSession(domain.Configuration.Sessions.System),
65+
SessionType.KeyGenerator => OpenSession(domain.Configuration.Sessions.KeyGenerator),
66+
SessionType.Service => OpenSession(domain.Configuration.Sessions.Service),
67+
_ => throw new ArgumentOutOfRangeException("type"),
68+
};
69+
}
70+
71+
/// <inheritdoc/>
72+
public Session OpenSession(SessionConfiguration configuration)
73+
{
74+
ArgumentValidator.EnsureArgumentNotNull(configuration, nameof(configuration));
75+
return domain.OpenSessionInternal(configuration, this, configuration.Supports(SessionOptions.AutoActivation));
76+
}
77+
78+
/// <inheritdoc/>
79+
public Task<Session> OpenSessionAsync(CancellationToken cancellationToken = default) =>
80+
OpenSessionAsync(domain.Configuration.Sessions.Default, cancellationToken);
81+
82+
/// <inheritdoc/>
83+
public Task<Session> OpenSessionAsync(SessionType type, CancellationToken cancellationToken = default)
84+
{
85+
return type switch {
86+
SessionType.User => OpenSessionAsync(domain.Configuration.Sessions.Default),
87+
SessionType.System => OpenSessionAsync(domain.Configuration.Sessions.System),
88+
SessionType.KeyGenerator => OpenSessionAsync(domain.Configuration.Sessions.KeyGenerator),
89+
SessionType.Service => OpenSessionAsync(domain.Configuration.Sessions.Service),
90+
_ => throw new ArgumentOutOfRangeException("type"),
91+
};
92+
}
93+
94+
/// <inheritdoc/>
95+
public Task<Session> OpenSessionAsync(SessionConfiguration configuration, CancellationToken cancellationToken = default)
96+
{
97+
ArgumentValidator.EnsureArgumentNotNull(configuration, nameof(configuration));
98+
99+
return domain.OpenSessionInternalAsync(configuration,
100+
this,
101+
configuration.Supports(SessionOptions.AllowSwitching),
102+
cancellationToken);
103+
}
47104

48105
// Constructors
49106

50-
internal StorageNode(NodeConfiguration configuration, ModelMapping mapping, TypeIdRegistry typeIdRegistry)
107+
internal StorageNode(Domain domain, NodeConfiguration configuration, ModelMapping mapping, TypeIdRegistry typeIdRegistry)
51108
{
52-
ArgumentValidator.EnsureArgumentNotNull(configuration, "configuration");
53-
ArgumentValidator.EnsureArgumentNotNull(mapping, "mapping");
54-
ArgumentValidator.EnsureArgumentNotNull(typeIdRegistry, "typeIdRegistry");
109+
ArgumentValidator.EnsureArgumentNotNull(domain, nameof(domain));
110+
ArgumentValidator.EnsureArgumentNotNull(configuration, nameof(configuration));
111+
ArgumentValidator.EnsureArgumentNotNull(mapping, nameof(mapping));
112+
ArgumentValidator.EnsureArgumentNotNull(typeIdRegistry, nameof(typeIdRegistry));
55113

114+
this.domain = domain;
56115
Configuration = configuration;
57116
Mapping = mapping;
58117
TypeIdRegistry = typeIdRegistry;

0 commit comments

Comments
 (0)