AbleCommerce Forums
»
AbleCommerce
»
General Questions
»
Registering additional discount calculator
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
|
|
|
|
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
|
1 user thanked mazhar for this useful post.
|
|
|
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.
|
|
|
|
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.
|
|
|
|
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.
|
|
|
|
AbleCommerce Forums
»
AbleCommerce
»
General Questions
»
Registering additional discount calculator
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.
Important Information:
The AbleCommerce Forums uses cookies. By continuing to browse this site, you are agreeing to our use of cookies.
More Details
Close