Skip to content

Commit

Permalink
Support nullable types.
Browse files Browse the repository at this point in the history
Support IEnumerable properties in objects (serializes to string).
  • Loading branch information
JDBarndt committed Jul 19, 2017
1 parent 5ac5cd3 commit 9c90061
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .vs/ProjectSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"CurrentProjectSetting": null
}
Binary file added .vs/slnx.sqlite
Binary file not shown.
24 changes: 22 additions & 2 deletions Logic/Common.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
Expand All @@ -14,7 +16,17 @@ public static DataTable GetDataTableFromFields<T>(IEnumerable<T> data, SqlBulkCo
foreach (PropertyInfo propertyInfo in listType.GetProperties())
{
var columnName = GetColumnName(propertyInfo);
dt.Columns.Add(columnName, propertyInfo.PropertyType);

if (propertyInfo.PropertyType.IsNonStringEnumerable())
{
dt.Columns.Add(columnName, typeof(string));
}
else
{
dt.Columns.Add(columnName, Nullable.GetUnderlyingType(propertyInfo.PropertyType) ?? propertyInfo.PropertyType);
}


sqlBulkCopy.ColumnMappings.Add(columnName, columnName);
}

Expand All @@ -24,7 +36,15 @@ public static DataTable GetDataTableFromFields<T>(IEnumerable<T> data, SqlBulkCo
foreach (PropertyInfo propertyInfo in listType.GetProperties())
{
var columnName = GetColumnName(propertyInfo);
dr[columnName] = propertyInfo.GetValue(value, null);

if (propertyInfo.PropertyType.IsNonStringEnumerable())
{
dr[columnName] = JsonConvert.SerializeObject(propertyInfo.GetValue(value, null));
}
else
{
dr[columnName] = propertyInfo.GetValue(value, null) ?? DBNull.Value;
}
}
dt.Rows.Add(dr);
}
Expand Down
27 changes: 27 additions & 0 deletions Logic/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

public static class Extensions
{
public static bool IsNonStringEnumerable(this PropertyInfo pi)
{
return pi != null && pi.PropertyType.IsNonStringEnumerable();
}

public static bool IsNonStringEnumerable(this object instance)
{
return instance != null && instance.GetType().IsNonStringEnumerable();
}

public static bool IsNonStringEnumerable(this Type type)
{
if (type == null || type == typeof(string))
return false;
return typeof(IEnumerable).IsAssignableFrom(type);
}
}
7 changes: 7 additions & 0 deletions Logic/Logic.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
Expand All @@ -41,10 +44,14 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Common.cs" />
<Compile Include="Extensions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SimpleSqlBulkUpdate.cs" />
<Compile Include="SimpleSqlBulkCopy.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
1 change: 0 additions & 1 deletion Logic/SimpleSqlBulkCopy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ public void Dispose()
sqlBulkCopy = null;
}


public void WriteToServer<T>(string destinationTableName, IEnumerable<T> data)
{
sqlBulkCopy.DestinationTableName = destinationTableName;
Expand Down
4 changes: 4 additions & 0 deletions Logic/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net451" />
</packages>

0 comments on commit 9c90061

Please sign in to comment.