Showing posts with label SubSonic. Show all posts
Showing posts with label SubSonic. Show all posts

Tuesday, November 27, 2007

How to use the SubSonic Collection Find() method with Predicate

Here is a code example to find a particular item within a collection of entities.




Instead of creating a loop to go over the collection to find your entity, you can take advantage of the Predicates.

Model.Person foundPerson =(Model.Person) personCollection.Find(delegate(Model.Person e) { return e.FirstName == "John"; });




I hope this is helpful. I did not find anything in the SubSonic forums regarding the Find method. This worked for me instead of creating a loop to go over all items in the collection.

Monday, November 26, 2007

SubSonic Updated Templates to Delete Entities With Children

In a recent project that I worked on I had a need to delete a top entity that had multiple children. With the generated code out of the box from SubSonic I found it difficult to delete the parent entity and all the children that depend on the parent entity since I had to specifically invoke the child entity for a delete operation. So I decided to mess with the templates. In particular with the 'Controller' (CS_ODSController.aspx).

I decided to create a method named "DeleteWithChildren()" that will take the primary key id of the parent entity to follow the same controller pattern already in place similar methods.


DeleteWithChildren() will go load the parent entity using the primary key and then invoke the corresponding controller class for each of each children.

For example,



public void DeleteWithChildren(object keyID)
{

// Load the entity
MyCompany.Model.Employee entity = new MyCompany.Model.Employee(keyID);

foreach(MyCompany.Model.SiteEmployee child in entity.SiteEmployeeRecords)
{


MyCompany.Controller.SiteEmployeeCtrl.Instance.DeleteWithChildren(child.SiteEmployeeId);

}


}




This allowed me to simple invoke the parent entity controller such as Controller.ParentEntity.DeleteWithChildren(5000); and have the controller delete everything the hangs off the ParentEntity object by calling the DeleteWithChildren() method for each child. This allows to delete hierarchical entities.

You can download the template file from my online box here.

My template was code to first check that a transaction was started since you might not want to attempt to delete everything from that entity without a transaction.

Tuesday, November 06, 2007

SubSonic - Using the Exist() Method using predicates

Here is a simple example on how to use the Exist() method that hangs of the collection entities generated by SubSonic.




for (int i = 0; i < originalentitycollection.Count; i++)
{
Predicate exist = delegate(Model.Person match)
{
if (match.PersonId == originalentitycollection[i].PersonId)
return true;
else return false;
};


if (!distinctpersoncollection.exists(exist))
{

distinctpersoncollection.add(originalentitycollection[i]);
}
}





You can visit MSDN for more information about the predicate delegates:
http://msdn2.microsoft.com/en-us/library/bfcke1bz.aspx