Overloading DataPortal_Create

Overloading DataPortal_Create

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


mwilkes posted on Tuesday, July 11, 2006

Having problems overloading the DataPortal_Create(ByVal criteria As Object) method from csla.core.BusinessBase.  In my Employee class, which inherits from BusinessBase, I have the following method declaration:

    <RunLocal()> _
    Private Overloads Sub DataPortal_Create(ByVal criteria As Criteria)
        Me.FirstName = "Joe"
    End Sub

I also have a windows ui that references the Employee class and tries creating a new employee by using the Employee's factory method:

    Public Shared Function NewEmployee() As Employee
        Return DataPortal.Create(Of Employee)()
    End Function

However, as I am stepping through the ui, the method that is actually getting called is in csla.core.businessbase which simply throws an exception.  If I change the method declaration in my Employee class to:

    <RunLocal()> _
    Private Overloads Sub DataPortal_Create(ByVal criteria As Object)
        Me.FirstName = "Joe"
    End Sub

everything runs fine.  I also should mention that inside my Employee class is the Private Criteria class.

There has to be a simple answer out there.  I know I'm missing something.

RockfordLhotka replied on Tuesday, July 11, 2006

This is (to some degree) discussed in Chapter 4, where the base class implements Overridable DataPortal_XYZ methods to "simplify" the creation of business objects.

It appears that the end result is that those virtual methods have made things more complex, and I should have probably dropped them... Too late now - as I shudder to think how many people's code would break at this point...

So the answer is this: if you are going to use "criteria As Object" you must override the method from the base class:

Protected Overrides Sub DataPortal_Create(ByVal criteria As Object)

And this is the method which will be called if you (in your factory) call DataPortal.Create() with no parameter, or with a Nothing/null parameter. It is also the fallback method that will be called if no strongly typed overload can be found when you do pass an actual criteria object.

mwilkes replied on Tuesday, July 11, 2006

Thanks for your help Rocky.  I kept referring to the text but eventually wound up keeping the Object type in the DataPortal_Create method.  However, I am able to strongly type the criteria variable in the DataPortal_Fetch method through the use of an overload.  I guess this has to do with the fact that my GetEmployee factory method is passing an actual criteria object to the DataPortal.

-Mike

antoan replied on Thursday, August 31, 2006

The problem occurs when the criteria is stongly typed not when passed as an object. ie:


private void DataPortal_Create(Criteria criteria) {...} fails.

Am I not able to implement this factory method with a custom criteria because MethodCaller picks up the BusinessBase implementation first as opposed to my overload?

ajj3085 replied on Thursday, August 31, 2006

The method signatuer you posted should be called as long as you're doing this..

return DataPortal.Create<BO>( new Criteria( /* params, if any */ ) );

Is that what the call looks like?

Copyright (c) Marimer LLC