Making a BeforeDelete in a repository

For general questions and discussions specific to the AbleCommerce GOLD ASP.Net shopping cart software.
Post Reply
User avatar
AbleMods
Master Yoda
Master Yoda
Posts: 5170
Joined: Wed Sep 26, 2007 5:47 am
Location: Fort Myers, Florida USA

Making a BeforeDelete in a repository

Post by AbleMods » Mon Feb 01, 2016 11:46 am

Background:
When a stored credit card profile is deleted by the shopper, any payment record that references the deleted payment profile is not updated. The payment.GatewayPaymentProfileId value is still populated even though that entity ID no longer exists in ac_GatewayPaymentProfiles.

Since there's no foreign key constraint, my only other option seems to be creating a BeforeDelete override in the repository class.

This creates all sorts of problems later in life, especially when something simple like 'if (payment.PaymentProfile != null)' throws an nHibernate exception.

Solution:
So I added this method to the GatewayPaymentProfileRepository.cs file. See below.

My Question:
Is this the recommended way to do what is needed? I can't keep having ac_Payment records pointing to payment profiles that no longer exist. And I don't want to litter up my code with a bunch of try-catch.

Thoughts?

Code: Select all

        // BEGIN MOD: AbleMods.com
        // DATE:  02/01/2016
         
        /// <inheritdoc />
        public override void BeforeDelete(object entity)
        {
            // Cast the abstract object into a typed instance of the class 
            GatewayPaymentProfile profile = (GatewayPaymentProfile) entity;

            // Remove reference for any payments associates with this payment profile
            NHibernateHelper.CreateSQLQuery("UPDATE ac_Payments SET GatewayPaymentProfileId = NULL WHERE GatewayPaymentProfileId = :targetGatewayPaymentProfileId ")
                .SetInt32("targetGatewayPaymentProfileId ", profile.Id)
                .ExecuteUpdate();
        }
        // END MOD: AbleMods.com
Joe Payne
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com

User avatar
Katie
AbleCommerce Admin
AbleCommerce Admin
Posts: 2651
Joined: Tue Dec 02, 2003 1:54 am
Contact:

Re: Making a BeforeDelete in a repository

Post by Katie » Mon Feb 01, 2016 1:25 pm

I'm pretty sure we have this fixed now because it was assigned to me for final QA. I will check with Mazhar to make sure we are talking about the same issue though and see if he has any feedback on your proposed solution.

Thanks
Katie
Thank you for choosing AbleCommerce!

http://help.ablecommerce.com - product support
http://wiki.ablecommerce.com - developer support

User avatar
AbleMods
Master Yoda
Master Yoda
Posts: 5170
Joined: Wed Sep 26, 2007 5:47 am
Location: Fort Myers, Florida USA

Re: Making a BeforeDelete in a repository

Post by AbleMods » Mon Feb 01, 2016 4:06 pm

Hey Thanks Katie !

I definitely would like to know so my solution doesn't conflict with yours. That would be bad :shock:
Joe Payne
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: Making a BeforeDelete in a repository

Post by mazhar » Mon Feb 01, 2016 10:32 pm

Our fix is a two step fix. First we put some code against AfterDelete to update related payment records and set the profile id to null when profile is deleted. Secondly in maintenance service we have a piece of code to fix any existing records having invalid profile ids. The reason we choose to do it by code was due to nature of relationships between subscription, payments. Introducing database constraint for this may introduce multiple cascade paths which may lead to errors.

User avatar
AbleMods
Master Yoda
Master Yoda
Posts: 5170
Joined: Wed Sep 26, 2007 5:47 am
Location: Fort Myers, Florida USA

Re: Making a BeforeDelete in a repository

Post by AbleMods » Tue Feb 02, 2016 12:05 am

Understood. Thanks Mahzar!
Joe Payne
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com

Post Reply