How to return value from UnitOfWork.AddCallBack

Posts   
 
    
Posts: 4
Joined: 17-Mar-2012
# Posted on: 17-Mar-2012 13:50:15   

Hello dear all.

I have a problem with AddCallBack function. The thing is that I need to execute the stored procedure and return a value from it. But it should be when the UnitOfWork commit everything.

So, I have a procedure:


public static int SysCheckCountryRegionCity(System.String inZip, System.String inCity, System.String inRegion, System.String inCountry, ref System.Int64 outCtyId, IDataAccessCore dataAccessProvider)
        {
            using(StoredProcedureCall call = CreateSysCheckCountryRegionCityCall(dataAccessProvider, inZip, inCity, inRegion, inCountry, outCtyId))
            {
                int toReturn = call.Call();
                outCtyId = call.GetParameterValue<System.Int64>(4);
                return toReturn;
            }
        }

So, InternalSaveBirthCity didn't call back. What did I do wrong? How to solve it?

Thank you. This procedure returns some ID. How to obtain it from UnitOfWork.AddCallBack? I tried to make a fake procedure and put it as callback delegate but it's doesn't work.


 private void SaveBirthCity(PPW.Common.UnitOfWork aUnitOfWork)
        {
            if ((m_BirthCity != null) || (m_BirthZip != null) || (m_BirthRegion != null) || (m_BirthCountry != null))
            {
                aUnitOfWork.AddCallBack(new ActionProcedures.SysCheckCountryRegionCityCallBack(InternalSaveBirthCity),
                    UnitOfWorkCallBackScheduleSlot.PreEntityUpdate, m_BirthZip, m_BirthCity, m_BirthRegion, m_BirthCountry);        
            }
        }

        private int InternalSaveBirthCity(string aZip, string aCity, string aRegion, string aCountry, ref long aOutCtyId, IDataAccessCore aDataAccessProvider)
        {
            var temp = ActionProcedures.SysCheckCountryRegionCity(aZip, aCity, aRegion, aCountry, ref aOutCtyId, aDataAccessProvider);
            BirthCityId = aOutCtyId;

            ResetBirthZipCity();

            return temp;
        }

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 19-Mar-2012 07:11:05   

Your callBack definition doesn't match with the signature of the delegate. You can create your own version of the delegate and the method:

public delegate void MySysCheckCountryRegionCityCallBack(string aZip, string aCity, string aRegion, string aCountry, IDataAccessCore dataAccessProvider);

private void SaveBirthCity(PPW.Common.UnitOfWork aUnitOfWork)
{
    if ((m_BirthCity != null) || (m_BirthZip != null) || (m_BirthRegion != null) || (m_BirthCountry != null))
    {
        // we pass true so the used DataAccessAdapter instance of the uow would be used
        aUnitOfWork.AddCallBack(new MySysCheckCountryRegionCityCallBack(InternalSaveBirthCity),
            UnitOfWorkCallBackScheduleSlot.PreEntityUpdate, true, m_BirthZip, m_BirthCity, m_BirthRegion, m_BirthCountry);      
    }
}

private void InternalSaveBirthCity(string aZip, string aCity, string aRegion, string aCountry, IDataAccessCore aDataAccessProvider)
{
    int aOutCtyId = 0;
    var temp = ActionProcedures.SysCheckCountryRegionCity(aZip, aCity, aRegion, aCountry, ref aOutCtyId, aDataAccessProvider);
    BirthCityId = aOutCtyId;

    ResetBirthZipCity();
}
David Elizondo | LLBLGen Support Team
Posts: 4
Joined: 17-Mar-2012
# Posted on: 19-Mar-2012 11:36:41   

Thank you for your answer!

Our delegate is:


public delegate int SysCheckCountryRegionCityCallBack(System.String inZip, System.String inCity, System.String inRegion, System.String inCountry, ref System.Int64 outCtyId, IDataAccessCore dataAccessProvider);       

So, we have a

ref outCityId 

Also if callBack definition doesn't match with the signature of the delegate it will not compile.

Maybe something else wrong with parameters?

Thanks!

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 19-Mar-2012 14:59:08   

You don't pass that last parameter:

aUnitOfWork.AddCallBack(new ActionProcedures.SysCheckCountryRegionCityCallBack(InternalSaveBirthCity), UnitOfWorkCallBackScheduleSlot.PreEntityUpdate, m_BirthZip, m_BirthCity, m_BirthRegion, m_BirthCountry);

Posts: 4
Joined: 17-Mar-2012
# Posted on: 19-Mar-2012 15:43:14   

It's very weird. My code is:


long temp = 0;
string temp2 = "";

aUnitOfWork.AddCallBack(new ActionProcedures.SysCheckCountryRegionCityCallBack        (InternalSaveBirthCity),
                    UnitOfWorkCallBackScheduleSlot.PreEntityUpdate, m_BirthZip, m_BirthCity, m_BirthRegion, m_BirthCountry, temp, temp2);       
            

We used this sample in other places and it worked. Now it doesn't call.

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 19-Mar-2012 15:49:38   

First of all you need to pass a boolean after the UnitOfWorkCallBackScheduleSlot Enum, that's for passing the DataAccessAdapter object.

Then why do you pass 2 parameters (temp, temp2) although only one is left after the Country parameter.

Here is the signature of the method:

public virtual void AddCallBack( 
   Delegate delegateToCall,
   UnitOfWorkCallBackScheduleSlot schedulingSlot,
   bool passInAdapter,
   params object[] parameters
)
Posts: 256
Joined: 05-Jul-2010
# Posted on: 19-Mar-2012 16:00:53   

Hi

I know what is happening here. I'm working on a temporary unit of work and later on I'm migrating it to the final unit work. There is a whole reason why I'm doing it, it has to do with the "iterations" and dependent objects.

This works really great for adding and removing objects, with constructqueues, I can figuere out what I want. This doesn't work for the callbacks. I cannot iterate through the callbacks and "move" them to the final unit of work.

For now I can solve it by reflection, but it might be interesting to make more public accessors for these callbacks.

thanks

A

Posts: 4
Joined: 17-Mar-2012
# Posted on: 19-Mar-2012 16:19:21   

Now it's solved. Thank you!