Skip to content

Commit

Permalink
Added Datatable support.
Browse files Browse the repository at this point in the history
  • Loading branch information
rohan3usturge committed Jul 10, 2017
1 parent fc69eba commit a273d5c
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 6 deletions.
14 changes: 12 additions & 2 deletions ProxyMapper/Core/DbCallInterceptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ internal class DbCallInterceptor : IInterceptor

private readonly IValidatorChain<CallInfo> _validatorChain;

public DbCallInterceptor(string connectionString, IDataProcessor dataProcessor)
private readonly IDatatableConverter _datatableConverter;

public DbCallInterceptor(string connectionString, IDataProcessor dataProcessor,IDatatableConverter datatableConverter)
{
this._connectionString = connectionString;
this._dataProcessor = dataProcessor;
this._datatableConverter = datatableConverter;
this._validatorChain = new DefaultValidatorChain<CallInfo>(new IValidator<CallInfo>[]
{
new CallInfoValidator()
Expand Down Expand Up @@ -62,7 +65,14 @@ public void Intercept(IInvocation invocation)
int i = 0;
foreach (object argument in arguments)
{
sqlCommand.Parameters.AddWithValue(parameterInfos[i++].Name, argument);
object argumentTobeAdded = argument;
DtAttribute customAttribute = argument.GetType().GetCustomAttribute<DtAttribute>();
if (customAttribute != null)
{
DataTable dataTable = this._datatableConverter.ToDataTable((IEnumerable) argument);
argumentTobeAdded = dataTable;
}
sqlCommand.Parameters.AddWithValue(parameterInfos[i++].Name, argumentTobeAdded);
}

//Call DB
Expand Down
34 changes: 34 additions & 0 deletions ProxyMapper/Core/DefaultDatatableConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections;
using System.Data;
using System.Reflection;

namespace ProxyMapper.Core
{
public class DefaultDatatableConverter : IDatatableConverter
{
public DataTable ToDataTable(IEnumerable list)
{
Type type = list.GetType().GetGenericArguments()[0];
PropertyInfo[] properties = type.GetProperties();
DataTable dataTable = new DataTable();
foreach (PropertyInfo info in properties)
{
dataTable.Columns.Add(new DataColumn(info.Name, Nullable.GetUnderlyingType(info.PropertyType) ?? info.PropertyType));
}

foreach (object entity in list)
{
object[] values = new object[properties.Length];
for (int i = 0; i < properties.Length; i++)
{
values[i] = properties[i].GetValue(entity);
}

dataTable.Rows.Add(values);
}

return dataTable;
}
}
}
10 changes: 10 additions & 0 deletions ProxyMapper/Core/IDatatableConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections;
using System.Data;

namespace ProxyMapper.Core
{
public interface IDatatableConverter
{
DataTable ToDataTable(IEnumerable list);
}
}
3 changes: 2 additions & 1 deletion ProxyMapper/Core/ProxyFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public static T CreateRepoProxy<T>(string connectionString, IDataProcessor dataP
"Proxies can be generated only for interfaces. Please provide a proper interface.");
}
object proxy = ProxyGenerator.CreateInterfaceProxyWithoutTarget(typeof(T),
new DbCallInterceptor(connectionString, GetDataProcessor(dataProcessor)));
new DbCallInterceptor(connectionString, GetDataProcessor(dataProcessor),
new DefaultDatatableConverter()));
return (T) proxy;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace ProxyMapper.Enums
{
public class DatatableAttribute : Attribute
public class DtAttribute : Attribute
{
public DatatableAttribute(string columns)
public DtAttribute(string columns)
{
this.Columns = columns;
}
Expand Down
2 changes: 1 addition & 1 deletion ProxyMapper/ProxyMapper.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net46;netcoreapp1.1</TargetFrameworks>
<TargetFrameworks>net46</TargetFrameworks>
<PackageId>ProxyMapper</PackageId>
<PackageVersion>0.1.1</PackageVersion>
<Authors>Rohan Usturge</Authors>
Expand Down

0 comments on commit a273d5c

Please sign in to comment.