Inferred column name from expressions

2 views
Skip to first unread message

Marcus Rehm

unread,
Dec 14, 2009, 3:48:03 PM12/14/09
to mvccontrib-discuss
Hi,

There is a way to set the ColumnBuilder to infer the name of the
column with the "full name" like bellow?
If I set:
_builder.For(x => x.DateOfBirth.Day);
_builder.Single().Name.ShouldEqual("DateOfBirth.Day");

In the actual implementation _builder.For(x => x.DateOfBirth.Day)
infer the name as "Day".
Other situation is when I call:
_builder.For(x => x.Name);
_builder.For(x => x.Address.Street.Name);
Both columns has the same name "Name";

I'm working on a grid render for jqGrid and when i'm posting the data
for sorting, i can't get the right column. The other point is that if
the name inferred was "DateOfBirth.Day" it can be passed as it is to
the sort expression on Linq.

Thankx in advance!

Jeremy Skinner

unread,
Dec 14, 2009, 4:46:02 PM12/14/09
to mvccontri...@googlegroups.com
I'm afraid this isn't currently possible. The ColumnBuilder always infers the name of the column based on the "Name" property of the MemberExpression. Based on an expression of x => x.DateOfBirth.Day, this will always return "Day". If you need to specify a custom column name, you'll need to use the .Named() method.

Jeremy

2009/12/14 Marcus Rehm <marcu...@gmail.com>

--
Contact Jeffrey Palermo or Eric Hexter with specific questions about the MvcContrib project.  Or go to http://mvccontrib.org

To unsubscribe from this group, send email to mvccontrib-disc...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/mvccontrib-discuss?hl=en

Marcus Rehm

unread,
Dec 15, 2009, 7:44:17 AM12/15/09
to mvccontrib-discuss
I understand Jeremy, but what if we change the ColumnBuilder method
"public IGridColumn<T> For(Expression<Func<T, object>>
propertySpecifier)" as bellow?

public IGridColumn<T> For(Expression<Func<T, object>>
propertySpecifier)
{
var memberExpression = GetMemberExpression(propertySpecifier);
var type = GetTypeFromMemberExpression(memberExpression);
var inferredName = memberExpression == null ? null :
memberExpression.ToString().Substring(

memberExpression.ToString().IndexOf(

propertySpecifier.Parameters[0].Name + ".") +

propertySpecifier.Parameters[0].Name.Length + 1);

var column = new GridColumn<T>(propertySpecifier.Compile(),
inferredName, type);
_columns.Add(column);
return column;
}

I runned all the test cases and all gone green, only the test
Should_infer_column_name_from_lambda in ColumnBuilderTester I changed
to expect "DateOfBirth.Day".
I think use the parameter[0] is not a problem as in this case we will
only have one parameter always.

Can you see another issue where i can't use this approach?

Thankx for the help!
Marcus



On Dec 14, 6:46 pm, Jeremy Skinner <jer...@jeremyskinner.co.uk> wrote:
> I'm afraid this isn't currently possible. The ColumnBuilder always infers
> the name of the column based on the "Name" property of the MemberExpression.
> Based on an expression of x => x.DateOfBirth.Day, this will always return
> "Day". If you need to specify a custom column name, you'll need to use the
> .Named() method.
>
> Jeremy
>
> 2009/12/14 Marcus Rehm <marcus.r...@gmail.com>
>
> > Hi,
>
> > There is a way to set the ColumnBuilder to infer the name of the
> > column with the "full name" like bellow?
> > If I set:
> > _builder.For(x => x.DateOfBirth.Day);
> > _builder.Single().Name.ShouldEqual("DateOfBirth.Day");
>
> > In the actual implementation _builder.For(x => x.DateOfBirth.Day)
> > infer the name as "Day".
> > Other situation is when I call:
> > _builder.For(x => x.Name);
> > _builder.For(x => x.Address.Street.Name);
> > Both columns has the same name "Name";
>
> > I'm working on a grid render for jqGrid and when i'm posting the data
> > for sorting, i can't get the right column. The other point is that if
> > the name inferred was "DateOfBirth.Day" it can be passed as it is to
> > the sort expression on Linq.
>
> > Thankx in advance!
>
> > --
> > Contact Jeffrey Palermo or Eric Hexter with specific questions about the
> > MvcContrib project.  Or go tohttp://mvccontrib.org

Jeremy Skinner

unread,
Dec 16, 2009, 3:15:08 AM12/16/09
to mvccontri...@googlegroups.com
Yes, this would work fine.  The problem is that it changes the existing behaviour that other people may rely on (I know my own projects rely on the current behaviour) so introducing this change would require people go back and re-write their pages. A better approach might be to make the name resolution pluggable so you can override the default method for obtaining the name from the expression.

If you want to create an issue for this on the CodePlex issue tracker then I'll try and get this implemented (but it probably won't be sometime until the new year)

Jeremy


2009/12/15 Marcus Rehm <marcu...@gmail.com>
Contact Jeffrey Palermo or Eric Hexter with specific questions about the MvcContrib project.  Or go to http://mvccontrib.org

Marcus Rehm

unread,
Dec 17, 2009, 3:51:08 PM12/17/09
to mvccontrib-discuss
Ok Jeremy! I'll try this approach overriding the method.

Thankx a lot!


On Dec 16, 5:15 am, Jeremy Skinner <jer...@jeremyskinner.co.uk> wrote:
> Yes, this would work fine.  The problem is that it changes the existing
> behaviour that other people may rely on (I know my own projects rely on the
> current behaviour) so introducing this change would require people go back
> and re-write their pages. A better approach might be to make the name
> resolution pluggable so you can override the default method for obtaining
> the name from the expression.
>
> If you want to create an issue for this on the CodePlex issue tracker then
> I'll try and get this implemented (but it probably won't be sometime until
> the new year)
>
> Jeremy
>

> 2009/12/15 Marcus Rehm <marcus.r...@gmail.com>

Reply all
Reply to author
Forward
0 new messages