I recently created a select select method based on the associated object type. It applies this to an IQueryOver object (class property). The method also has access to the nhibernate configuration. You can add them as method parameters. It needs work for production, but the method works fine in dev, only used it for one object.
This method was created because I am trying to list my data at the server level, and an excellent result transformer will not work.
After receiving a collection of objects (query.List ()), you may need to reload the objects in order to populate one on many child objects. Many to one match will be proxied for lazy loads.
public void DistinctRootProjectionList<E>() { var classMapping = Context.Config.GetClassMapping(typeof(E)); var propertyIterator = classMapping.UnjoinedPropertyIterator; List<IProjection> projections = new List<IProjection>(); ProjectionList list = Projections.ProjectionList(); list.Add(Projections.Property(classMapping.IdentifierProperty.Name), classMapping.IdentifierProperty.Name); foreach (var item in propertyIterator) { if (item.Value.IsSimpleValue || item.Value.Type.IsEntityType) { list.Add(Projections.Property(item.Name), item.Name); } } query.UnderlyingCriteria.SetProjection(Projections.Distinct(list)); query.TransformUsing(Transformers.AliasToBean<E>()); }
The code I used to load one of many relationships ... T is an entity type.
for (int i = 0; i < resp.Data.Count; i++) { resp.Data[i] = session.Load<T>(GetInstanceIdValue(resp.Data[i])); }
longday Aug 12 '11 at 13:45 2011-08-12 13:45
source share