Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated to support DbGeography type #94

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
61 changes: 17 additions & 44 deletions EntityFramework.Utilities/EntityFramework.Utilities/EFDataReader.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Microsoft.SqlServer.Types;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
Expand All @@ -11,53 +12,16 @@ public class EFDataReader<T> : DbDataReader
{
public IEnumerable<T> Items { get; set; }
public IEnumerator<T> Enumerator { get; set; }
public IList<string> Properties { get; set; }
public List<Func<T, object>> Accessors { get; set; }

public EFDataReader(IEnumerable<T> items, IEnumerable<ColumnMapping> properties)
public IList<string> PropertyNames { get; set; }
public IList<PropertyInfo> Properties { get; set; }
public EFDataReader(IEnumerable<T> items)
{
Properties = properties.Select(p => p.NameOnObject).ToList();
Accessors = properties.Select(p =>
{
if (p.StaticValue != null)
{
Func<T,object> func = x => p.StaticValue;
return func;
}

var parts = p.NameOnObject.Split('.');

PropertyInfo info = typeof(T).GetProperty(parts[0]);
var method = typeof(EFDataReader<T>).GetMethod("MakeDelegate");
var generic = method.MakeGenericMethod(info.PropertyType);

var getter = (Func<T, object>)generic.Invoke(this, new object[] { info.GetGetMethod(true) });

var temp = info;
foreach (var part in parts.Skip(1))
{
var i = temp.PropertyType.GetProperty(part);
var g = i.GetGetMethod();

var old = getter;
getter = x => g.Invoke(old(x), null);

temp = i;
}


return getter;
}).ToList();
Properties = typeof(T).GetProperties().ToList();
PropertyNames = Properties.Select(p => p.Name).ToList();
Items = items;
Enumerator = items.GetEnumerator();
}

public static Func<T, object> MakeDelegate<U>(MethodInfo @get)
{
var f = (Func<T, U>)Delegate.CreateDelegate(typeof(Func<T, U>), @get);
return t => f(t);
}

public override void Close()
{
this.Enumerator = null;
Expand Down Expand Up @@ -95,7 +59,16 @@ public override int RecordsAffected

public override object GetValue(int ordinal)
{
return this.Accessors[ordinal](this.Enumerator.Current);
if (Properties[ordinal].PropertyType == typeof(DbGeography))
{
var data = Properties[ordinal].GetValue(this.Enumerator.Current, null) as DbGeography;
if (data != null)
{
return SqlGeography.Point(data.Latitude.Value, data.Longitude.Value, 4326);
}
return null;
}
return Properties[ordinal].GetValue(this.Enumerator.Current, null);
}

public override int GetOrdinal(string name)
Expand Down