Fixed bug on Get when there is no record found, cleaned up unused code

parent a92c8091
...@@ -45,12 +45,11 @@ public void Get() ...@@ -45,12 +45,11 @@ public void Get()
{ {
try try
{ {
var user = connection.Get<User>(1); connection.Get<User>(1);
Debug.Fail("Fail, should have thrown exception"); Debug.Fail("Fail, should have thrown exception");
} }
catch (Exception) catch (Exception)
{ {
Console.WriteLine("ok");
} }
} }
...@@ -60,6 +59,8 @@ public void InsertGetUpdate() ...@@ -60,6 +59,8 @@ public void InsertGetUpdate()
{ {
using (var connection = GetOpenConnection()) using (var connection = GetOpenConnection())
{ {
connection.Get<IUser>(3).IsNull();
var id = connection.Insert(new User {Name = "Adam", Age = 10}); var id = connection.Insert(new User {Name = "Adam", Age = 10});
id.IsEqualTo(1); id.IsEqualTo(1);
var user = connection.Get<IUser>(id); var user = connection.Get<IUser>(id);
......
...@@ -52,7 +52,7 @@ private static IEnumerable<PropertyInfo> TypePropertiesCache(Type type) ...@@ -52,7 +52,7 @@ private static IEnumerable<PropertyInfo> TypePropertiesCache(Type type)
public static T Get<T>(this IDbConnection connection, object id) public static T Get<T>(this IDbConnection connection, object id)
{ {
var type = typeof(T); var type = typeof(T);
if(!type.IsInterface) if (!type.IsInterface)
throw new DataException("This version of Get<T>() only supports interfaces."); throw new DataException("This version of Get<T>() only supports interfaces.");
var keys = KeyPropertiesCache(type); var keys = KeyPropertiesCache(type);
...@@ -68,24 +68,20 @@ public static T Get<T>(this IDbConnection connection, object id) ...@@ -68,24 +68,20 @@ public static T Get<T>(this IDbConnection connection, object id)
var sql = "select * from " + name + "s where " + onlyKey.Name + " = @" + onlyKey.Name; var sql = "select * from " + name + "s where " + onlyKey.Name + " = @" + onlyKey.Name;
var dynParms = new DynamicParameters(); var dynParms = new DynamicParameters();
dynParms.Add("@" + onlyKey.Name, id); dynParms.Add("@" + onlyKey.Name, id);
var res = connection.Query(sql, dynParms).FirstOrDefault() as SqlMapper.FastExpando;
if (res == null)
return (T) ((object) null);
if (type.IsInterface)
{
var proxy = ProxyGenerator.GetInterfaceProxy<T>(); var proxy = ProxyGenerator.GetInterfaceProxy<T>();
var res = connection.Query(sql, dynParms).FirstOrDefault() as SqlMapper.FastExpando;
foreach (var property in TypePropertiesCache(type)) foreach (var property in TypePropertiesCache(type))
{ {
var val = res.GetProperty(property.Name); var val = res.GetProperty(property.Name);
property.SetValue(proxy,val,null); property.SetValue(proxy, val, null);
} }
((IProxy) proxy).IsDirty = false; //reset change tracking and return ((IProxy)proxy).IsDirty = false; //reset change tracking and return
return proxy; return proxy;
}
else //for future support, will never be called now...
{
var res = connection.Query<T>(sql, dynParms);
return res.FirstOrDefault();
}
} }
/// <summary> /// <summary>
...@@ -102,7 +98,7 @@ public static long Insert<T>(this IDbConnection connection, T entityToInsert) ...@@ -102,7 +98,7 @@ public static long Insert<T>(this IDbConnection connection, T entityToInsert)
sb.AppendFormat("insert into {0}s (", name); sb.AppendFormat("insert into {0}s (", name);
var allProperties = TypePropertiesCache(typeof(T)); var allProperties = TypePropertiesCache(typeof(T));
var keyProperties = KeyPropertiesCache(typeof (T)); var keyProperties = KeyPropertiesCache(typeof(T));
for (var i = 0; i < allProperties.Count(); i++) for (var i = 0; i < allProperties.Count(); i++)
{ {
...@@ -140,7 +136,7 @@ public static long Insert<T>(this IDbConnection connection, T entityToInsert) ...@@ -140,7 +136,7 @@ public static long Insert<T>(this IDbConnection connection, T entityToInsert)
/// <returns>true if updated, false if not found or not modified (tracked entities)</returns> /// <returns>true if updated, false if not found or not modified (tracked entities)</returns>
public static bool Update<T>(this IDbConnection connection, T entityToUpdate) public static bool Update<T>(this IDbConnection connection, T entityToUpdate)
{ {
var proxy = ((IProxy) entityToUpdate); var proxy = ((IProxy)entityToUpdate);
if (proxy != null) if (proxy != null)
{ {
if (!proxy.IsDirty) return false; if (!proxy.IsDirty) return false;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment