Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 0c1f056

Browse files
committed
Remove check for invalid fields as OrmLite behavior is to silently ignore invalid fields + has better perf/less alloc
1 parent c3a318c commit 0c1f056

File tree

2 files changed

+21
-50
lines changed

2 files changed

+21
-50
lines changed

src/ServiceStack.OrmLite/OrmLiteReadCommandExtensions.cs

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -893,25 +893,20 @@ internal static T LoadSingleById<T>(this IDbCommand dbCmd, object value, string[
893893
return row;
894894
}
895895

896-
public static void LoadReferences<T>(this IDbCommand dbCmd, T instance, string[] include = null)
896+
public static void LoadReferences<T>(this IDbCommand dbCmd, T instance, IEnumerable<string> include = null)
897897
{
898898
var loadRef = new LoadReferencesSync<T>(dbCmd, instance);
899899
var fieldDefs = loadRef.FieldDefs;
900900

901-
if (include != null)
902-
{
903-
// Check that any include values aren't reference fields of the specified type
904-
var includeLower = include.Map(x => x.ToLower());
905-
var fieldNames = fieldDefs.ConvertAll(q => q.FieldName.ToLower());
906-
var invalid = includeLower.Except(fieldNames).ToList();
907-
if (invalid.Count > 0)
908-
throw new ArgumentException("Fields '{0}' are not Reference Properties of Type '{1}'".Fmt(invalid.Join("', '"), typeof(T).Name));
909-
910-
fieldDefs = fieldDefs.Where(f => includeLower.Contains(f.FieldName.ToLower())).ToList();
911-
}
901+
var includeSet = include != null
902+
? new HashSet<string>(include, StringComparer.OrdinalIgnoreCase)
903+
: null;
912904

913905
foreach (var fieldDef in fieldDefs)
914906
{
907+
if (includeSet != null && !includeSet.Contains(fieldDef.Name))
908+
continue;
909+
915910
dbCmd.Parameters.Clear();
916911
var listInterface = fieldDef.FieldType.GetTypeWithGenericInterfaceOf(typeof(IList<>));
917912
if (listInterface != null)
@@ -925,25 +920,20 @@ public static void LoadReferences<T>(this IDbCommand dbCmd, T instance, string[]
925920
}
926921
}
927922

928-
internal static List<Into> LoadListWithReferences<Into, From>(this IDbCommand dbCmd, SqlExpression<From> expr = null, string[] include = null)
923+
internal static List<Into> LoadListWithReferences<Into, From>(this IDbCommand dbCmd, SqlExpression<From> expr = null, IEnumerable<string> include = null)
929924
{
930925
var loadList = new LoadListSync<Into, From>(dbCmd, expr);
931-
932926
var fieldDefs = loadList.FieldDefs;
933-
if (include != null)
934-
{
935-
// Check that any include values aren't reference fields of the specified From type
936-
var includeLower = include.Map(x => x.ToLower());
937-
var fieldNames = fieldDefs.ConvertAll(q => q.FieldName.ToLower());
938-
var invalid = includeLower.Except(fieldNames).ToList();
939-
if (invalid.Count > 0)
940-
throw new ArgumentException("Fields '{0}' are not Reference Properties of Type '{1}'".Fmt(invalid.Join("', '"), typeof(From).Name));
941-
942-
fieldDefs = fieldDefs.Where(f => includeLower.Contains(f.FieldName.ToLower())).ToList();
943-
}
927+
928+
var includeSet = include != null
929+
? new HashSet<string>(include, StringComparer.OrdinalIgnoreCase)
930+
: null;
944931

945932
foreach (var fieldDef in fieldDefs)
946933
{
934+
if (includeSet != null && !includeSet.Contains(fieldDef.Name))
935+
continue;
936+
947937
var listInterface = fieldDef.FieldType.GetTypeWithGenericInterfaceOf(typeof(IList<>));
948938
if (listInterface != null)
949939
{

tests/ServiceStack.OrmLite.Tests/LoadReferencesTests.cs

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -687,32 +687,13 @@ public void Can_load_only_included_references()
687687
Assert.That(dbCustomer.PrimaryAddress, Is.Null);
688688

689689
// Invalid field name
690-
try
691-
{
692-
dbCustomers = db.LoadSelect<Customer>(q => q.Id == customer.Id, include: new[] { "InvalidOption1", "InvalidOption2" });
693-
Assert.Fail();
694-
}
695-
catch (System.ArgumentException ex)
696-
{
697-
}
698-
catch (System.Exception ex)
699-
{
700-
Assert.Fail();
701-
}
702-
690+
dbCustomers = db.LoadSelect<Customer>(q => q.Id == customer.Id, include: new[] { "InvalidOption1", "InvalidOption2" });
691+
Assert.That(dbCustomers.All(x => x.Orders == null));
692+
Assert.That(dbCustomers.All(x => x.PrimaryAddress == null));
703693

704-
try
705-
{
706-
dbCustomer = db.LoadSingleById<Customer>(customer.Id, include: new[] { "InvalidOption1", "InvalidOption2" });
707-
Assert.Fail();
708-
}
709-
catch (System.ArgumentException ex)
710-
{
711-
}
712-
catch (System.Exception ex)
713-
{
714-
Assert.Fail();
715-
}
694+
dbCustomer = db.LoadSingleById<Customer>(customer.Id, include: new[] { "InvalidOption1", "InvalidOption2" });
695+
Assert.That(dbCustomer.Orders, Is.Null);
696+
Assert.That(dbCustomer.PrimaryAddress, Is.Null);
716697
}
717698

718699
[Test]

0 commit comments

Comments
 (0)