/// <summary> /// Loads all rows from the database (in a streaming fashion, allows you to traverse all /// objects without worrying about memory usage) /// </summary> public IEnumerable <T> LoadAll() { using (var connection = _factory.OpenSqlConnection()) { using (var transaction = connection.BeginTransaction(_settings.TransactionIsolationLevel)) { using (var command = connection.CreateCommand()) { command.Transaction = transaction; command.CommandTimeout = _settings.CommandTimeoutSeconds; command.CommandType = CommandType.Text; command.CommandText = _schemaManager.GetQuery(); using (var reader = command.ExecuteReader()) { var classMapProperties = _classMap.Properties.ToDictionary(p => p.PropertyName); var lookup = new DataReaderLookup(reader, classMapProperties); while (reader.Read()) { yield return((T)_activator.CreateInstance(lookup)); } } } } } }
public void CanCreateClassFromProperties() { var activator = new Activator(typeof(SomeClassWithProperties), new[] { nameof(SomeClassWithProperties.Number), nameof(SomeClassWithProperties.Text) }); var values = new TestValueLookup(new Dictionary <string, object> { { nameof(SomeClassWithProperties.Number), 123 }, { nameof(SomeClassWithProperties.Text), "HELLO" }, }); var instance = (SomeClassWithProperties)activator.CreateInstance(values); Assert.That(instance.Number, Is.EqualTo(123)); Assert.That(instance.Text, Is.EqualTo("HELLO")); }
public void SkipsPropertyWhenNotIncluded_Constructor() { var activator = new Activator(typeof(SomeClassWithConstructor), new[] { nameof(SomeClassWithConstructor.Number) }); var values = new TestValueLookup(new Dictionary <string, object> { { nameof(SomeClassWithConstructor.Number), 123 }, { nameof(SomeClassWithConstructor.Text), "HELLO" }, }); var instance = (SomeClassWithConstructor)activator.CreateInstance(values); Assert.That(instance.Number, Is.EqualTo(123)); Assert.That(instance.Text, Is.EqualTo(null)); }