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

Commit 60db99b

Browse files
committed
Merge pull request #469 from jklemmack/Feature/LoadSingle
Add include support to LoadSingleById
2 parents 5ffeef2 + a44a1d2 commit 60db99b

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

src/ServiceStack.OrmLite/OrmLiteReadApi.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -669,9 +669,9 @@ public static long LongScalar(this IDbConnection dbConn)
669669
/// Returns the first result with all its references loaded, using a primary key id. E.g:
670670
/// <para>db.LoadSingleById&lt;Person&gt;(1)</para>
671671
/// </summary>
672-
public static T LoadSingleById<T>(this IDbConnection dbConn, object idValue)
672+
public static T LoadSingleById<T>(this IDbConnection dbConn, object idValue, string[] include = null)
673673
{
674-
return dbConn.Exec(dbCmd => dbCmd.LoadSingleById<T>(idValue));
674+
return dbConn.Exec(dbCmd => dbCmd.LoadSingleById<T>(idValue, include));
675675
}
676676

677677
/// <summary>

src/ServiceStack.OrmLite/OrmLiteReadCommandExtensions.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -878,22 +878,34 @@ internal static long ToLong(object result)
878878
return (long)result;
879879
}
880880

881-
internal static T LoadSingleById<T>(this IDbCommand dbCmd, object value)
881+
internal static T LoadSingleById<T>(this IDbCommand dbCmd, object value, string[] include = null)
882882
{
883883
var row = dbCmd.SingleById<T>(value);
884884
if (row == null)
885885
return default(T);
886886

887-
dbCmd.LoadReferences(row);
887+
dbCmd.LoadReferences(row, include);
888888

889889
return row;
890890
}
891891

892-
public static void LoadReferences<T>(this IDbCommand dbCmd, T instance)
892+
public static void LoadReferences<T>(this IDbCommand dbCmd, T instance, string[] include = null)
893893
{
894894
var loadRef = new LoadReferencesSync<T>(dbCmd, instance);
895+
var fieldDefs = loadRef.FieldDefs;
895896

896-
foreach (var fieldDef in loadRef.FieldDefs)
897+
if (!include.IsEmpty())
898+
{
899+
// Check that any include values aren't reference fields of the specified type
900+
var fields = fieldDefs.Select(q => q.FieldName);
901+
var invalid = include.Except<string>(fields).ToList();
902+
if (invalid.Count > 0)
903+
throw new ArgumentException("Fields '{0}' are not Reference Properties of Type '{1}'".Fmt(invalid.Join("', '"), typeof(T).Name));
904+
905+
fieldDefs = fieldDefs.Where(fd => include.Contains(fd.FieldName)).ToList();
906+
}
907+
908+
foreach (var fieldDef in fieldDefs)
897909
{
898910
dbCmd.Parameters.Clear();
899911
var listInterface = fieldDef.FieldType.GetTypeWithGenericInterfaceOf(typeof(IList<>));

tests/ServiceStack.OrmLite.Tests/LoadReferencesTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,13 @@ public void Can_load_only_included_references()
656656
Assert.That(dbCustomers[0].Orders, Is.Null);
657657
Assert.That(dbCustomers[0].PrimaryAddress, Is.Not.Null);
658658

659+
// Test LoadSingleById
660+
var dbCustomer = db.LoadSingleById<Customer>(customer.Id, include: new[] { "PrimaryAddress" });
661+
Assert.That(dbCustomer.Name, Is.EqualTo("Customer 1"));
662+
663+
Assert.That(dbCustomer.Orders, Is.Null);
664+
Assert.That(dbCustomer.PrimaryAddress, Is.Not.Null);
665+
659666

660667
// Invalid field name
661668
try
@@ -670,6 +677,20 @@ public void Can_load_only_included_references()
670677
{
671678
Assert.Fail();
672679
}
680+
681+
682+
try
683+
{
684+
dbCustomer = db.LoadSingleById<Customer>(customer.Id, include: new[] { "InvalidOption1", "InvalidOption2" });
685+
Assert.Fail();
686+
}
687+
catch (System.ArgumentException ex)
688+
{
689+
}
690+
catch (System.Exception ex)
691+
{
692+
Assert.Fail();
693+
}
673694
}
674695

675696
[Test]

0 commit comments

Comments
 (0)