Skip to content

Commit c878c64

Browse files
authored
Merge pull request #227 from servicetitan/upstream/avoid_DoubleNullChecking
Avoid double null checking; Refactor OpenSession[Async] methods
2 parents 569851f + c416cd5 commit c878c64

File tree

2 files changed

+26
-74
lines changed

2 files changed

+26
-74
lines changed

Orm/Xtensive.Orm/Orm/Domain.cs

Lines changed: 18 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,8 @@ private void NotifyDisposing()
187187
/// }
188188
/// </code></sample>
189189
/// <seealso cref="Session"/>
190-
public Session OpenSession()
191-
{
192-
var configuration = Configuration.Sessions.Default;
193-
return OpenSession(configuration);
194-
}
190+
public Session OpenSession() =>
191+
OpenSession(Configuration.Sessions.Default);
195192

196193
/// <summary>
197194
/// Opens new <see cref="Session"/> of specified <see cref="SessionType"/>.
@@ -205,21 +202,8 @@ public Session OpenSession()
205202
/// // work with persistent objects here.
206203
/// }
207204
/// </code></sample>
208-
public Session OpenSession(SessionType type)
209-
{
210-
switch (type) {
211-
case SessionType.User:
212-
return OpenSession(Configuration.Sessions.Default);
213-
case SessionType.System:
214-
return OpenSession(Configuration.Sessions.System);
215-
case SessionType.KeyGenerator:
216-
return OpenSession(Configuration.Sessions.KeyGenerator);
217-
case SessionType.Service:
218-
return OpenSession(Configuration.Sessions.Service);
219-
default:
220-
throw new ArgumentOutOfRangeException("type");
221-
}
222-
}
205+
public Session OpenSession(SessionType type) =>
206+
OpenSession(GetSessionConfiguration(type));
223207

224208
/// <summary>
225209
/// Opens new <see cref="Session"/> with specified <see cref="SessionConfiguration"/>.
@@ -234,14 +218,8 @@ public Session OpenSession(SessionType type)
234218
/// }
235219
/// </code></sample>
236220
/// <seealso cref="Session"/>
237-
public Session OpenSession(SessionConfiguration configuration)
238-
{
239-
ArgumentValidator.EnsureArgumentNotNull(configuration, "configuration");
240-
241-
return OpenSessionInternal(configuration,
242-
null,
243-
configuration.Supports(SessionOptions.AutoActivation));
244-
}
221+
public Session OpenSession(SessionConfiguration configuration) =>
222+
OpenSessionInternal(configuration, null, configuration.Supports(SessionOptions.AutoActivation));
245223

246224
internal Session OpenSessionInternal(SessionConfiguration configuration, StorageNode storageNode, bool activate)
247225
{
@@ -283,11 +261,8 @@ internal Session OpenSessionInternal(SessionConfiguration configuration, Storage
283261
/// }
284262
/// </code></sample>
285263
/// <seealso cref="Session"/>
286-
public Task<Session> OpenSessionAsync(CancellationToken cancellationToken = default)
287-
{
288-
var configuration = Configuration.Sessions.Default;
289-
return OpenSessionAsync(configuration, cancellationToken);
290-
}
264+
public Task<Session> OpenSessionAsync(CancellationToken cancellationToken = default) =>
265+
OpenSessionAsync(Configuration.Sessions.Default, cancellationToken);
291266

292267
/// <summary>
293268
/// Opens new <see cref="Session"/> of specified <see cref="SessionType"/> asynchronously.
@@ -304,18 +279,7 @@ public Task<Session> OpenSessionAsync(CancellationToken cancellationToken = defa
304279
public Task<Session> OpenSessionAsync(SessionType type, CancellationToken cancellationToken = default)
305280
{
306281
cancellationToken.ThrowIfCancellationRequested();
307-
switch (type) {
308-
case SessionType.User:
309-
return OpenSessionAsync(Configuration.Sessions.Default, cancellationToken);
310-
case SessionType.System:
311-
return OpenSessionAsync(Configuration.Sessions.System, cancellationToken);
312-
case SessionType.KeyGenerator:
313-
return OpenSessionAsync(Configuration.Sessions.KeyGenerator, cancellationToken);
314-
case SessionType.Service:
315-
return OpenSessionAsync(Configuration.Sessions.Service, cancellationToken);
316-
default:
317-
throw new ArgumentOutOfRangeException("type");
318-
}
282+
return OpenSessionAsync(GetSessionConfiguration(type), cancellationToken);
319283
}
320284

321285
/// <summary>
@@ -348,9 +312,17 @@ public Task<Session> OpenSessionAsync(SessionConfiguration configuration, Cancel
348312
}
349313
}
350314

315+
internal SessionConfiguration GetSessionConfiguration(SessionType type) =>
316+
type switch {
317+
SessionType.User => Configuration.Sessions.Default,
318+
SessionType.System => Configuration.Sessions.System,
319+
SessionType.KeyGenerator => Configuration.Sessions.KeyGenerator,
320+
SessionType.Service => Configuration.Sessions.Service,
321+
_ => throw new ArgumentOutOfRangeException(nameof(type))
322+
};
323+
351324
internal async Task<Session> OpenSessionInternalAsync(SessionConfiguration configuration, StorageNode storageNode, SessionScope sessionScope, CancellationToken cancellationToken)
352325
{
353-
ArgumentValidator.EnsureArgumentNotNull(configuration, nameof(configuration));
354326
configuration.Lock(true);
355327

356328
if (isDebugEventLoggingEnabled) {

Orm/Xtensive.Orm/Orm/StorageNode.cs

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -75,44 +75,24 @@ public sealed class StorageNode : ISessionSource
7575
internal ConcurrentDictionary<PersistRequestBuilderTask, ICollection<PersistRequest>> PersistRequestCache { get; }
7676

7777
/// <inheritdoc/>
78-
public Session OpenSession()
79-
{
80-
return OpenSession(domain.Configuration.Sessions.Default);
81-
}
78+
public Session OpenSession() =>
79+
OpenSession(domain.Configuration.Sessions.Default);
8280

8381
/// <inheritdoc/>
84-
public Session OpenSession(SessionType type)
85-
{
86-
return type switch {
87-
SessionType.User => OpenSession(domain.Configuration.Sessions.Default),
88-
SessionType.System => OpenSession(domain.Configuration.Sessions.System),
89-
SessionType.KeyGenerator => OpenSession(domain.Configuration.Sessions.KeyGenerator),
90-
SessionType.Service => OpenSession(domain.Configuration.Sessions.Service),
91-
_ => throw new ArgumentOutOfRangeException("type"),
92-
};
93-
}
82+
public Session OpenSession(SessionType type) =>
83+
OpenSession(domain.GetSessionConfiguration(type));
9484

9585
/// <inheritdoc/>
96-
public Session OpenSession(SessionConfiguration configuration)
97-
{
98-
return domain.OpenSessionInternal(configuration, this, configuration.Supports(SessionOptions.AutoActivation));
99-
}
86+
public Session OpenSession(SessionConfiguration configuration) =>
87+
domain.OpenSessionInternal(configuration, this, configuration.Supports(SessionOptions.AutoActivation));
10088

10189
/// <inheritdoc/>
10290
public Task<Session> OpenSessionAsync(CancellationToken cancellationToken = default) =>
10391
OpenSessionAsync(domain.Configuration.Sessions.Default, cancellationToken);
10492

10593
/// <inheritdoc/>
106-
public Task<Session> OpenSessionAsync(SessionType type, CancellationToken cancellationToken = default)
107-
{
108-
return type switch {
109-
SessionType.User => OpenSessionAsync(domain.Configuration.Sessions.Default),
110-
SessionType.System => OpenSessionAsync(domain.Configuration.Sessions.System),
111-
SessionType.KeyGenerator => OpenSessionAsync(domain.Configuration.Sessions.KeyGenerator),
112-
SessionType.Service => OpenSessionAsync(domain.Configuration.Sessions.Service),
113-
_ => throw new ArgumentOutOfRangeException("type"),
114-
};
115-
}
94+
public Task<Session> OpenSessionAsync(SessionType type, CancellationToken cancellationToken = default) =>
95+
OpenSessionAsync(domain.GetSessionConfiguration(type), cancellationToken);
11696

11797
/// <inheritdoc/>
11898
public Task<Session> OpenSessionAsync(SessionConfiguration configuration, CancellationToken cancellationToken = default)

0 commit comments

Comments
 (0)