From: <aye...@us...> - 2008-07-18 05:59:32
|
Revision: 3637 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=3637&view=rev Author: ayenderahien Date: 2008-07-18 05:59:41 +0000 (Fri, 18 Jul 2008) Log Message: ----------- Applying patch from Will Shaver for NH-1291, Example.Create with anonymous objects Modified Paths: -------------- trunk/nhibernate/src/NHibernate/Criterion/Example.cs Modified: trunk/nhibernate/src/NHibernate/Criterion/Example.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Criterion/Example.cs 2008-07-13 16:06:50 UTC (rev 3636) +++ trunk/nhibernate/src/NHibernate/Criterion/Example.cs 2008-07-18 05:59:41 UTC (rev 3637) @@ -1,6 +1,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Reflection; using Iesi.Collections.Generic; using NHibernate.Engine; using NHibernate.Persister.Entity; @@ -271,6 +272,30 @@ _selector.Include(value, name, type); } + private object[] GetPropertyValues(IEntityPersister persister, ICriteria criteria, ICriteriaQuery criteriaQuery) + { + System.Type type = _entity.GetType(); + if(type == persister.GetMappedClass(GetEntityMode(criteria, criteriaQuery))) //not using anon object + { + return persister.GetPropertyValues(_entity, GetEntityMode(criteria, criteriaQuery)); + } + ArrayList list = new ArrayList(); + for(int i = 0; i < persister.PropertyNames.Length; i++) + { + PropertyInfo pInfo = type.GetProperty(persister.PropertyNames[i]); + if(pInfo != null) + { + list.Add(pInfo.GetValue(_entity, null)); + } + else + { + list.Add(null); //to maintain same order as PropertyNames list + _excludedProperties.Add(persister.PropertyNames[i]); //exclude the properties that aren't in the anon object (duplicates ok) + } + } + return list.ToArray(); + } + public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery, IDictionary<string, IFilter> enabledFilters) { SqlStringBuilder builder = new SqlStringBuilder(); @@ -279,7 +304,7 @@ IEntityPersister meta = criteriaQuery.Factory.GetEntityPersister(criteriaQuery.GetEntityName(criteria)); String[] propertyNames = meta.PropertyNames; IType[] propertyTypes = meta.PropertyTypes; - object[] propertyValues = meta.GetPropertyValues(_entity, GetEntityMode(criteria, criteriaQuery)); + object[] propertyValues = GetPropertyValues(meta, criteria, criteriaQuery); for (int i = 0; i < propertyNames.Length; i++) { object propertyValue = propertyValues[i]; @@ -323,12 +348,14 @@ return builder.ToSqlString(); } + + //note: now that Criterion are adding typed values via ICriteriaQuery.AddUsedTypedValues this function is never called. public override TypedValue[] GetTypedValues(ICriteria criteria, ICriteriaQuery criteriaQuery) { IEntityPersister meta = criteriaQuery.Factory.GetEntityPersister(criteriaQuery.GetEntityName(criteria)); string[] propertyNames = meta.PropertyNames; IType[] propertyTypes = meta.PropertyTypes; - object[] values = meta.GetPropertyValues(_entity, GetEntityMode(criteria, criteriaQuery)); + object[] values = GetPropertyValues(meta, criteria, criteriaQuery); ArrayList list = new ArrayList(); for (int i = 0; i < propertyNames.Length; i++) @@ -366,7 +393,8 @@ EntityMode? result = meta.GuessEntityMode(_entity); if (!result.HasValue) { - throw new InvalidCastException(_entity.GetType().FullName); + return EntityMode.Poco; //this occurs for anon objects + //throw new InvalidCastException(_entity.GetType().FullName); } return result.Value; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |