Subclassing Csla Classes

Subclassing Csla Classes

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


Jav posted on Thursday, July 20, 2006

Here is one way to subclass a Csla class that seems to work.
Class Base_Class is a Root class inherited from Csla.BusinessBase.  Class Sub_Class is inherited from Base_Class.

In Base_Class:

1. Make the Criteria class Protected so it can be used from Sub_Class
2. In DataAccess section, separate almost all of the DataPortal_Create code into a new Method:

         Protected Sub CreateObject(param1, param2........)

         End Sub

3. Change the DataPortal_Create method to

         Protected Overrides Sub DataPortal_Create(ByVal Criteria As Object)
                  Dim crit As Criteria = CType(Criteria, Criteria)
                  CreateObject(crit.param1, crit.param2 ............)
         End Sub

4. Do the same thing with the DataPortal_Fetch by separating the implementaition code into a Protected FetchObject method.

In Sub_Class:

1. No Criteria class is created.  We will use the Protected Criteria class of the Base_Class
2. Create your Factory methods as necessary in the same way as we do. 
3. In DataAccess section, the DataPortal_Create and DataPortal_Fetch will read the same as in the Base_Class above.  The only difference will be if you need to initialize some additional variables in the Sub_Class.

Since it is the BaseClass that is totally in-charge of all data, we do not need to do anything to the Save method or to the Insert, Update, Delete methods.  Sub_Class will only access the Base_Class data through the Base_Class Properties, Base_Class Instance Variables will remain Private.  Sensitive data in Base_Class can be shielded by making the Properties of that data Protected and not offering Public access in the Sub_Class.  One can create multiple Sub_Classes on top of the same Base_Class to handle different situations.  One can even place these Sub_Classes in different Assemblies, further customizing the access.

Jav

 

malloc1024 replied on Thursday, July 20, 2006

Thanks for sharing.  This should be in a "sticky" so it doesn't get lost in this forum.

Tom Cooley replied on Thursday, July 20, 2006

Correct me if I'm wrong, but having the Criteria in the base class will result in the DataPortal creating the base class - not the sub class. The DataPortal.Create method uses reflection on the Criteria class to determine its parent class (the one it's nested inside of). In this case, it is the base class.

To accomplish your intent, you need the Criteria class to inherit from Core.CriteriaBase.

mr_lasseter replied on Thursday, July 20, 2006

Could you please provide an example of doing so?  I have been struggling with this for some time.  Thanks.

Jav replied on Thursday, July 20, 2006

Tom,

You are correct up to a point (see below).  I am using the following call in my SubClass

      Return Csla.DataPortal.Create(Of  MySubClass)(New Criteria(param1, param2......))

When I walk it through to the DataPortal Module into the function

      Public Function Create(Of T)(ByVal criteria As Object) As T

the type of T (which in my case is MySubClass) is used to create the object.

Initially there were two reason why I decided to eliminate the Criteria from SubClass:
      1. I was trying to directly call the DataPortal_xxx of the BaseClass, and the type mismatch of Criteria class was a problem.
      2. I thought the two criteria classes will be confusing

Now that I am taking the parameters from the Criteria class in the SubClass and passing them straight to a separate Method, the first reason shouldn't apply.  But I decided to continue with a single Criteria class.

Now if you did make your call as follows:
         Return Csla.DataPortal.Create(New Criteria(param1, param2......))

then you will need to include the criteria class of the SubClass, and I expect it will work too.

I must add that this project only began this morning.  So far in one object hierarchy that I am testing it with it seems to be working correctly.  If there are other repurcussions, only time will tell.Smile [:)]

Jav

Copyright (c) Marimer LLC