Skip to content

Commit 532b01e

Browse files
Added unit-tests for the ado-net repository
1 parent 6d27756 commit 532b01e

File tree

5 files changed

+619
-46
lines changed

5 files changed

+619
-46
lines changed

src/DotNetToolkit.Repository.AdoNet/AdoNetRepositoryBase.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1412,7 +1412,7 @@ private void PrepareSelectStatement(IFetchStrategy<TEntity> fetchStrategy, out D
14121412
var mainTablePrimaryKeyName = typeof(TEntity).GetPrimaryKeyPropertyInfo().GetColumnName();
14131413

14141414
// Default select
1415-
var selectSql = string.Join(",\n\t",
1415+
var columns = string.Join(",\n\t",
14161416
SqlPropertiesMapping.Select(x =>
14171417
{
14181418
var colAlias = cfg.GenerateColumnAlias(x.Value);
@@ -1429,7 +1429,7 @@ private void PrepareSelectStatement(IFetchStrategy<TEntity> fetchStrategy, out D
14291429
// the same type as the primary table
14301430
// Only do a join when the primary table has a foreign key property for the join table
14311431
var paths = mainTableProperties
1432-
.Where(x => x.IsComplex() && !string.IsNullOrEmpty(x.PropertyType.GetForeignKeyName(mainTableType)))
1432+
.Where(x => x.IsComplex() && !string.IsNullOrEmpty(x.PropertyType.GetPrimaryKeyPropertyInfo()?.GetColumnName()))
14331433
.Select(x => x.Name)
14341434
.ToList();
14351435

@@ -1451,13 +1451,13 @@ private void PrepareSelectStatement(IFetchStrategy<TEntity> fetchStrategy, out D
14511451
{
14521452
var joinStatementSb = new StringBuilder();
14531453

1454-
sb.Append($"SELECT\n\t{selectSql}");
1454+
sb.Append($"SELECT\n\t{columns}");
14551455

14561456
foreach (var path in fetchStrategy.IncludePaths)
14571457
{
14581458
var joinTablePropertyInfo = mainTableProperties.Single(x => x.Name.Equals(path));
14591459
var joinTableType = joinTablePropertyInfo.PropertyType;
1460-
var joinTableForeignKeyName = joinTableType.GetForeignKeyName(mainTableType);
1460+
var joinTableForeignKeyName = joinTableType.GetForeignKeyPropertyInfo(mainTableType)?.GetColumnName();
14611461

14621462
// Only do a join when the primary table has a foreign key property for the join table
14631463
if (!string.IsNullOrEmpty(joinTableForeignKeyName))
@@ -1492,7 +1492,7 @@ private void PrepareSelectStatement(IFetchStrategy<TEntity> fetchStrategy, out D
14921492
}
14931493
else
14941494
{
1495-
sb.Append($"SELECT\n\t{selectSql}\nFROM [{TableName}] AS [{mainTableAlias}]");
1495+
sb.Append($"SELECT\n\t{columns}\nFROM [{TableName}] AS [{mainTableAlias}]");
14961496
}
14971497

14981498
cfg.Sql = sb.ToString();

src/DotNetToolkit.Repository.AdoNet/Internal/ExtensionMethods.cs

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

src/DotNetToolkit.Repository/Helpers/ConventionHelper.cs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -143,45 +143,43 @@ public static int GetColumnOrder(this PropertyInfo pi)
143143
/// <param name="sourceType">The source type.</param>
144144
/// <param name="foreignType">The foreign type to match.</param>
145145
/// <returns>The name of the foreign key.</returns>
146-
public static string GetForeignKeyName(this Type sourceType, Type foreignType)
146+
public static PropertyInfo GetForeignKeyPropertyInfo(this Type sourceType, Type foreignType)
147147
{
148148
var properties = sourceType.GetRuntimeProperties().Where(x => x.IsMapped());
149149
var foreignPropertyInfo = properties.SingleOrDefault(x => x.PropertyType == foreignType);
150-
var foreignKeyName = string.Empty;
150+
151+
PropertyInfo propertyInfo = null;
151152

152153
if (foreignPropertyInfo != null)
153154
{
154155
var propertyInfosWithForeignKeys = properties.Where(x => x.GetCustomAttribute<ForeignKeyAttribute>() != null);
155156
if (propertyInfosWithForeignKeys.Any())
156157
{
157158
// Try to find by checking on the foreign key property
158-
foreignKeyName = propertyInfosWithForeignKeys
159+
propertyInfo = propertyInfosWithForeignKeys
159160
.Where(x => x.IsPrimitive())
160-
.SingleOrDefault(x => x.GetCustomAttribute<ForeignKeyAttribute>().Name.Equals(foreignPropertyInfo.Name))
161-
?.GetColumnName();
161+
.SingleOrDefault(x => x.GetCustomAttribute<ForeignKeyAttribute>().Name.Equals(foreignPropertyInfo.Name));
162162

163163
// Try to find by checking on the navigation property
164-
if (string.IsNullOrEmpty(foreignKeyName))
164+
if (propertyInfo == null)
165165
{
166-
foreignKeyName = properties
166+
propertyInfo = properties
167167
.Where(x => x.IsPrimitive())
168-
.SingleOrDefault(x => foreignPropertyInfo.GetCustomAttribute<ForeignKeyAttribute>().Name.Equals(x.GetColumnName()))
169-
?.GetColumnName();
168+
.SingleOrDefault(x => foreignPropertyInfo.GetCustomAttribute<ForeignKeyAttribute>().Name.Equals(x.GetColumnName()));
170169
}
171170
}
172171

173172
// Try to find by naming convention
174-
if (string.IsNullOrEmpty(foreignKeyName))
173+
if (propertyInfo == null)
175174
{
176175
var foreignPrimaryKeyName = foreignType.GetPrimaryKeyPropertyInfo().GetColumnName();
177176

178-
foreignKeyName = properties
179-
.SingleOrDefault(x => x.Name == $"{foreignType.Name}{foreignPrimaryKeyName}")
180-
?.GetColumnName();
177+
propertyInfo = properties
178+
.SingleOrDefault(x => x.Name == $"{foreignPropertyInfo.Name}{foreignPrimaryKeyName}");
181179
}
182180
}
183181

184-
return foreignKeyName;
182+
return propertyInfo;
185183
}
186184

187185
/// <summary>

src/DotNetToolkit.Repository/RepositoryAsyncBase.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -535,9 +535,6 @@ protected RepositoryAsyncBase(ILogger logger) : base(logger)
535535
if (selector == null)
536536
throw new ArgumentNullException(nameof(selector));
537537

538-
if (fetchStrategy == null)
539-
throw new ArgumentNullException(nameof(fetchStrategy));
540-
541538
return GetEntityAsync(key, fetchStrategy, selector, cancellationToken);
542539
}
543540

0 commit comments

Comments
 (0)