I've looked at many posts here and I'm not entirely clear on the answer to this question.
I want to be able to implement an interface with composition and have all the methods be called on the component:
- Create an interface for the Component (i.e. Car).
- Create a class, implementing the Component (i.e. SimpleCar)
- Create another class which implements the Component interface (i.e. create a DeluxeCar which implements the methods by calling them on a SimpleCar member) by calling them on a Component member.
Now the caveat, the important part is I want to be able to do part 3 without writing all the wrapper methods to make DeluxeCar call the methods on SimpleCar. Preferable only needing to add a C# decorator or some C# template magic.
Two solutions I've seen: 1. ExtensionMethods - I believe you can implement a CarComposite interface and place extension methods on it (Similar to what's done here in C. Lawrence Wenham's answer: Multiple Inheritance in C#). However, I don't think that you can make the Composite interface also implement the Component interface. At least I haven't been able to get a working example. Perhaps I am wrong though. 2. Aspect Oriented Programming. Use PostSharp or another AOP framework to do step 3. I haven't been able to find example code on how to do this. Further I would really like to avoid PostSharp as I don't like the license and want a completely free solution. I think PostSharp states you can't do this with their free version anyways (See AspectInheritance http://www.postsharp.net/purchase). A working example for Spring.Net would help a lot.
Can someone please provide an example of #3 please in using standard C# libraries or Spring.net?
Update
h.alex's suggestion is interesting but still doesn't do what I need. Your use of the abstract class is a bit limiting. Imagine having a Boat interface in addition to the Car interface. Then you want to create an AmphibiousVehicle which implements both Boat and Car. In C# there is no multiple inheritance so you can't inherit both the AbstractCar and AbstractBoat class. You are forced to add even more boilerplate and create an AbstractAmphibiousVechicle abstract class as well. This might be fine if you don't have too many different classes. But sometimes you want to create many different classes and mix and match these behaviours. This is pretty common in video games. You might have a ton of different aspects that you want to be able to do with a game object (Push it, Throw it, Collect it, etc.). You will be forced to make many different abstract classes this way, each having a lot of boilerplate.
I want something like this that PostSharp has, but I'd really like to know if there is a framework with a less restrictive license which can do the same. Is this something that can be easily done in Spring.net for example? http://doc.postsharp.net/postsharp-3.0/##PostSharp-3.0.chm/html/T_PostSharp_Aspects_CompositionAspect.htm