Skip to content

Commit b30a833

Browse files
committed
#53 refactored the activate and load to accomodate NonEntity and Entity Classes.
1 parent 0049309 commit b30a833

File tree

5 files changed

+36
-23
lines changed

5 files changed

+36
-23
lines changed

SubSonic.Core.DataAccessLayer/src/Builders/DbSqlQueryBuilder/DbSqlQueryBuilderQueryProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public TResult Execute<TResult>(Expression expression)
189189
}
190190
}
191191
}
192-
else if (CmdBehavior == CommandBehavior.SingleRow)
192+
else if (CmdBehavior == CommandBehavior.Default)
193193
{
194194
IList result = Activator.CreateInstance(typeof(List<>).MakeGenericType(elementType)) as IList;
195195

SubSonic.Core.DataAccessLayer/src/Collections/SubSonicCollection/SubSonicCollection.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,13 @@ public override IEnumerable ToArray()
126126

127127
IEnumerator<TEntity> IEnumerable<TEntity>.GetEnumerator()
128128
{
129-
if (TableData is ICollection<TEntity> data)
129+
if (!IsLoaded)
130130
{
131-
if (!IsLoaded)
132-
{
133-
Load();
134-
}
131+
Load();
132+
}
135133

134+
if (TableData is ICollection<TEntity> data)
135+
{
136136
return data.GetEnumerator();
137137
}
138138
else
@@ -143,12 +143,18 @@ IEnumerator<TEntity> IEnumerable<TEntity>.GetEnumerator()
143143

144144
public override IQueryable Load()
145145
{
146-
if (Provider.Execute<IEnumerable<TEntity>>(Expression) is ISubSonicCollection elements)
146+
IEnumerable<TEntity> result = Provider.Execute<IEnumerable<TEntity>>(Expression);
147+
148+
if (result is ISubSonicCollection elements)
147149
{
148150
TableData = (IEnumerable)Activator.CreateInstance(typeof(HashSet<>).MakeGenericType(ElementType), elements.ToArray());
149-
150-
IsLoaded = true;
151151
}
152+
else
153+
{
154+
TableData = (IEnumerable)Activator.CreateInstance(typeof(HashSet<>).MakeGenericType(ElementType), result);
155+
}
156+
157+
IsLoaded = true;
152158

153159
return this;
154160
}

SubSonic.Core.DataAccessLayer/src/Extensions/Internal/DatabaseReaders.cs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,31 @@
33
using SubSonic.Schema;
44
using System;
55
using System.Data;
6+
using System.Reflection;
67

78
namespace SubSonic
89
{
910
internal static partial class InternalExtensions
1011
{
1112
public static object LoadInstanceOf(this IDataRecord data, object entity)
1213
{
13-
IDbEntityModel model = SubSonicContext.DbModel.GetEntityModel(entity.GetType());
14+
Type entityType = entity.GetType();
15+
SubSonicContext.DbModel.TryGetEntityModel(entityType, out IDbEntityModel model);
1416

15-
foreach (IDbEntityProperty property in model.Properties)
17+
foreach (PropertyInfo property in entityType.GetProperties())
1618
{
17-
if (property.EntityPropertyType == DbEntityPropertyType.Value)
19+
if (model != null && model[property.Name].EntityPropertyType != DbEntityPropertyType.Value)
1820
{
19-
object value = data[property.Name];
21+
continue;
22+
}
23+
24+
object value = data[model?[property.Name].Name ?? property.Name];
2025

21-
if (property.PropertyType.IsAssignableFrom(value.GetType()))
22-
{
23-
model.EntityModelType
24-
.GetProperty(property.PropertyName)
25-
.SetValue(entity, value);
26-
}
26+
if (property.PropertyType.IsAssignableFrom(value.GetType()))
27+
{
28+
entityType
29+
.GetProperty(property.Name)
30+
.SetValue(entity, value);
2731
}
2832
}
2933

@@ -49,7 +53,8 @@ public static object ActivateAndLoadInstanceOf(this IDataRecord data, Type entit
4953

5054
object entity = null;
5155

52-
if (SubSonicContext.DbOptions.EnableProxyGeneration)
56+
if (SubSonicContext.DbOptions.EnableProxyGeneration &&
57+
SubSonicContext.DbModel.IsEntityModelRegistered(entityType))
5358
{
5459
DynamicProxyWrapper wrapper = DynamicProxy.GetProxyWrapper(entityType);
5560

SubSonic.Tests/DAL/SUT/BaseTestFixture.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ INNER JOIN [dbo].[Person] AS [T5]
283283

284284
Context.Database.Instance.AddCommandBehavior(memberInitializedUsingDatasource, cmd =>
285285
{
286-
return BuildDataTable(CurrentRenters);
286+
return BuildDataTable(CurrentRenters.ToList());
287287
});
288288

289289
Context.Database.Instance.AddCommandBehavior(memberInitializedUsingDatasourceCount, cmd =>
@@ -340,7 +340,9 @@ protected DataTable BuildDataTable<TEntity>(IEnumerable<TEntity> entities)
340340
);
341341
}
342342

343-
return entities.ToDataTable();
343+
DataTable data = entities.ToDataTable();
344+
345+
return data;
344346
}
345347

346348
protected virtual void SetInsertBehaviors()

SubSonic.Tests/SubSonic.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
2323
<PackageReference Include="SubSonic.Core.Abstractions" Version="4.2.0-beta.4" />
2424
<PackageReference Include="SubSonic.Extensions.SqlServer" Version="4.2.0" />
25-
<PackageReference Include="SubSonic.Extensions.Test" Version="4.2.0" />
25+
<PackageReference Include="SubSonic.Extensions.Test" Version="4.2.1-beta.6" />
2626
</ItemGroup>
2727

2828
<ItemGroup>

0 commit comments

Comments
 (0)