My first crack at it is working most of the time. The challenge I
make a query without projections the tuple coming back pushes only an
entity with the identity property set. I would expect the get the
full entity back:
public class SearchResultsTransformer : IResultTransformer
{
SearchResults results = new SearchResults();
bool projectionBasedResult;
List<PropertyInfo> properties;
public SearchResultsTransformer(Type entityType, IList<string>
propertyNames)
{
Check.ForNullValue(entityType, "entityType");
properties = new List<PropertyInfo>
(entityType.GetProperties());
if ((propertyNames == null) ||
(propertyNames.Count == 0))
{
projectionBasedResult = false;
foreach (PropertyInfo propertyInfo in properties)
{
results.Columns.Add(new SearchResults.Column
{ Name = propertyInfo.Name, Type = propertyInfo.PropertyType });
}
}
else
{
projectionBasedResult = true;
foreach (string propertyName in propertyNames)
{
results.Columns.Add(new SearchResults.Column
{ Name = propertyName, Type = properties.Find(x => string.Equals
(x.Name, propertyName,
StringComparison.OrdinalIgnoreCase)).PropertyType });
}
}
}
public object TransformTuple(object[] tuple, string[] aliases)
{
Dictionary<string, object> row = new Dictionary<string,
object>();
if (projectionBasedResult)
{
for (int i = 0; i < results.Columns.Count; i++)
{
SearchResults.Column column = results.Columns[i];
row.Add(column.Name, tuple[i]);
}
}
else
{
object entity = tuple[0];
for (int i = 0; i < results.Columns.Count; i++)
{
SearchResults.Column column = results.Columns[i];
object value = properties.Find(x => string.Equals
(x.Name, column.Name, StringComparison.OrdinalIgnoreCase)).GetValue
(entity, null);
row.Add(column.Name, value);
}
}
results.Rows.Add(row);
return null;
}
public IList TransformList(IList collection)
{
return new List<SearchResults>() { results };
}
}
Could there be something I am missing to cause it to return the full
entity in the criteria?
On Aug 13, 9:18 am, Fabio Maulo <[email protected]> wrote:
> Default implementation of ResultTransformer are for some easy and normal
> cases.If you need something special you should implement your own
> ResultTrasformer (more easy to do than explain it).
>
> 2009/8/13 Colin Bowern <[email protected]>
>
>
>
>
>
>
>
> > The actual object (not the example) has a rather large data set that
> > is used during most processing. I am looking to retrieve a subset of
> > the properties for some bulk operations. The SQL generated by the
> > call is correct - the problem resides in mapping the results back to
> > the entity. The AliasToBean class does not understand how to deal
> > with an element from a complex property like a class or struct. If I
> > can extend the behavior of the AliasToBean process to look for "." and
> > navigate the sub-properties accordingly it would be sufficient.
>
> > Cheers,
> > Colin
>
> > On Aug 13, 12:45 am, Fabio Maulo <[email protected]> wrote:
> > > You want something that is not an entity. What you want ? an entity or
> > > something else ?
>
> > > 2009/8/12 Colin Bowern <[email protected]>
>
> > > > I'd like to avoid the DTOs if possible. Are there any examples of
> > > > extending the AliasToBean transformer to deal with situations like
> > > > this?
>
> > > > On Aug 12, 6:37 pm, armin-landscheidt <[email protected]> wrote:
> > > > > Try to use a third class:
>
> > > > > class FooBar{
> > > > > public long Id{get;set;}
> > > > > public string Name{get;set;}
> > > > > public string BarCode{get;set;}
>
> > > > > }
>
> > > > > IList<FooBar> result = session.CreateCriteria(typeof(Foo))
> > > > > .CreateAlias("Bar", "Bar")
> > > > > .SetProjection(Projections.ProjectionList()
> > > > > .Add(Projections.Property("Id"), "Id")
> > > > > .Add(Projections.Property("Name"), "Name")
> > > > > .Add(Projections.Property("Bar.Code"), "BarCode"))
>
> > .SetResultTransformer(Transformers.AliasToBean(typeof(FooBar)))
> > > > > .List<FooBar>();
>
> > > > > This is for creating DTOs.
>
> > > > > > If I have two classes:
>
> > > > > > public class Foo
> > > > > > {
> > > > > > public virtual int Id { get; set; }
> > > > > > public virtual string Name { get; set; }
> > > > > > public virtual Bar Bar { get; set; }
> > > > > > }
>
> > > > > > public class Bar
> > > > > > {
> > > > > > public virtual int Id { get; set; }
> > > > > > public virtual string Code { get; set; }
> > > > > > public virtual string Name { get; set; }
> > > > > > }
>
> > > > > > And I want to only return Foo.Id, Foo.Name and Bar.Code (ignoring
> > > > > > Bar.Id and Bar.Name) I was thinking that projections would be the
> > way
> > > > > > to go:
>
> > > > > > IList<Foo> result = session.CreateCriteria(typeof(Foo))
> > > > > > .CreateAlias("Bar", "Bar")
> > > > > > .SetProjection(Projections.ProjectionList()
> > > > > > .Add(Projections.Property("Id"), "Id")
> > > > > > .Add(Projections.Property("Name"), "Name")
> > > > > > .Add(Projections.Property("Bar.Code"), "Bar.Code"))
>
> > .SetResultTransformer(Transformers.AliasToBean(typeof(Foo)))
> > > > > > .List<Foo>();
>
> > > > > > When I run this, however, the transformer complains of not being
> > able
> > > > > > to find a setter for the property Bar.Code. I understand this is
> > > > > > because the projection cannot resolve the sub-property. I can't
> > find
> > > > > > much of a reference on how to do this sort of query. Any thoughts?
>
> > > > > > Thanks,
> > > > > > Colin- Hide quoted text -
>
> > > > > - Show quoted text -
>
> > > --
> > > Fabio Maulo- Hide quoted text -
>
> > > - Show quoted text -
>
> --
> Fabio Maulo- Hide quoted text -
>
> - Show quoted text -
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en
-~----------~----~----~----~------~----~------~--~---