Passing var's around
-
Forgive the stupid title, but let me explain... I have the following code (mostly lifted from a Linq example):
var nestedGroup = from update in updates group update by update.SupplierName into group1 from group2 in (from update in group1 group update by update.MailBoxNumber ) group group2 by group1.Key;
And, rather neatly, it does what I want in that it creates a tree like structure. Nice. However, as with a lot of data, eventually you want to visualise it (in this case a treeview seemingly the optimal display mechanism). All well and good, but, but... ...since it's an inferred type, you can't pass it anywhere as it's local to this method - and as I'm learning all about the benefits of UI patterns and the seperations of concerns, it would be a very bad move for me to have data access and or sorting logic in the UI layer (or is this, would you argue, actually presentation logic?). Now I know thatvar
is, for the most part, just a convenience and the type it actually encapsulates in this instance is an IEnumerable>>, but I really don't want to write a method that takes that kind of a coupling... So how do you correctly move this kind of data around your application and across tiers? -
Forgive the stupid title, but let me explain... I have the following code (mostly lifted from a Linq example):
var nestedGroup = from update in updates group update by update.SupplierName into group1 from group2 in (from update in group1 group update by update.MailBoxNumber ) group group2 by group1.Key;
And, rather neatly, it does what I want in that it creates a tree like structure. Nice. However, as with a lot of data, eventually you want to visualise it (in this case a treeview seemingly the optimal display mechanism). All well and good, but, but... ...since it's an inferred type, you can't pass it anywhere as it's local to this method - and as I'm learning all about the benefits of UI patterns and the seperations of concerns, it would be a very bad move for me to have data access and or sorting logic in the UI layer (or is this, would you argue, actually presentation logic?). Now I know thatvar
is, for the most part, just a convenience and the type it actually encapsulates in this instance is an IEnumerable>>, but I really don't want to write a method that takes that kind of a coupling... So how do you correctly move this kind of data around your application and across tiers?martin_hughes wrote:
the type it actually encapsulates in this instance is an IEnumerable> >
Actually, it's not encapsulating -- it really is that type! If you look at the IL in Reflector, you'll see it really is that type. So really your concern is passing around this IEnumerable that contains many nested generic types. I agree that for code readability, you shouldn't pass around this type. Best way to get around this is create a class that wraps up this whole
IGrouping>
stuff.class SuppliersWithMailBox
{
...
}You'll then pass around IEnumerable.
-
martin_hughes wrote:
the type it actually encapsulates in this instance is an IEnumerable> >
Actually, it's not encapsulating -- it really is that type! If you look at the IL in Reflector, you'll see it really is that type. So really your concern is passing around this IEnumerable that contains many nested generic types. I agree that for code readability, you shouldn't pass around this type. Best way to get around this is create a class that wraps up this whole
IGrouping>
stuff.class SuppliersWithMailBox
{
...
}You'll then pass around IEnumerable.
Ahhh, lateral thinking at its best; so instead of:
var = blah blah bla
It'd be:IEnumerable <supplierswithmailbox> thingyMaBob = blah blah blah
I'll give it a try. Thanks! -
Ahhh, lateral thinking at its best; so instead of:
var = blah blah bla
It'd be:IEnumerable <supplierswithmailbox> thingyMaBob = blah blah blah
I'll give it a try. Thanks!Something like that. You may need to still do:
var nestedGroup = blah blah blah;
var suppliersWithMailBoxes = ConvertNestedGroupToSuppliers(); // something like this.You get the idea.