Default Collection Parameters

Posts   
 
    
Posts: 1268
Joined: 10-Mar-2006
# Posted on: 18-Apr-2006 18:40:52   

Given an Entity with a 1:N relationship, you can reference properties of the entity to get back the items in that relationship:


MyEntity entity = new MyEntity(1);
Grid.DataSource=entity.MyNCollection;

If I want the collection to be sorted by some particular field, so I do this:


MyEntity entity = new MyEntity(1);
entity.SetCollectionParametersMyNCollection(0, new SortExpression(MyNFields.SomeField));
Grid.DataSource=entity.MyNCollection;

And that works fine. However, for 99% of all consumers of this collection, it should be sorted by 'SomeField' and I would like to default it to that, so when the Entity is created that sort is setup by default. Ideally, this would be something you setup in the LLBLGen GUI designer. However, since that feature is not available, I wanted to just set that sort when the MyEntity is created. There are no 'user code' sections in the constructors for the class - where is the best place to set something like this up?

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 19-Apr-2006 04:07:24   

I would personally setup a prefetch for the MyNCollection that was sorted. Something like


IPrefetchPath path = new PrefetchPath((int)EntityType.MyEntity);
ISortExpression sorter = new SortExpression(MyNFields.SomeField | SortOperator.Ascending);
path.Add(MyEntity.PrefetchPathMyNCollection, 0, null, null, sorter);

As far as user code regions for constructors, you should be able to use

__LLBLGENPRO_USER_CODE_REGION_START InitClassEmpty and __LLBLGENPRO_USER_CODE_REGION_START InitClassFetch

One of these user code regions are called by the constructors.