Skip to content
This repository was archived by the owner on Apr 15, 2025. It is now read-only.

Commit 4c54797

Browse files
committed
Caching table definitions to improve performance. Closes #72
1 parent 6d3dca3 commit 4c54797

File tree

3 files changed

+66
-4
lines changed

3 files changed

+66
-4
lines changed

‎DataCore.Test/DataCore.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
<Compile Include="Models\TestNullableProperty.cs" />
9090
<Compile Include="Models\TestOther.cs" />
9191
<Compile Include="Models\TestOverride.cs" />
92+
<Compile Include="PerformanceTest.cs" />
9293
<Compile Include="QueryTestGroupBy.cs" />
9394
<Compile Include="QueryTestHaving.cs" />
9495
<Compile Include="QueryTestJoins.cs" />

‎DataCore.Test/PerformanceTest.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using DataCore.Test.Models;
2+
using NUnit.Framework;
3+
4+
namespace DataCore.Test
5+
{
6+
[TestFixture]
7+
public class PerformanceTest
8+
{
9+
[Test]
10+
public void TimedSelect()
11+
{
12+
using (var connection = TestHelper.GetConnectionFor(TestHelper.DatabaseType.Sqlite, "Data Source=:memory:"))
13+
{
14+
var db = TestHelper.GetDatabaseFor(TestHelper.DatabaseType.Sqlite, connection);
15+
16+
db.CreateTableIfNotExists<TestClass>();
17+
db.Insert(TestHelper.GetNewTestClass());
18+
19+
for (int i = 0; i < 100000; i++)
20+
{
21+
db.SelectById<TestClass>(1);
22+
}
23+
24+
connection.Close();
25+
}
26+
}
27+
28+
[Test]
29+
public void TimedSelectWhere()
30+
{
31+
using (var connection = TestHelper.GetConnectionFor(TestHelper.DatabaseType.Sqlite, "Data Source=:memory:"))
32+
{
33+
var db = TestHelper.GetDatabaseFor(TestHelper.DatabaseType.Sqlite, connection);
34+
35+
db.CreateTableIfNotExists<TestClass>();
36+
db.Insert(TestHelper.GetNewTestClass());
37+
38+
for (int i = 0; i < 100000; i++)
39+
{
40+
db.Select<TestClass>(t => t.Id == 1);
41+
}
42+
43+
connection.Close();
44+
}
45+
}
46+
}
47+
}

‎DataCore/TableDefinition.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,31 @@
77

88
namespace DataCore
99
{
10-
public class TableDefinition
10+
public sealed class TableDefinition
1111
{
12-
public string Name { get; set; }
13-
public FieldDefinition IdField { get; set; }
14-
public List<FieldDefinition> Fields { get; set; }
12+
private static readonly Dictionary<Type, TableDefinition> Definitions = new Dictionary<Type, TableDefinition>();
13+
14+
public string Name { get; }
15+
public FieldDefinition IdField { get; }
16+
public List<FieldDefinition> Fields { get; }
1517

1618
public TableDefinition(Type type)
1719
{
20+
if (Definitions.ContainsKey(type))
21+
{
22+
var definition = Definitions[type];
23+
Name = definition.Name;
24+
IdField = definition.IdField;
25+
Fields = definition.Fields;
26+
27+
return;
28+
}
29+
1830
Name = GetTableName(type);
1931
IdField = GetIdFieldForType(type);
2032
Fields = GetPropertiesForType(type).Select(GetFieldForProperty).ToList();
33+
34+
Definitions.Add(type, this);
2135
}
2236

2337
private IEnumerable<PropertyInfo> GetPropertiesForType(Type type)

0 commit comments

Comments
 (0)