views:

70

answers:

2

I have a .net website written in C# and will make functionalities that other developers can use. So I will make some default implementation and a developer can overwrite some methods

Example: i have a class ShoppingCart and a class Product the class product haves a method getProductPrice the shoppingcart will call the method getProductPrice for calculating the total price of cart

The ShoppingCart and Product are in the same project and i will give the developers the .dll so they can't change the source code so we can update the assembly later

So they need to make a other project and extend the product class and overwrite the method getProductPrice so they can implement there own logic The problem is that the shoppingcart will not call the extended method but the original

If we make already a extended project for the developers and the shoppingcart will call the extended method then we have a circular reference because the extended product needs a reference to product and the shopping cart to the extended product

Partial classes also don't works because we only can use partials within the same assembly

Does anyone have a suggestion ?

Thanks in advance.

A: 

Declare your method as a virtual method. That allows you to override it in an inheriting class so that any calls to it will always be routed to the overriding implementation. More about virtual methods.

Also, GetProductPrice sounds like something that might be better to implement as a property instead of a method.

Matti Virkkunen
+1  A: 

You should mark all your Product methods as virtual:

public virtual decimal GetProductPrice() { };

Then the developers who will take over later can inherit from the product and override the method:

public class OverriddenProduct : Product
{
   public override decimal GetProductPrice() { }
}

Because OverridenProduct (in my example above) inherits from Product, it can still be added into the Product collection within your ShoppingCart class.

Just symantics I guess, but the ShoppingCart class's method should probably be GetTotalPrice() not GetProductPrice() as I'm assuming you're getting the total cost of the products in the cart.

GenericTypeTea
Trying to implement it, changing some logic and classes but i think this is a solution
Marco