Overriding DataPortal_Fetch in my own Base Class

Overriding DataPortal_Fetch in my own Base Class

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


razorkai posted on Thursday, May 18, 2006

Hi guys.

I tried to implement an override in my own BusinessBase class defined as follows

public abstract class EQBusinessBase<T> : Csla.BusinessBase<T> where T : EQBusinessBase<T>

All my objects are based on this instead of BusinessBase, and this lets me customise/add features wiothout modifying Csla code.  Today I tried to do

protected override void DataPortal_Fetch(object criteria)
{
   do some stuff
}

But the code never gets hit.  Can someone tell me why?  Other stuff in the class works fine.

Thanks.

P.S. FWIW, I am trying to get a reference to the name of the DataPortal xyz method currently being executed within the DataPortal_OnDataPortalInvoke event of a Business Object, and figured I could store this in the ApplicationContext.GlobalContext for want of another way to do this.  The event tells you that a xyz method is being excuted, but not which one. This is what I was trying to in the Fetch overrride.  If someone could suggest a way to do this then that would be great too.

John.

DavidDilworth replied on Thursday, May 18, 2006

This may seem like an obvious question, but are you going through one of the standard factory methods and calling the DataPortal ?

For example:

public static AvailableRoom GetAvailableRoom(int roomId)

{

return CSLA.DataPortal.Fetch<AvailableRoom>(new CriteriaIntIdentity(typeof(AvailableRoom), roomId));

}

Have you stepped through this code and had a break point set in the DataPortal_Fetch?

razorkai replied on Thursday, May 18, 2006

Hi David

Yes I am calling a factory method and I have tried a breakpoint in DataPortal_Fetch of my BO class.  It hits that one OK but not the one in my base class.

John.

DavidDilworth replied on Thursday, May 18, 2006

So you've got an override for DataPortal_Fetch() in your BO class as well as in your abstract base class (ABC).  In that case the method in the ABC will never be called because it will always call the instance method version, which is the one for your BO.

In the DataPortal_Fetch() override in your BO class you need to explicitly call the method in the ABC.

Add something like this to your code:

base.DataPortal_Fetch(criteria)

razorkai replied on Thursday, May 18, 2006

The DataPortal_Fetch in the BOo class is not an override, just a method with that name.  The abstract base class has the overrride version.  But I think I understand.  Why is it that with some classes of BO you seem to need to do an overrride of Fetch whereas with others you don't?

DavidDilworth replied on Thursday, May 18, 2006

The DataPortal_Fetch in the BO class should be declared override.

Otherwise you're hiding the method in the base class.  This generates a compiler warning which can be suppressed by using the "new" modifier.  Check the MSDN for "new modifier keyword (C#)" in the C# Programmer's Reference.

Do you not get the compiler warning?

razorkai replied on Thursday, May 18, 2006

No I don't get that warning David.  I have been doing a bit of copy and paste from http://www.onelittlevictory.com/ and if you look at the EditableRoot data access section there it does not have the overrride for Fetch - although that does not mean it is correct!  It would make more sense to me if they all had to have override specified.

RockfordLhotka replied on Thursday, May 18, 2006

Ideally though, you wouldn't _override_, but instead you'd overload.
 
private void DataPortal_Fetch(Criteria criteria)
 
That way you can get a strongly typed parameter, rather than a parameter of type object that has to be casted by hand.
 
Rocky
 

DavidDilworth replied on Thursday, May 18, 2006

DOH! Of course Rocky's right and that explains what's going on!

You're not getting the compiler warning because the method in your BO is an overload not an override.

And that's also why it never calls the method in the ABC.

razorkai replied on Thursday, May 18, 2006

Ah!  Thanks again for the info guys.

RockfordLhotka replied on Thursday, May 18, 2006

The problem is that there's a quirk in the way reflection interacts with generics... It turns out that reflection won't find a method implemented in a base class if that base class is both abstract and generic (which is your case).

I've been working with Ricky Supit on solving this, and there's a prototype solution in cvs for C# at the moment (www.lhotka.net/cslacvs) so you can try that. I think I did the same thing for VB, but I don't recall for sure (the last couple weeks I've been flying so much I'm kind of blurry...).

In any case, you can expect the fix to be in version 2.0.1 (basically a bug fix release) within the next couple weeks.

razorkai replied on Thursday, May 18, 2006

Thanks for the info Rocky, I'll check it out.

Copyright (c) Marimer LLC