Adaptor Code vs Lazy approach

Posts   
 
    
Hameed
User
Posts: 34
Joined: 02-May-2005
# Posted on: 16-May-2005 15:01:45   

I have been using the selfservice template for a while now. Due to a requirement for PocketPC support I have started to use the adaptor template. Largely due to problems with SqlCE only supporting a single connection and no pooling.

I like Lazy Loading!!!!!

I have a Contact Entity which has a FK relationship with Purpose, Location and Type entities. Each of the entities also have some sub entities. Now with Selfservice I just traversed the entities collection and it gave me the entities filled in. With adaptor this is not the case.

Could I request someone check that I am doing this right? Is this the way entities should be filled in. In addition is there no way of just saying fill all child entities with out so much code.

            EntityCollection patients = new EntityCollection(new PatientEntityFactory());
            IPrefetchPath2 ppath = new PrefetchPath2((int)EntityType.PatientEntity);
            IPrefetchPathElement2 pcon = ppath.Add(PatientEntity.PrefetchPathContact);
            pcon.SubPath.Add(ContactEntity.PrefetchPathLocationType);
            pcon.SubPath.Add(ContactEntity.PrefetchPathPurposeType);
            pcon.SubPath.Add(ContactEntity.PrefetchPathContactType);

            pcon.SubPath.Add(ContactEntity.PrefetchPathUser).SubPath.Add(UserEntity.PrefetchPathMedicalStaff);
            
            ISortExpression sorterCase = new SortExpression();
            sorterCase.Add(SortClauseFactory.Create(CaseFieldIndex.CreatedDate, SortOperator.Descending));

            pcon.SubPath.Add(ContactEntity.PrefetchPathCase,1, null, null, sorterCase).SubPath.Add(CaseEntity.PrefetchPathCaseType);

            ISortExpression sorterCategory = new SortExpression();
            sorterCategory.Add(SortClauseFactory.Create(CategoryFieldIndex.CreatedDate, SortOperator.Descending));
                        
            pcon.SubPath.Add(ContactEntity.PrefetchPathCategory,1, null, null, sorterCategory).SubPath.Add(CategoryEntity.PrefetchPathCategoryType);                        

            IRelationPredicateBucket filter = new RelationPredicateBucket();
            filter.PredicateExpression.Add(PredicateFactory.CompareValue(PatientFieldIndex.Patient_ID, ComparisonOperator.Equal,mPatient.Patient_ID));

            InstanceManager.DataAccessAdaptor.FetchEntityCollection(patients,filter,ppath);

I am using the Prefecth path to fill the entities.

For a particular patient entity I am getting all associated contacts and other child entitites. I had to create a point-less entity collection entity called patients, although I have a patient entity, mPatient, passed into the method.

Thanks.

jeffreygg
User
Posts: 805
Joined: 26-Oct-2003
# Posted on: 16-May-2005 20:25:46   

No, that's pretty much it. simple_smile I just scanned your code so I'm not sure if every line of code is necessary; but, in general, yes, constructing prefetch paths is fairly verbose.

The upside is that you shouldn't have to write a whole lot of code like this. Adapter enables one to write well-encapsulated logic. If this pattern is followed well, reusability should go way up resulting in - hopefully - less overall code.

The problem with just populating all child entities is - where do you stop? One idea I had was to have an argument like "PrefetchPathDepth" such that one could specify that all related entities up to n deep would be fetched into the graph.


adapter.FetchEntityCollection(entities, path, prefetchPathDepth = 2)

How do you think that would work for you?

Jeff...

Hameed
User
Posts: 34
Joined: 02-May-2005
# Posted on: 17-May-2005 10:12:01   

Thanks Jeff,

The upside is that you shouldn't have to write a whole lot of code like this. Adapter enables one to write well-encapsulated logic. If this pattern is followed well, reusability should go way up resulting in - hopefully - less overall code.

Do you mean that I should be creating business layer for each dal entity?

The problem with just populating all child entities is - where do you stop?

If a method or enum exists that would be passed in as a parameter which lets the adaptor know, for this parent entity fill all child entities that are related. I understand this may cause a performance issue but for entities which are only 2 or 3 level deep I'm guessing this would be OK.

One idea I had was to have an argument like "PrefetchPathDepth" such that one could specify that all related entities up to n deep would be fetched into the graph.

Code:

adapter.FetchEntityCollection(entities, path, prefetchPathDepth = 2)

How do you think that would work for you?

I think this would be great in resolving my issue of verbose code. I guess this does not exist for current adaptor template. How would this be implemented.

Hameed.

jeffreygg
User
Posts: 805
Joined: 26-Oct-2003
# Posted on: 17-May-2005 18:49:38   

Hameed wrote:

Thanks Jeff,

The upside is that you shouldn't have to write a whole lot of code like this. Adapter enables one to write well-encapsulated logic. If this pattern is followed well, reusability should go way up resulting in - hopefully - less overall code.

Do you mean that I should be creating business layer for each dal entity?

You should structure your business layer as your needs require it. But, the end result should still be the same - your fetch logic should be consolidated and reused as much as possible.

The problem with just populating all child entities is - where do you stop?

If a method or enum exists that would be passed in as a parameter which lets the adaptor know, for this parent entity fill all child entities that are related. I understand this may cause a performance issue but for entities which are only 2 or 3 level deep I'm guessing this would be OK.

One idea I had was to have an argument like "PrefetchPathDepth" such that one could specify that all related entities up to n deep would be fetched into the graph.

Code:

adapter.FetchEntityCollection(entities, path, prefetchPathDepth = 2)

How do you think that would work for you?

I think this would be great in resolving my issue of verbose code. I guess this does not exist for current adaptor template. How would this be implemented.

Hameed.

Not really sure as it's just an idea right now. We'll see if Frans picks it up and decides to implement it in a future version. simple_smile

Jeff...