Skip to content

Commit 09e979f

Browse files
committed
Added out parameter on With methods to fix ThatRendle#184
1 parent d2c6796 commit 09e979f

2 files changed

Lines changed: 38 additions & 4 deletions

File tree

Simple.Data.BehaviourTest/Query/WithTest.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,5 +249,28 @@ public void CriteriaReferencesShouldNotBeDuplicatedInSql()
249249

250250
GeneratedSqlIs(expectedSql);
251251
}
252+
253+
/// <summary>
254+
/// Test for issue #184
255+
/// </summary>
256+
[Test]
257+
public void CriteriaReferencesShouldUseWithAlias()
258+
{
259+
const string expectedSql = "select [dbo].[employee].[id],[dbo].[employee].[name]," +
260+
"[dbo].[employee].[managerid],[dbo].[employee].[departmentid]," +
261+
"[foo].[id] as [__with1__foo__id],[foo].[name] as [__with1__foo__name]" +
262+
" from [dbo].[employee] join [dbo].[department] [foo] on ([foo].[id] = [dbo].[employee].[departmentid])" +
263+
" where ([dbo].[employee].[name] like @p1" +
264+
" and [foo].[name] = @p2)";
265+
266+
dynamic foo;
267+
var q = _db.Employees.Query()
268+
.Where(_db.Employees.Name.Like("A%"))
269+
.WithOne(_db.Employees.Department.As("Foo"), out foo)
270+
.Where(foo.Name == "Admin");
271+
EatException(() => q.ToList());
272+
273+
GeneratedSqlIs(expectedSql);
274+
}
252275
}
253276
}

Simple.Data/SimpleQuery.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,11 +327,22 @@ public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, o
327327
return false;
328328
}
329329

330-
private static bool MethodIsCallable(MethodInfo mi, InvokeMemberBinder binder, object[] args)
330+
public SimpleQuery With(ObjectReference reference, out dynamic queryObjectReference)
331331
{
332-
return mi.Name.Equals(binder.Name, StringComparison.OrdinalIgnoreCase) &&
333-
mi.GetParameters().Length == 1 &&
334-
Convert.ChangeType(args, mi.GetParameters().Single().ParameterType) != null;
332+
queryObjectReference = reference;
333+
return With(new[] {reference});
334+
}
335+
336+
public SimpleQuery WithOne(ObjectReference reference, out dynamic queryObjectReference)
337+
{
338+
queryObjectReference = reference;
339+
return With(new[] {reference}, WithType.One);
340+
}
341+
342+
public SimpleQuery WithMany(ObjectReference reference, out dynamic queryObjectReference)
343+
{
344+
queryObjectReference = reference;
345+
return With(new[] {reference}, WithType.Many);
335346
}
336347

337348
private SimpleQuery ParseWith(InvokeMemberBinder binder, object[] args)

0 commit comments

Comments
 (0)