How to pass Criteria object created in List class to the inner object?

How to pass Criteria object created in List class to the inner object?

Old forum URL: forums.lhotka.net/forums/t/5187.aspx


spulkundwar posted on Friday, August 01, 2008

hi all,
i am new to CSLA. I have a list class PropertyList.cs, which has a criteria class, i want to use this criteria class in the Property class. My Property class has a member of AreaList and i want to use the same criteria for loading Property list and AreaList.

Is there any way to get an object of Criteria class created in PropertyList.cs inside the Property Class, when CSLA fetches PropertyList.

Regards,
Sandy

JonStonecash replied on Friday, August 01, 2008

Let me see if I can thread this needle.  The typical pattern is to pass an instance of a criteria object to the DataPortal_Fetch method.  In the DataPortal_fetch method, you perform what ever database magic that you want to pull the needed data for the list into the .NET code.  Typically, this is SQL connection and command code that creates a safe data reader.  A common pattern is to move through the datareader to create and populate the data in each member of the list.  My personal preference is to pass the reference to the datareader into the constructor of the list member class and let it suck any goodness out of the datareader that it can.  You can also pass other parameters into the list member  constructor.  Sometimes it is useful to pass a reference to the containing list or of the root class instance that is holding the list.  For example, Department holds an EmployeeList that holds the individual Employees, with the individual Employees with each Employee holding a reference to its owning Department.  You could pass a reference in the constructor call as well to the criteria (or to one or more values stored in the criteria).  CSLA gives you that power. 

Now just because you can do this, it does not mean that you should.  The criteria objects do two very important things:  First and most obvious, the contents of the criteria object specify the criteria that is to be used to manipulate data in the database.  Second and not so obvious, the context of the criteria defines the type of the business object to be retrieved.  The typical criteria object is declared within the business object.  CSLA (in most cases) uses reflection to determine the declaring type of the criteria and uses that to handle the creation of individual business objects.  You can specify a criteria outside of the business object.  If you do that you must derive the criteria from CSLA.CriteriaBase and explicitly set the business object type in the ObjectType property of CriteriaBase.  There is a constructor on the CriteriaBase class that can be used to specify the related/owning class type.  If you are going to reuse the criteria object in a different business object, you must set the ObjectType property to the correct business object type or strnage and not so nice things will happen.

Welcome to the wonderful world of CSLA.

Jon Stonecash

JoeFallon1 replied on Friday, August 01, 2008

Jon's answer is spot on.

You do not give enough information about you BOs for us to give a definitive answer. Most of my list classes do not have a criteria class. They are child objects which take a datareader from their Parent which gets the Criteria.

e.g. An Order Parent gets a Crtieris containing OrderID. The Parent fetches the main order data and then passes the datareader to the child Lines collection to grab the order lines.

If you truly have a re-useable Criteria class then simply move it to its own namespace and Inherit from CriteriaBase - I did this years ago and I rarely have an embedded Criteria class anymore.

Joe

 

tetranz replied on Friday, August 01, 2008

JoeFallon1:
If you truly have a re-useable Criteria class then simply move it to its own namespace and Inherit from CriteriaBase - I did this years ago and I rarely have an embedded Criteria class anymore.

BTW, the generic SingleCriteria class in CSLA 3.5 is very useful for all the times when you just need a single id for criteria.  CSLA 3.5 really does reduce the amount of code required, especially with collections. Thanks Rocky.

Cheers
Ross

Copyright (c) Marimer LLC