Re-use of PrefetchPaths

Posts   
 
    
swegele
User
Posts: 33
Joined: 25-Jan-2007
# Posted on: 01-Jun-2012 09:42:22   

Using LLBLGEN 2.5 with SQL 2008R2

Not sure if I am way out in left field here...I am wondering how I would re-use some/most of this prefetch path:

        private static PrefetchPath2 GetPrefetchPath()
        {
            PrefetchPath2 prefetchObject = new PrefetchPath2(IRB.DAL.EntityType.CSREntity);
            IPrefetchPathElement2 prefetchSubPath;

            //primary investigator (we only want the latest active)
            prefetchSubPath = prefetchObject.Add(IRB.DAL.EntityClasses.CSREntity.PrefetchPathCSRPrimaryInvestigatorSnapshot);
            prefetchSubPath.Filter.Add(IRB.DAL.HelperClasses.CSRPrimaryInvestigatorSnapshotFields.RecordActive == true);
            prefetchSubPath.Sorter.Add(new SortClause(IRB.DAL.HelperClasses.CSRPrimaryInvestigatorSnapshotFields.InsertDate, null, SortOperator.Descending));
            prefetchSubPath.MaxAmountOfItemsToReturn = 1;
            prefetchSubPath = prefetchSubPath.SubPath.Add(IRB.DAL.EntityClasses.CSRPrimaryInvestigatorSnapshotEntity.PrefetchPathInvestigator);

            //main location (we only want the latest active)
            prefetchSubPath = prefetchObject.Add(IRB.DAL.EntityClasses.CSREntity.PrefetchPathCSRMainLocationSnapshot);
            prefetchSubPath.Filter.Add(IRB.DAL.HelperClasses.CSRMainLocationSnapshotFields.RecordActive == true);
            prefetchSubPath.Sorter.Add(new SortClause(IRB.DAL.HelperClasses.CSRMainLocationSnapshotFields.InsertDate, null, SortOperator.Descending));
            prefetchSubPath.MaxAmountOfItemsToReturn = 1;
            prefetchSubPath = prefetchSubPath.SubPath.Add(IRB.DAL.EntityClasses.CSRMainLocationSnapshotEntity.PrefetchPathLocation);

            //company
            prefetchSubPath = prefetchObject.Add(IRB.DAL.EntityClasses.CSREntity.PrefetchPathCompany);

            //Appreqs
            prefetchSubPath = prefetchObject.Add(IRB.DAL.EntityClasses.CSREntity.PrefetchPathLinkCSRToCSRApplicationRequirement);
            prefetchSubPath.Filter.Add(IRB.DAL.HelperClasses.LinkCSRToCSRApplicationRequirementFields.Fulfilled == false);

            return prefetchObject;
        }

This prefetch path starts at the CSREntity.

But what if I start a query one level higher on UploadFile where CSREntity is now the child and all the other stuff is now grandchild...like this: 1. CSREntity as root then -> Primary Investigator & Main Location as children 2. UploadFileEntity as root then -> CSREntity as child then -> Primary Investigator & Main Location as grandchildren.

The problem is the child level in the first example starts with a PrefetchPath2.Add, whereas the second example grandchild level would be expecting IPrefetchPathElement2.Add. So I can't figure out a way to re-use the GetPrefetchPath. So if I copy and paste...now I have two places to remember to edit my prefetch paths if something changes. Ugh

Am I making any sense at all? It's late :-)

Sean

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 01-Jun-2012 12:20:05   

What about the following:

One way:

private void MyMethod()
{
var prefetch = new PrefetchPath(EntityType.CustomerEntity);
GetCustomersPrefetchs();
}

private void GetCustomersPrefetchs(IPrefetchPath path)
{
path.Add(CustomerEntity.PrefetchPathOrders).SubPath.Add(GetOrdersPrefetchs(path));
}

private void GetOrdersPrefetchs(IPrefetchPath path)
{
path.Add(OrderEntity.PrefetchPathOrderDetailEntity);
}

Another way:

private void MyMethod()
{
var prefetch = new PrefetchPath(EntityType.CustomerEntity);
prefetch.Add(GetCustomersPrefetchs());

}

private IPrefetchPathElement GetCustomersPrefetchs()
{
return CustomerEntity.PrefetchPathOrders.SubPath.Add(GetOrdersPrefetchs());
}

private IPrefetchPathElement GetOrdersPrefetchs()
{
return OrderEntity.PrefetchPathOrderDetailEntity;
}
swegele
User
Posts: 33
Joined: 25-Jan-2007
# Posted on: 01-Jun-2012 20:32:31   

OK Walaa based on what you said I tried this:

        private static IPrefetchPathElement2 GetPIPrefetch()
        {
            IPrefetchPathElement2 prefetchPath;

            //primary investigator (we only want the latest active)
            prefetchPath = IRB.DAL.EntityClasses.CSREntity.PrefetchPathCSRPrimaryInvestigatorSnapshot;
            prefetchPath.Filter.Add(IRB.DAL.HelperClasses.CSRPrimaryInvestigatorSnapshotFields.RecordActive == true);
            prefetchPath.Sorter.Add(new SortClause(IRB.DAL.HelperClasses.CSRPrimaryInvestigatorSnapshotFields.InsertDate, null, SortOperator.Descending));
            prefetchPath.MaxAmountOfItemsToReturn = 1;
            prefetchPath.SubPath.Add(IRB.DAL.EntityClasses.CSRPrimaryInvestigatorSnapshotEntity.PrefetchPathInvestigator);

            return prefetchPath;
        }

        private static IPrefetchPathElement2 GetMainLocationPrefetch()
        {
            IPrefetchPathElement2 prefetchPath;

            //main location (we only want the latest active)
            prefetchPath = IRB.DAL.EntityClasses.CSREntity.PrefetchPathCSRMainLocationSnapshot;
            prefetchPath.Filter.Add(IRB.DAL.HelperClasses.CSRMainLocationSnapshotFields.RecordActive == true);
            prefetchPath.Sorter.Add(new SortClause(IRB.DAL.HelperClasses.CSRMainLocationSnapshotFields.InsertDate, null, SortOperator.Descending));
            prefetchPath.MaxAmountOfItemsToReturn = 1;
            prefetchPath.SubPath.Add(IRB.DAL.EntityClasses.CSRMainLocationSnapshotEntity.PrefetchPathLocation);

            return prefetchPath;
        }

        private static IPrefetchPathElement2 GetCompanyPrefetch() 
        {
            return IRB.DAL.EntityClasses.CSREntity.PrefetchPathCompany; 
        }

        private static IPrefetchPathElement2 GetAppReqsPretch()
        {
            IPrefetchPathElement2 prefetchSubPath = IRB.DAL.EntityClasses.CSREntity.PrefetchPathLinkCSRToCSRApplicationRequirement;
            prefetchSubPath.Filter.Add(IRB.DAL.HelperClasses.LinkCSRToCSRApplicationRequirementFields.Fulfilled == false);
            return prefetchSubPath;
        }

        private static PrefetchPath2 GetPrefetchPath()
        {
            PrefetchPath2 prefetchObject = new PrefetchPath2(IRB.DAL.EntityType.CSREntity);

            prefetchObject.Add(GetPIPrefetch());
            prefetchObject.Add(GetMainLocationPrefetch());
            prefetchObject.Add(GetCompanyPrefetch());
            prefetchObject.Add(GetAppReqsPretch());

            return prefetchObject;
        }

        private static IPrefetchPathElement2 GetPrefetchPathForUploadFile()
        {
            IPrefetchPathElement2 prefetchPath = IRB.DAL.EntityClasses.UploadFileEntity.PrefetchPathCSR;
            prefetchPath.SubPath.Add(GetPIPrefetch());
            prefetchPath.SubPath.Add(GetMainLocationPrefetch());
            prefetchPath.SubPath.Add(GetCompanyPrefetch());
            prefetchPath.SubPath.Add(GetAppReqsPretch());

            return prefetchPath;
        }

That is better...but is there anyway to go one step further. Notice how the code in the two bottom methods looks awfully similar.


            prefetchObject.Add(GetPIPrefetch());
            prefetchObject.Add(GetMainLocationPrefetch());
            prefetchObject.Add(GetCompanyPrefetch());
            prefetchObject.Add(GetAppReqsPretch());

vs


            prefetchPath.SubPath.Add(GetPIPrefetch());
            prefetchPath.SubPath.Add(GetMainLocationPrefetch());
            prefetchPath.SubPath.Add(GetCompanyPrefetch());
            prefetchPath.SubPath.Add(GetAppReqsPretch());

The GetPrefetchPathForUploadFile is called from outside this object...from the UploadFile object which is building its own prefetch.

If I can't refactor/reuse anymore then that is OK

Thanks, Sean

swegele
User
Posts: 33
Joined: 25-Jan-2007
# Posted on: 01-Jun-2012 21:59:58   

I figured it out :-) I am so anal!!

        private static List<IPrefetchPathElement2> GetPrefetchPathList()
        {
            List<IPrefetchPathElement2> list = new List<IPrefetchPathElement2>();

            list.Add(GetPIPrefetch());
            list.Add(GetMainLocationPrefetch());
            list.Add(GetCompanyPrefetch());
            list.Add(GetAppReqsPretch());

            return list;
        }

        private static PrefetchPath2 GetPrefetchPath()
        {
            PrefetchPath2 prefetchObject = new PrefetchPath2(IRB.DAL.EntityType.CSREntity);

            foreach (IPrefetchPathElement2 item in GetPrefetchPathList())
            {
                prefetchObject.Add(item);
            }

            return prefetchObject;
        }

        internal static IPrefetchPathElement2 GetPrefetchPathForUploadFile()
        {
            IPrefetchPathElement2 prefetchPath = IRB.DAL.EntityClasses.UploadFileEntity.PrefetchPathCSR;

            foreach (IPrefetchPathElement2 item in GetPrefetchPathList())
            {
                prefetchPath.SubPath.Add(item);
            }

            return prefetchPath;
        }

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 01-Jun-2012 23:07:41   

Good you figured it out wink

David Elizondo | LLBLGen Support Team