Permalink
Browse files

Slight fix to detail loading

  • Loading branch information...
1 parent 8bee509 commit b3fcbd2352a1c4dd09c46876ea92acb2e489538d @markrendle committed Oct 12, 2012
@@ -0,0 +1,54 @@
+namespace Simple.Data
+{
+ using System.Collections.Generic;
+ using System.Linq;
+
+ public partial class InMemoryAdapter : IAdapterWithRelation
+ {
+ public bool IsValidRelation(string tableName, string relatedTableName)
+ {
+ return _joins.Any(
+ ji =>
+ _nameComparer.Equals(tableName, ji.MasterTableName) &&
+ _nameComparer.Equals(relatedTableName, ji.MasterPropertyName))
+ ||
+ _joins.Any(
+ ji =>
+ _nameComparer.Equals(tableName, ji.DetailTableName) &&
+ _nameComparer.Equals(relatedTableName, ji.DetailPropertyName));
+ }
+
+ public object FindRelated(string tableName, IDictionary<string, object> row, string relatedTableName)
+ {
+ return FindMaster(tableName, row, relatedTableName) ?? FindDetail(tableName, row, relatedTableName);
+ }
+
+ private object FindMaster(string tableName, IDictionary<string, object> row, string relatedTableName)
+ {
+ var master = _joins.FirstOrDefault(ji =>
+ _nameComparer.Equals(tableName, ji.MasterTableName) &&
+ _nameComparer.Equals(relatedTableName, ji.MasterPropertyName));
+ if (master != null)
+ {
+ object result;
+ row.TryGetValue(master.DetailPropertyName, out result);
+ return result;
+ }
+ return null;
+ }
+
+ private object FindDetail(string tableName, IDictionary<string, object> row, string relatedTableName)
+ {
+ var detail = _joins.FirstOrDefault(ji =>
+ _nameComparer.Equals(tableName, ji.DetailTableName) &&
+ _nameComparer.Equals(relatedTableName, ji.DetailPropertyName));
+ if (detail != null)
+ {
+ object result;
+ row.TryGetValue(detail.MasterPropertyName, out result);
+ return result;
+ }
+ return null;
+ }
+ }
+}
@@ -93,6 +93,7 @@
<Compile Include="ForUpdateClause.cs" />
<Compile Include="FunctionSignature.cs" />
<Compile Include="InMemoryAdapter.cs" />
+ <Compile Include="InMemoryAdapterIAdapterWithRelation.cs" />
<Compile Include="InMemoryAdapterIAdapterWithTransactions.cs" />
<Compile Include="BadExpressionException.cs" />
<Compile Include="JoinType.cs" />
@@ -95,9 +95,8 @@ private object GetRelatedData(GetMemberBinder binder, IAdapterWithRelation relat
{
result = related is IDictionary<string, object>
? (object) new SimpleRecord(related as IDictionary<string, object>, binder.Name, _database)
- : new SimpleResultSet(
- ((IEnumerable<IDictionary<string, object>>) related).Select(
- dict => new SimpleRecord(dict, binder.Name, _database)));
+ : ((IEnumerable<IDictionary<string, object>>) related).Select(
+ dict => new SimpleRecord(dict, binder.Name, _database)).ToList<dynamic>();
}
return result;

0 comments on commit b3fcbd2

Please sign in to comment.