Created
December 29, 2011 03:10
-
-
Save DevJohnC/1531492 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TableName("\"ModelTest\"")] | |
[PrimaryKey("id")] | |
public class ModelTest | |
{ | |
public int id { get; set; } | |
public string test1 { get; set; } | |
public string test2 { get; set; } | |
} | |
public static class ExpandoCasting | |
{ | |
public static T To<T>(this System.Dynamic.ExpandoObject record) where T : new() | |
{ | |
var dictionary = record.ToDictionary(); | |
T obj = new T(); | |
var objType = obj.GetType(); | |
var properties = objType.GetProperties(); | |
foreach (var property in properties) | |
{ | |
if (dictionary.ContainsKey(property.Name)) | |
{ | |
var fieldval = dictionary[property.Name]; | |
property.SetValue(obj, fieldval, null); | |
} | |
} | |
return obj; | |
} | |
} | |
public class GenericModel<T> : DynamicModel where T : new() | |
{ | |
public GenericModel() | |
: base("hardcoded", "", "") | |
{ | |
T instance = new T(); | |
var type = instance.GetType(); | |
TableName = type.Name; | |
var attribs = type.GetCustomAttributes(true); | |
foreach (var attrib in attribs) | |
{ | |
if (attrib is PrimaryKeyAttribute) | |
{ | |
PrimaryKeyField = ((PrimaryKeyAttribute)attrib).PrimaryKey; | |
} | |
else if (attrib is TableNameAttribute) | |
{ | |
TableName = ((TableNameAttribute)attrib).TableName; | |
} | |
} | |
} | |
public override IEnumerable<dynamic> Query(string sql, DbConnection connection, params object[] args) | |
{ | |
return (IEnumerable<dynamic>)TypedQuery(sql, connection, args); | |
} | |
public override IEnumerable<dynamic> Query(string sql, params object[] args) | |
{ | |
return (IEnumerable<dynamic>)TypedQuery(sql, args); | |
} | |
public IEnumerable<T> TypedQuery(string sql, params object[] args) | |
{ | |
using (var conn = OpenConnection()) | |
{ | |
var rdr = CreateCommand(sql, conn, args).ExecuteReader(); | |
while (rdr.Read()) | |
{ | |
yield return ((System.Dynamic.ExpandoObject)rdr.RecordToExpando()).To<T>(); ; | |
} | |
} | |
} | |
public IEnumerable<T> TypedQuery(string sql, DbConnection connection, params object[] args) | |
{ | |
using (var rdr = CreateCommand(sql, connection, args).ExecuteReader()) | |
{ | |
while (rdr.Read()) | |
{ | |
yield return ((System.Dynamic.ExpandoObject)rdr.RecordToExpando()).To<T>(); ; | |
} | |
} | |
} | |
} | |
[AttributeUsage(AttributeTargets.Class)] | |
public class TableNameAttribute : Attribute | |
{ | |
public TableNameAttribute(string tableName) | |
{ | |
TableName = tableName; | |
} | |
public string TableName { get; private set; } | |
} | |
[AttributeUsage(AttributeTargets.Class)] | |
public class PrimaryKeyAttribute : Attribute | |
{ | |
public PrimaryKeyAttribute(string primaryKey) | |
{ | |
PrimaryKey = primaryKey; | |
} | |
public string PrimaryKey { get; private set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment