Skip to content

Commit 3ce5110

Browse files
committed
Fix a number of code smells
I'm not going to treat the change of SyncRoot from a field to a property as a breaking change. Technically it is, but it's a mistake that it was a field anyway.
1 parent b639a33 commit 3ce5110

11 files changed

Lines changed: 48 additions & 49 deletions

File tree

CSF.Entities/Entity`1.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ object IEntity.IdentityValue
7474
/// </summary>
7575
/// <returns>The converted value.</returns>
7676
/// <param name="value">The value to convert.</param>
77-
TIdentity ToIdentityType(object value)
77+
static TIdentity ToIdentityType(object value)
7878
{
7979
var formatter = System.Globalization.CultureInfo.InvariantCulture;
8080
var converted = Convert.ChangeType(value, typeof(TIdentity), formatter);

CSF.Entities/IdentityFactory.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ public class IdentityFactory : ICreatesIdentity
3333
{
3434
static readonly IGetsIdentityType identityTypeProvider = new IdentityTypeProvider();
3535

36-
/// <summary>
37-
/// Create an identity from the specified parameters.
38-
/// </summary>
39-
/// <returns>The created identity, or a <c>null</c> reference if the identity value is equal to the default of its data-type.</returns>
40-
/// <param name="entityType">The entity type.</param>
41-
/// <param name="identityValue">The identity value.</param>
36+
/// <summary>
37+
/// Create an identity from the specified parameters.
38+
/// </summary>
39+
/// <returns>The created identity, or a <c>null</c> reference if the identity value is equal to the default of its data-type.</returns>
40+
/// <param name="entityType">The entity type.</param>
41+
/// <param name="identityValue">The identity value.</param>
4242
public IIdentity Create(Type entityType, object identityValue)
4343
{
4444
if (entityType == null)
@@ -79,7 +79,7 @@ public IIdentity Create(Type entityType, Type identityType, object identityValue
7979
return (IIdentity)Activator.CreateInstance(closedIdentityType, new[] { convertedValue });
8080
}
8181

82-
object GetDefaultValue(Type identityType)
82+
static object GetDefaultValue(Type identityType)
8383
=> identityType.IsValueType ? Activator.CreateInstance(identityType) : null;
8484
}
8585
}

CSF.ORM.Entities/EntityData.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -162,21 +162,17 @@ public void Update<TEntity>(TEntity entity) where TEntity : class, IEntity
162162
if (entity == null)
163163
throw new ArgumentNullException(nameof(entity));
164164

165-
return AddAsyncInternal(entity, token);
165+
return AddInternalAsync(entity, token);
166166
}
167167

168-
async Task<IIdentity<TEntity>> AddAsyncInternal<TEntity>(TEntity entity, CancellationToken token = default(CancellationToken)) where TEntity : class, IEntity
168+
async Task<IIdentity<TEntity>> AddInternalAsync<TEntity>(TEntity entity, CancellationToken token = default(CancellationToken)) where TEntity : class, IEntity
169169
{
170-
var identity = entity.GetIdentity();
171-
object idValue;
172-
if (identity != null)
173-
{
174-
idValue = await persister.AddAsync(entity, identity.Value, token);
175-
return ReferenceEquals(idValue, null) ? null : (IIdentity<TEntity>)idFactory.Create(typeof(TEntity), idValue);
176-
}
170+
var currentIdentity = entity.GetIdentity();
171+
var currentIdentityValue = (currentIdentity != null)? currentIdentity.Value : null;
177172

178-
idValue = await persister.AddAsync(entity, null, token);
179-
return ReferenceEquals(idValue, null) ? null : (IIdentity<TEntity>)idFactory.Create(typeof(TEntity), idValue);
173+
var newIdentityValue = await persister.AddAsync(entity, currentIdentityValue, token)
174+
.ConfigureAwait(false);
175+
return (newIdentityValue is null) ? null : (IIdentity<TEntity>) idFactory.Create(typeof(TEntity), newIdentityValue);
180176
}
181177

182178
/// <summary>

CSF.ORM.Entities/IdentityGeneratingPersisterDecorator.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,17 @@ public void Delete<T>(T item, object identity) where T : class
9494
public async Task<object> AddAsync<T>(T item, object identity = null, CancellationToken token = default(CancellationToken)) where T : class
9595
{
9696
if (identity != null)
97-
return await wrapped.AddAsync<T>(item, identity, token);
97+
return await wrapped.AddAsync<T>(item, identity, token).ConfigureAwait(false);
9898

9999
if (item is IEntity entity)
100100
{
101101
identityGenerator.UpdateWithIdentity(entity);
102-
return await wrapped.AddAsync<T>((T)entity, entity.IdentityValue, token);
102+
return await wrapped.AddAsync<T>((T)entity, entity.IdentityValue, token)
103+
.ConfigureAwait(false);
103104
}
104105

105-
return await wrapped.AddAsync<T>(item, identity, token);
106+
return await wrapped.AddAsync<T>(item, identity, token)
107+
.ConfigureAwait(false);
106108
}
107109

108110
/// <summary>

CSF.ORM.Entities/IdentityPopulatingTheoryQueryDecorator.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ public TQueried Theorise<TQueried>(object identityValue) where TQueried : class
9191
/// <typeparam name="TQueried">The type of object to retrieve.</typeparam>
9292
public async Task<TQueried> TheoriseAsync<TQueried>(object identityValue, CancellationToken token = default(CancellationToken)) where TQueried : class
9393
{
94-
var output = await wrapped.TheoriseAsync<TQueried>(identityValue, token);
94+
var output = await wrapped.TheoriseAsync<TQueried>(identityValue, token)
95+
.ConfigureAwait(false);
9596

9697
if (output is IEntity entity && !entity.HasIdentity)
9798
entity.IdentityValue = identityValue;

CSF.ORM.NHibernate.Common/EagerFetchingProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public IQueryableWithEagerFetching<TQueried, TGrandchild> FetchGrandchildren<TQu
129129
return new QueryableWithFetchingAdapter<TQueried, TGrandchild>(nhFetchRequest);
130130
}
131131

132-
IQueryable<T> GetUnderlyingQueryIfAvailable<T>(IQueryable<T> query)
132+
static IQueryable<T> GetUnderlyingQueryIfAvailable<T>(IQueryable<T> query)
133133
=> (query is IProvidesQueryable<T> queryProvider) ? queryProvider.GetQueryable() : query;
134134

135135
INhFetchRequest<TQueried, TFetched> GetNhFetchRequest<TQueried, TFetched>(IQueryable<TQueried> query)

CSF.ORM/InMemory/DataStore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class DataStore
3636
/// <summary>
3737
/// Gets a synchronisation object under which the current instance may be locked.
3838
/// </summary>
39-
public readonly ReaderWriterLockSlim SyncRoot;
39+
public ReaderWriterLockSlim SyncRoot { get; }
4040

4141
/// <summary>
4242
/// Gets the collection of items in the data store.

PersistenceTester/CSF.PersistenceTester.Core/Impl/PersistenceTester.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using System;
2-
using CSF.EqualityRules;
2+
using CSF.EqualityRules;
33
using CSF.ORM;
44

55
namespace CSF.PersistenceTester.Impl
@@ -47,8 +47,8 @@ PersistenceTestResult TrySetup()
4747
};
4848
}
4949
finally
50-
{
51-
conn?.Dispose();
50+
{
51+
conn?.Dispose();
5252
}
5353

5454
return null;
@@ -77,8 +77,8 @@ PersistenceTestResult TrySave()
7777
};
7878
}
7979
finally
80-
{
81-
conn?.Dispose();
80+
{
81+
conn?.Dispose();
8282
}
8383

8484
return null;
@@ -113,8 +113,8 @@ PersistenceTestResult TryCompare()
113113
};
114114
}
115115
finally
116-
{
117-
conn?.Dispose();
116+
{
117+
conn?.Dispose();
118118
}
119119
}
120120

@@ -124,22 +124,22 @@ PersistenceTestResult TryCompare()
124124
/// This method ensures that both values can be converted to string now, while we still have the connection.
125125
/// </summary>
126126
/// <param name="equalityResult">An equality result.</param>
127-
void EnsureEqualityResultMayBeReportedUpon(EqualityResult equalityResult)
128-
{
129-
foreach(var item in equalityResult.RuleResults)
127+
static void EnsureEqualityResultMayBeReportedUpon(EqualityResult equalityResult)
128+
{
129+
foreach(var item in equalityResult.RuleResults)
130130
{
131131
try { item.ValueA?.ToString(); }
132132
catch(Exception e)
133-
{
134-
throw new PersistenceTestingException($"An exception was raised when pre-converting the persisted value {item.Name} to string.", e);
135-
}
136-
133+
{
134+
throw new PersistenceTestingException($"An exception was raised when pre-converting the persisted value {item.Name} to string.", e);
135+
}
136+
137137
try { item.ValueB?.ToString(); }
138138
catch (Exception e)
139139
{
140140
throw new PersistenceTestingException($"An exception was raised when pre-converting the retrieved value {item.Name} to string.", e);
141141
}
142-
}
142+
}
143143
}
144144

145145
/// <summary>

PersistenceTester/CSF.PersistenceTester.NUnit/Constraints/NotEqualResult.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ void WriteFailingRule(MessageWriter writer, EqualityRuleResult ruleResult)
2626
writer.WriteLine($"{RuleName(ruleResult)} Expected {Format(ruleResult.ValueA)} but got {Format(ruleResult.ValueB)}");
2727
}
2828

29-
string RuleName(EqualityRuleResult ruleResult)
29+
static string RuleName(EqualityRuleResult ruleResult)
3030
{
3131
return $">>> [{ruleResult.Name,20}]:";
3232
}
3333

34-
string Format(object value)
34+
static string Format(object value)
3535
{
3636
if (ReferenceEquals(value, null)) return "<null>";
3737
if (value is string || value is char) return $"\"{value}\"";

PersistenceTester/CSF.PersistenceTester.NUnit/Constraints/SuccessfulPersistenceConstraint.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ namespace CSF.PersistenceTester.Constraints
88
public class SuccessfulPersistenceConstraint : Constraint
99
{
1010
/// <summary>
11-
/// Applies the constraint to an actual value, returning a ConstraintResult.
12-
/// </summary>
13-
/// <param name="actual">The value to be tested</param>
14-
/// <returns>A ConstraintResult</returns>
11+
/// Applies the constraint to an actual value, returning a ConstraintResult.
12+
/// </summary>
13+
/// <param name="actual">The value to be tested</param>
14+
/// <returns>A ConstraintResult</returns>
1515
public override ConstraintResult ApplyTo<TActual>(TActual actual)
1616
{
1717
ConstraintResult result;
@@ -35,15 +35,15 @@ public override ConstraintResult ApplyTo<TActual>(TActual actual)
3535
return new NotEqualResult(this, testResult.EqualityResult);
3636
}
3737

38-
ConstraintResult GetTypeCheckResult<TActual>(TActual actual)
38+
static ConstraintResult GetTypeCheckResult<TActual>(TActual actual)
3939
{
4040
var constraint = new InstanceOfTypeConstraint(typeof(PersistenceTestResult));
4141
var result = constraint.ApplyTo(actual);
4242
if (result?.IsSuccess == true) return null;
4343
return result;
4444
}
4545

46-
ConstraintResult GetNullCheckResult(PersistenceTestResult actual)
46+
static ConstraintResult GetNullCheckResult(PersistenceTestResult actual)
4747
{
4848
var constraint = new NotConstraint(new NullConstraint());
4949
var result = constraint.ApplyTo(actual);

0 commit comments

Comments
 (0)