field ordering in EF templates

Posts   
 
    
MJC
User
Posts: 50
Joined: 06-May-2004
# Posted on: 22-Aug-2011 17:34:49   

some of the templates (like ste and poco entity templates) don't implement field ordering

Searching for: entity.Fields.Where(f=>!f.IsDiscriminator).OrderBy(f=>f.Name).ToList();

C:\Program Files (x86)\Solutions Design\LLBLGen Pro v3.1\Frameworks\Entity Framework\Templates\Shared\Shared\edmxFileInclude.lpt(305): var fieldsToTraverse = entity.Fields.Where(f=>!f.IsDiscriminator).OrderBy(f=>f.Name).ToList(); C:\Program Files (x86)\Solutions Design\LLBLGen Pro v3.1\Frameworks\Entity Framework\Templates\V4\C#\pocoEntityClass.lpt(12): //var entityFields = entity.Fields.Where(f=>!f.IsDiscriminator).OrderBy(f=>f.Name).ToList();
C:\Program Files (x86)\Solutions Design\LLBLGen Pro v3.1\Frameworks\Entity Framework\Templates\V4\C#\steEntityClass.lpt(12): var entityFields = entity.Fields.Where(f=>!f.IsDiscriminator).OrderBy(f=>f.Name).ToList(); C:\Program Files (x86)\Solutions Design\LLBLGen Pro v3.1\Frameworks\Entity Framework\Templates\V4\Shared\typeInfoListPage.lpt(111): <% var fields = entity.Fields.Where(f=>!f.IsDiscriminator).OrderBy(f=>f.Name).ToList(); C:\Program Files (x86)\Solutions Design\LLBLGen Pro v3.1\Frameworks\Entity Framework\Templates\V4\VB.NET\pocoEntityClass.lpt(12): var entityFields = entity.Fields.Where(f=>!f.IsDiscriminator).OrderBy(f=>f.Name).ToList(); C:\Program Files (x86)\Solutions Design\LLBLGen Pro v3.1\Frameworks\Entity Framework\Templates\V4\VB.NET\steEntityClass.lpt(12): var entityFields = entity.Fields.Where(f=>!f.IsDiscriminator).OrderBy(f=>f.Name).ToList();

Found 6 occurrence(s) in 6 file(s)

Could we change those lines (in the shipped templates) to either:

var entityFields = entity.Fields.Where(f=>!f.IsDiscriminator).OrderBy(f=>f.FieldIndex).ToList(); or var entityFields = entity.Fields.Where(f=>!f.IsDiscriminator).ApplyFieldOrdering().ToList();

The 2nd one seems to be the default in the other templates.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 22-Aug-2011 18:55:18   

Could you elaborate a bit why you would need field ordering in these contexts? .NET doesn't have any ordering in properties for example, hence we didn't implement field ordering in templates which only output .NET code.

Frans Bouma | Lead developer LLBLGen Pro
MJC
User
Posts: 50
Joined: 06-May-2004
# Posted on: 22-Aug-2011 19:30:22   

There are factory methods to create entities for example that could benefit from field ordering, that's probably the primary use-case.

example: line 113 of pocoEntityClass.lpt which iterates mandatory fields (using foreach on entityFields)

public static <%=entity.Name%> Create<%=entity.Name%>(<%=string.Join(", ", parameterFragments.ToArray())%>)

Ordering in the designer allows me to control how things are setup in those factory methods. This helps me code generate things in other custom templates that depend on these methods.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 24-Aug-2011 11:40:18   

Ok. There's currently a problem, we can't fix this without breaking existing applications, so we won't fix this in the current version. We've added a workitem for v3.5 to see if we can fix it in that version (if time permits).

For the time being, please add the ordering manually to the template, by copying the template, making the changes and creating a small custom templatebindings file which binds the templateid for the template you're altering to the copy of the template. Place the template bindings file above the normal one on tab 2 of the generation configuration dialog when generating code.

Frans Bouma | Lead developer LLBLGen Pro
MJC
User
Posts: 50
Joined: 06-May-2004
# Posted on: 24-Aug-2011 16:11:45   

Thanks!

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 17-Jan-2012 10:28:09   

We've decided not to implement this change. The reason is that by applying this change, code will break and potentially at runtime only (all integer fields, fields change order: compilation succeeds). This is a problem which is hard to track down, and although it's an edge case, we find it's not something we should enforce onto users as it's not optional.

Additionally, the fields are ordered predictably, namely by name. Generating calls to the factory methods isn't a problem as you can simply order all non-optional fields by name for the call.

Frans Bouma | Lead developer LLBLGen Pro
MJC
User
Posts: 50
Joined: 06-May-2004
# Posted on: 17-Jan-2012 17:53:07   

I understand the problem. As the dictator of my little universe though, I want to force this change on everybody, and it's everybody's fault if they don't read the release notes and refactor their code stuck_out_tongue_winking_eye Just kidding of course.simple_smile It's fine, we can work around this. Thanks for considering it though.