public T Get <T>(object id, string tableName = null, int?commandTimeout = null) where T : class { IClassMapper classMapper = ClassMapperFactory.GetMapper <T>(tableName); KeyConditionResult keyConditionResult = SqlGenerator.GetKeyConditionById(classMapper, id); SqlConvertResult sqlConvertResult = SqlGenerator.Select(classMapper, keyConditionResult.Sql, null, keyConditionResult.Parameters, false); T result = DbConnection.Query <T>(sqlConvertResult.Sql, sqlConvertResult.Parameters, DbTransaction, true, commandTimeout, CommandType.Text).SingleOrDefault(); if (result == null) { return(null); } IPropertyMap persistedMap = classMapper.GetPersistedMap(); if (persistedMap != null) { persistedMap.PropertyInfo.SetValue(result, true, null); } IPropertyMap propertyChangedListMap = classMapper.GetPropertyChangedListMap(); if (propertyChangedListMap != null) { IList <string> changedList = propertyChangedListMap.PropertyInfo.GetValue(result, null) as IList <string>; if (changedList != null) { changedList.Clear(); } } return(result); }
public bool Save <T>(T entity, string tableName = null, int?commandTimeout = null) where T : class { IClassMapper classMapper = ClassMapperFactory.GetMapper <T>(tableName); IPropertyMap persistedMap = classMapper.GetPersistedMap(); if (persistedMap == null) { throw new Exception(string.Format("{0}没有映射持久化标识列,无法使用此方法!", typeof(T).FullName)); } bool isPersisted = (bool)persistedMap.PropertyInfo.GetValue(entity, null); return(isPersisted ? Update(entity, tableName, commandTimeout) : Insert(entity, tableName, commandTimeout)); }