Skip to content

Commit dc03d92

Browse files
authored
Merge pull request #248 from servicetitan/upstream/EnsureNotLocked2
Optimizing refactoring: convert EnsureNotLocked() from extension to Method. The extension is marked obsolete
2 parents 7f9ccee + 75834f4 commit dc03d92

File tree

93 files changed

+370
-375
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+370
-375
lines changed

Orm/Xtensive.Orm/Collections/CollectionBaseSlim.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ IEnumerator IEnumerable.GetEnumerator()
7777
/// <inheritdoc/>
7878
public virtual void Add(TItem item)
7979
{
80-
this.EnsureNotLocked();
80+
EnsureNotLocked();
8181
items.Add(item);
8282
}
8383

@@ -88,21 +88,21 @@ public virtual void Add(TItem item)
8888
/// <exception cref="T:System.ArgumentNullException">collection is null.</exception>
8989
public virtual void AddRange(IEnumerable<TItem> collection)
9090
{
91-
this.EnsureNotLocked();
91+
EnsureNotLocked();
9292
items.AddRange(collection);
9393
}
9494

9595
/// <inheritdoc/>
9696
public virtual bool Remove(TItem item)
9797
{
98-
this.EnsureNotLocked();
98+
EnsureNotLocked();
9999
return items.Remove(item);
100100
}
101101

102102
/// <inheritdoc/>
103103
public virtual void Clear()
104104
{
105-
this.EnsureNotLocked();
105+
EnsureNotLocked();
106106
items.Clear();
107107
}
108108

Orm/Xtensive.Orm/Collections/ExtensionCollection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void Set<T>(T value)
6767
/// <exception cref="ArgumentException">Wrong arguments.</exception>
6868
public void Set(Type extensionType, object value)
6969
{
70-
this.EnsureNotLocked();
70+
EnsureNotLocked();
7171
ArgumentValidator.EnsureArgumentNotNull(extensionType, "extensionType");
7272
if (extensionType.IsValueType)
7373
throw new ArgumentException(string.Format(
@@ -87,7 +87,7 @@ public void Set(Type extensionType, object value)
8787
/// <inheritdoc/>
8888
public void Clear()
8989
{
90-
this.EnsureNotLocked();
90+
EnsureNotLocked();
9191
extensions = null;
9292
}
9393

Orm/Xtensive.Orm/Collections/FlagCollection.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public bool ContainsKey(TKey key)
7272
/// <inheritdoc/>
7373
public void Add(TKey key, TFlag flag)
7474
{
75-
this.EnsureNotLocked();
75+
EnsureNotLocked();
7676
if (keys.Contains(key))
7777
throw new ArgumentException("key", Strings.ExCollectionAlreadyContainsSpecifiedItem);
7878
if (keys.Count >= MaxItemCount)
@@ -91,7 +91,7 @@ public virtual void Add(TKey key)
9191
public bool Remove(TKey key)
9292
{
9393
ArgumentValidator.EnsureArgumentIsNotDefault(key, "key");
94-
this.EnsureNotLocked();
94+
EnsureNotLocked();
9595
int index = keys.IndexOf(key);
9696
if (index < 0)
9797
return false;
@@ -130,7 +130,7 @@ public TFlag this[TKey key]
130130
set
131131
{
132132
ArgumentValidator.EnsureArgumentIsNotDefault(key, "key");
133-
this.EnsureNotLocked();
133+
EnsureNotLocked();
134134
int index = keys.IndexOf(key);
135135
if (index < 0)
136136
Add(key, value);
@@ -191,7 +191,7 @@ public void Add(KeyValuePair<TKey, TFlag> item)
191191
/// <inheritdoc/>
192192
public void Clear()
193193
{
194-
this.EnsureNotLocked();
194+
EnsureNotLocked();
195195
keys.Clear();
196196
flags = new BitVector32(0);
197197
}

Orm/Xtensive.Orm/Collections/TypeRegistry.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public bool Contains(Type type)
5757
/// <param name="type">The type to register.</param>
5858
public void Register(Type type)
5959
{
60-
this.EnsureNotLocked();
60+
EnsureNotLocked();
6161
ArgumentValidator.EnsureArgumentNotNull(type, "type");
6262
if (!isProcessingPendingActions)
6363
Register(new TypeRegistration(type));
@@ -78,7 +78,7 @@ public void Register(Type type)
7878
/// <exception cref="ArgumentNullException">When <paramref name="assembly"/> is null.</exception>
7979
public void Register(Assembly assembly)
8080
{
81-
this.EnsureNotLocked();
81+
EnsureNotLocked();
8282
ArgumentValidator.EnsureArgumentNotNull(assembly, "assembly");
8383
Register(new TypeRegistration(assembly));
8484
}
@@ -95,7 +95,7 @@ public void Register(Assembly assembly)
9595
/// or <paramref name="namespace"/> is empty string.</exception>
9696
public void Register(Assembly assembly, string @namespace)
9797
{
98-
this.EnsureNotLocked();
98+
EnsureNotLocked();
9999
ArgumentValidator.EnsureArgumentNotNull(assembly, "assembly");
100100
ArgumentValidator.EnsureArgumentNotNullOrEmpty(@namespace, "@namespace");
101101
Register(new TypeRegistration(assembly, @namespace));
@@ -109,7 +109,7 @@ public void Register(Assembly assembly, string @namespace)
109109
/// otherwise, <see langword="false" />.</returns>
110110
public bool Register(TypeRegistration action)
111111
{
112-
this.EnsureNotLocked();
112+
EnsureNotLocked();
113113
ArgumentValidator.EnsureArgumentNotNull(action, "action");
114114
if (actionSet.Contains(action))
115115
return false;
@@ -143,7 +143,7 @@ private void ProcessPendingActions()
143143
/// <inheritdoc/>
144144
public override void Lock(bool recursive)
145145
{
146-
this.EnsureNotLocked();
146+
EnsureNotLocked();
147147
ProcessPendingActions();
148148
assemblies = new ReadOnlyHashSet<Assembly>((HashSet<Assembly>)assemblies);
149149
base.Lock(recursive);

Orm/Xtensive.Orm/Core/Extensions/LockableExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
// Created by: Alex Yakunin
55
// Created: 2008.07.04
66

7+
using System;
8+
79
namespace Xtensive.Core
810
{
911
/// <summary>
@@ -16,6 +18,7 @@ public static class LockableExtensions
1618
/// </summary>
1719
/// <param name="lockable">Lockable object to check.</param>
1820
/// <exception cref="InstanceIsLockedException">Specified instance is locked.</exception>
21+
[Obsolete("Use LockableBase.EnsureNotLocked method instead.")]
1922
public static void EnsureNotLocked(this ILockable lockable)
2023
{
2124
ArgumentValidator.EnsureArgumentNotNull(lockable, "lockable");

Orm/Xtensive.Orm/Core/Interfaces/ILockable.cs

Lines changed: 4 additions & 4 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) 2003-2022 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

55
namespace Xtensive.Core
66
{
@@ -23,7 +23,7 @@ public interface ILockable
2323
bool IsLocked { get; }
2424

2525
/// <summary>
26-
/// Locks the instance (non-recursively).
26+
/// Locks the instance recursively.
2727
/// </summary>
2828
void Lock();
2929

Lines changed: 17 additions & 25 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) 2003-2022 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: Alex Yakunin
55
// Created: 2007.11.22
66

@@ -16,36 +16,28 @@ namespace Xtensive.Core
1616
[Serializable]
1717
public abstract class LockableBase: ILockable
1818
{
19-
private bool isLocked;
2019

2120
/// <inheritdoc/>
22-
public bool IsLocked
21+
public bool IsLocked { [DebuggerStepThrough] get; private set; }
22+
23+
/// <summary>
24+
/// Ensures the object is not locked (see <see cref="ILockable.Lock()"/>) yet.
25+
/// </summary>
26+
/// <exception cref="InstanceIsLockedException">The instance is locked.</exception>
27+
public void EnsureNotLocked()
2328
{
24-
[DebuggerStepThrough]
25-
get { return isLocked; }
29+
if (IsLocked) {
30+
throw new InstanceIsLockedException(Strings.ExInstanceIsLocked);
31+
}
2632
}
2733

2834
/// <inheritdoc/>
29-
public void Lock()
30-
{
31-
Lock(true);
32-
}
35+
public void Lock() => Lock(true);
3336

3437
/// <inheritdoc/>
35-
public virtual void Lock(bool recursive)
36-
{
37-
isLocked = true;
38-
}
38+
public virtual void Lock(bool recursive) => IsLocked = true;
39+
3940

40-
/// <summary>
41-
/// Unlocks the object.
42-
/// Sets <see cref="IsLocked"/> to <see langword="false"/>.
43-
/// </summary>
44-
protected void Unlock()
45-
{
46-
isLocked = false;
47-
}
48-
4941

5042
// Constructors
5143

@@ -63,7 +55,7 @@ protected LockableBase()
6355
/// <param name="isLocked">Initial <see cref="IsLocked"/> property value.</param>
6456
protected LockableBase(bool isLocked)
6557
{
66-
this.isLocked = isLocked;
58+
IsLocked = isLocked;
6759
}
6860
}
6961
}

Orm/Xtensive.Orm/Modelling/Actions/ActionSequence.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public ActionScope LogAction()
4242
public void Add(NodeAction action)
4343
{
4444
ArgumentValidator.EnsureArgumentNotNull(action, "action");
45-
this.EnsureNotLocked();
45+
EnsureNotLocked();
4646
// Only locked actions can be added
4747
var ca = action as PropertyChangeAction;
4848
if (ca!=null && actions.Count!=0) {
@@ -56,7 +56,7 @@ public void Add(NodeAction action)
5656
actions.RemoveAt(lastIndex);
5757
}
5858
}
59-
action.Lock(true);
59+
action.Lock(true);
6060
actions.Add(action);
6161
}
6262

Orm/Xtensive.Orm/Modelling/Actions/CreateNodeAction.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public Type Type {
3232
get { return type; }
3333
set {
3434
ArgumentValidator.EnsureArgumentNotNull(value, "value");
35-
this.EnsureNotLocked();
35+
EnsureNotLocked();
3636
type = value;
3737
}
3838
}
@@ -44,7 +44,7 @@ public string Name {
4444
get { return name; }
4545
set {
4646
ArgumentValidator.EnsureArgumentNotNullOrEmpty(value, "value");
47-
this.EnsureNotLocked();
47+
EnsureNotLocked();
4848
name = value;
4949
}
5050
}
@@ -56,7 +56,7 @@ public string Name {
5656
public int? Index {
5757
get { return index; }
5858
set {
59-
this.EnsureNotLocked();
59+
EnsureNotLocked();
6060
index = value;
6161
}
6262
}
@@ -71,7 +71,7 @@ public object[] Parameters {
7171
return parameters==null ? null : (object[]) parameters.Clone();
7272
}
7373
set {
74-
this.EnsureNotLocked();
74+
EnsureNotLocked();
7575
parameters = value;
7676
}
7777
}

Orm/Xtensive.Orm/Modelling/Actions/DataAction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public DataHint DataHint
2626
get { return dataHint; }
2727
set
2828
{
29-
this.EnsureNotLocked();
29+
EnsureNotLocked();
3030
dataHint = value;
3131
Path = dataHint.SourceTablePath;
3232
}

0 commit comments

Comments
 (0)