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

上级 a92c8091
......@@ -45,12 +45,11 @@ public void Get()
{
try
{
var user = connection.Get<User>(1);
connection.Get<User>(1);
Debug.Fail("Fail, should have thrown exception");
}
catch (Exception)
{
Console.WriteLine("ok");
}
}
......@@ -60,6 +59,8 @@ public void InsertGetUpdate()
{
using (var connection = GetOpenConnection())
{
connection.Get<IUser>(3).IsNull();
var id = connection.Insert(new User {Name = "Adam", Age = 10});
id.IsEqualTo(1);
var user = connection.Get<IUser>(id);
......
......@@ -49,10 +49,10 @@ private static IEnumerable<PropertyInfo> TypePropertiesCache(Type type)
/// <param name="connection">Open SqlConnection</param>
/// <param name="id">Id of the entity to get, must be marked with [Key] attribute</param>
/// <returns>Entity of T</returns>
public static T Get<T>(this IDbConnection connection, object id)
public static T Get<T>(this IDbConnection connection, object id)
{
var type = typeof(T);
if(!type.IsInterface)
if (!type.IsInterface)
throw new DataException("This version of Get<T>() only supports interfaces.");
var keys = KeyPropertiesCache(type);
......@@ -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 dynParms = new DynamicParameters();
dynParms.Add("@" + onlyKey.Name, id);
if (type.IsInterface)
{
var proxy = ProxyGenerator.GetInterfaceProxy<T>();
var res = connection.Query(sql, dynParms).FirstOrDefault() as SqlMapper.FastExpando;
foreach (var property in TypePropertiesCache(type))
{
var val = res.GetProperty(property.Name);
property.SetValue(proxy,val,null);
}
((IProxy) proxy).IsDirty = false; //reset change tracking and return
return proxy;
}
else //for future support, will never be called now...
var res = connection.Query(sql, dynParms).FirstOrDefault() as SqlMapper.FastExpando;
if (res == null)
return (T) ((object) null);
var proxy = ProxyGenerator.GetInterfaceProxy<T>();
foreach (var property in TypePropertiesCache(type))
{
var res = connection.Query<T>(sql, dynParms);
return res.FirstOrDefault();
var val = res.GetProperty(property.Name);
property.SetValue(proxy, val, null);
}
((IProxy)proxy).IsDirty = false; //reset change tracking and return
return proxy;
}
/// <summary>
......@@ -102,7 +98,7 @@ public static long Insert<T>(this IDbConnection connection, T entityToInsert)
sb.AppendFormat("insert into {0}s (", name);
var allProperties = TypePropertiesCache(typeof(T));
var keyProperties = KeyPropertiesCache(typeof (T));
var keyProperties = KeyPropertiesCache(typeof(T));
for (var i = 0; i < allProperties.Count(); i++)
{
......@@ -126,7 +122,7 @@ public static long Insert<T>(this IDbConnection connection, T entityToInsert)
sb.Append(") ");
connection.Execute(sb.ToString(), entityToInsert);
//NOTE: would prefer to use IDENT_CURRENT('tablename') or IDENT_SCOPE but these are not available on SQLCE
var r = connection.Query("select @@IDENTITY id");
var r = connection.Query("select @@IDENTITY id");
tx.Commit();
return (int)r.First().id;
}
......@@ -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>
public static bool Update<T>(this IDbConnection connection, T entityToUpdate)
{
var proxy = ((IProxy) entityToUpdate);
var proxy = ((IProxy)entityToUpdate);
if (proxy != null)
{
if (!proxy.IsDirty) return false;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册