Commit 1e8011ce authored by Martin Devillers's avatar Martin Devillers Committed by Nick Craver

FIX: Properties of type Table<T, TId> are not initialized

Table properties that implement Table<T,TId> instead of Table<T> are not recognized during the initialization of the Database<T> class. As a result, these properties remain uninitialized after the call to Database<T>.Init().

// Works fine
public Table<Person> Persons { get; set; }

// Remains 'null' after Init
public Table<Car, Guid> Cars { get; set; }
parent da7f0dd3
...@@ -177,7 +177,7 @@ internal void InitDatabase(DbConnection connection, int commandTimeout) ...@@ -177,7 +177,7 @@ internal void InitDatabase(DbConnection connection, int commandTimeout)
internal virtual Action<TDatabase> CreateTableConstructorForTable() internal virtual Action<TDatabase> CreateTableConstructorForTable()
{ {
return CreateTableConstructor(typeof(Table<>)); return CreateTableConstructor(typeof(Table<>), typeof(Table<,>));
} }
public void BeginTransaction(IsolationLevel isolation = IsolationLevel.ReadCommitted) public void BeginTransaction(IsolationLevel isolation = IsolationLevel.ReadCommitted)
...@@ -197,13 +197,13 @@ public void RollbackTransaction() ...@@ -197,13 +197,13 @@ public void RollbackTransaction()
transaction = null; transaction = null;
} }
protected Action<TDatabase> CreateTableConstructor(Type tableType) protected Action<TDatabase> CreateTableConstructor(params Type[] tableTypes)
{ {
var dm = new DynamicMethod("ConstructInstances", null, new Type[] { typeof(TDatabase) }, true); var dm = new DynamicMethod("ConstructInstances", null, new Type[] { typeof(TDatabase) }, true);
var il = dm.GetILGenerator(); var il = dm.GetILGenerator();
var setters = GetType().GetProperties() var setters = GetType().GetProperties()
.Where(p => p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == tableType) .Where(p => p.PropertyType.IsGenericType && tableTypes.Contains(p.PropertyType.GetGenericTypeDefinition()))
.Select(p => Tuple.Create( .Select(p => Tuple.Create(
p.GetSetMethod(true), p.GetSetMethod(true),
p.PropertyType.GetConstructor(new Type[] { typeof(TDatabase), typeof(string) }), p.PropertyType.GetConstructor(new Type[] { typeof(TDatabase), typeof(string) }),
...@@ -340,4 +340,4 @@ public void Dispose() ...@@ -340,4 +340,4 @@ public void Dispose()
} }
} }
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment