Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Dapper.MoqTests.Samples/Car.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
namespace Dapper.MoqTests.Samples
using System;

namespace Dapper.MoqTests.Samples
{
// ReSharper disable UnusedAutoPropertyAccessor.Global
public class Car
{
public string Registration { get; set; }
public string Make { get; set; }
public string Model { get; set; }
public DateTime? DateRegistered { get; set; }
}
}
7 changes: 5 additions & 2 deletions Dapper.MoqTests.Samples/Samples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,15 @@ public void ReturnManyComplexObjects()
{
Registration = "ABC123",
Make = "Vauxhall",
Model = "Astra"
Model = "Astra",
DateRegistered = null,
};
var ford = new Car
{
Registration = "DEF456",
Make = "Ford",
Model = "Mondeo"
Model = "Mondeo",
DateRegistered = new DateTime(2022, 2, 1, 12, 11, 10),
};
connectionFactory
.Setup(f => f.OpenConnection())
Expand All @@ -215,6 +217,7 @@ from [Cars]

var result = repository.GetCars();

Assert.That(result.Select(c => c.DateRegistered), Is.EquivalentTo(new DateTime?[] { null, new DateTime(2022, 2, 1, 12, 11, 10) }));
Assert.That(result.Select(c => c.Model), Is.EquivalentTo(new[] { "Astra", "Mondeo" }));
}

Expand Down
45 changes: 39 additions & 6 deletions Dapper.MoqTests/ObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,24 @@ public static IDataReader GetDataReader(this object value)

var properties = value.GetType()
.GetProperties()
.Where(p => IsPrimitiveType(p.PropertyType))
.Select(p => new { Property = p, NullablePrimitiveType = NullablePrimitiveType(p.PropertyType) })
.Where(p => p.NullablePrimitiveType != null || IsPrimitiveType(p.Property.PropertyType))
.ToArray();
var dataTable = new DataTable();
foreach (var property in properties)
dataTable.Columns.Add(property.Name, property.PropertyType);
{
if (property.NullablePrimitiveType != null)
{
var column = new DataColumn(property.Property.Name, property.NullablePrimitiveType);
column.AllowDBNull = true;
dataTable.Columns.Add(column);
break;
}

dataTable.Columns.Add(property.Property.Name, property.Property.PropertyType);
}

var rowValues = properties.Select(p => p.GetValue(value)).ToArray();
var rowValues = properties.Select(p => p.Property.GetValue(value)).ToArray();
dataTable.Rows.Add(rowValues);

return new DataTableReader(dataTable);
Expand All @@ -48,15 +59,26 @@ private static DataTable GetDataTableForArray(IEnumerable value)

var properties = elementType
.GetProperties()
.Where(p => IsPrimitiveType(p.PropertyType))
.Select(p => new { Property = p, NullablePrimitiveType = NullablePrimitiveType(p.PropertyType) })

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest making a change more along the lines of:

.Select(p => new {
  name = p.Name,
  dataType = p.PropertyType,
  nullableDataType = NullablePrimativeType(p.PropertyType) != null,
})

In doing so the for loop later can be simplified:

foreach (var property in properties)
{
  var columnType = nullableDataType ?? property.PropertyType;
  var column = new DataColumn(property.name, columnType) 
  {
    AllowDBNull = nullableDataType != null
  }
  dataTable.Columns.Add(column);
}

Or something to that effect.

.Where(p => p.NullablePrimitiveType != null || IsPrimitiveType(p.Property.PropertyType))
.ToArray();
var dataTable = new DataTable();
foreach (var property in properties)
dataTable.Columns.Add(property.Name, property.PropertyType);
{
if (property.NullablePrimitiveType != null)
{
var column = new DataColumn(property.Property.Name, property.NullablePrimitiveType);
column.AllowDBNull = true;
dataTable.Columns.Add(column);
break;
}

dataTable.Columns.Add(property.Property.Name, property.Property.PropertyType);
}

foreach (var row in value)
{
var rowValues = properties.Select(p => p.GetValue(row)).ToArray();
var rowValues = properties.Select(p => p.Property.GetValue(row)).ToArray();
dataTable.Rows.Add(rowValues);
}

Expand All @@ -83,5 +105,16 @@ private static bool IsPrimitiveType(Type type)
{
return type.IsPrimitive || PrimitiveDataTypes.Contains(type);
}
private static Type NullablePrimitiveType(Type type)
{
var underlyingType = Nullable.GetUnderlyingType(type);

if (underlyingType != null && IsPrimitiveType(underlyingType))
{
return underlyingType;
}

return null;
}
}
}