logo
Welcome to our new AbleCommerce forums. As a guest, you may view the information here. To post to this forum, you must have a registered account with us, either as a new user evaluating AbleCommerce or an existing user of the application. For all questions related to the older version of Gold and earlier, please go to AbleCommerce Gold forum. Please use your AbleCommerce username and password to Login. New Registrations are disabled.

Notification

Icon
Error

Options
Go to last post Go to first unread
Joe Payne2  
#1 Posted : Sunday, November 1, 2020 8:04:53 PM(UTC)
Joe Payne2

Rank: Advanced Member

Groups: HelpDesk, Developers
Joined: 11/9/2018(UTC)
Posts: 564

Thanks: 122 times
Was thanked: 26 time(s) in 25 post(s)
I'm building an elaborate tier-based discount system based on annual sales.

If I decorate a class with:

Code:
[RegisterFor(typeof(IDiscountCalculator))]


will that register it as a discount calculator IN ADDITION to the default discount calculator? Or does it replace it?

I see in basket.Recalculate() that you initiate discount calculations like this:

Code:

                // RECALCULATE DISCOUNTS
                IDiscountCalculator discountCalculator = AbleContext.Resolve<IDiscountCalculator>();
                discountCalculator.Calculate(basket);


But I don't see how that fires all discount calculator classes registered for the given IOC container. Or does that just happen internally to the whole process perhaps?

p.s. these code examples are from Gold R12 but concept should be same

Wanna join the discussion?! Login to your AbleCommerce Forums forum account. New Registrations are disabled.

mazhar  
#2 Posted : Monday, November 2, 2020 12:08:52 PM(UTC)
mazhar

Rank: Administration

Groups: Admin, Administrators, HelpDesk, System, Authorized User, Developers, Registered
Joined: 10/5/2018(UTC)
Posts: 175

Thanks: 8 times
Was thanked: 17 time(s) in 15 post(s)
When you decorate a discount calculator with RegisterFor attribute it will replace the default discount calculator. If you want to extend the discount calculator then you can inherit your discount calculator from default discount calculator and override its method. This way you can trigger the base.MethodName() within overridden method to do the default calculation and then add your bits afterwards.
Code:

    [RegisterFor(typeof(IDiscountCalculator))]
    public class MyDiscountCalculator : DiscountCalculator
    {
        public override decimal Calculate(Basket basket)
        {
            base.Calculate(basket);

           // YOUR CODE GOES HERE

        }
    }

Edited by user Monday, November 2, 2020 12:10:03 PM(UTC)  | Reason: Not specified

thanks 1 user thanked mazhar for this useful post.
Joe Payne2 on 11/2/2020(UTC)
Joe Payne2  
#3 Posted : Monday, November 2, 2020 12:38:13 PM(UTC)
Joe Payne2

Rank: Advanced Member

Groups: HelpDesk, Developers
Joined: 11/9/2018(UTC)
Posts: 564

Thanks: 122 times
Was thanked: 26 time(s) in 25 post(s)
Ah ok. Thank you that helps a great deal.

So what benefit is there to the Windsor Castle implementation of IOC? It doesn't seem like it's even necessary since you can do as you described above.

Or, is the benefit because of doing what you described above requires a recompile. But using an IOC implementation could replace and override simply by installing a new DLL and adding a reference in /App_Data/windsor.config using a 3rd party DLL separate from CommmerceBuilder.
Joe Payne2  
#4 Posted : Wednesday, November 11, 2020 7:24:55 AM(UTC)
Joe Payne2

Rank: Advanced Member

Groups: HelpDesk, Developers
Joined: 11/9/2018(UTC)
Posts: 564

Thanks: 122 times
Was thanked: 26 time(s) in 25 post(s)
I eventually figured this out.

You do still need to add an entry to the /app_data/window.config if you intend to override an existing class. Even if that override code is also contained in the same CommerceBuilder.dll. Otherwise your override is ignored and the base class always gets called first.

I found that to be odd since my custom discount calculator was also in namespace CommerceBuilder.Marketing. But when I walk the code with the debugger, the app skips my .Calculate() method and goes straight to the default discount calculator unless I have the entry in the windsor.config file. I expected my public override decimal Calculate() to take precedence but it didn't.


mazhar  
#5 Posted : Wednesday, November 11, 2020 8:16:22 AM(UTC)
mazhar

Rank: Administration

Groups: Admin, Administrators, HelpDesk, System, Authorized User, Developers, Registered
Joined: 10/5/2018(UTC)
Posts: 175

Thanks: 8 times
Was thanked: 17 time(s) in 15 post(s)
Quote:
I found that to be odd since my custom discount calculator was also in namespace CommerceBuilder.Marketing. But when I walk the code with the debugger, the app skips my .Calculate() method and goes straight to the default discount calculator unless I have the entry in the windsor.config file. I expected my public override decimal Calculate() to take precedence but it didn't.

I think the issue is that your custom calculator is present within same DLL. We register the component in IOC in this order, first we register from CommerceBuilder.dll, next we register from plugin DLLs and lastly we register from windsor.config file. If we found multiple implementations within in CommerceBuilder we register the first one found.

In your case application must be picking the default implementation first and registering it instead of your custom implementation. I hope this helps with the confusion, as long as you have only one implementation you won't see this issue.
Users browsing this topic
Guest (6)
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.