Programming advice (c# and Silverlight/wpf)

So I’m dabbling in Silverlight and I’m working on a little project. It’s a character generator for a game. Each character has a set of attributes and talents and skills that define him.

So my question comes in on what would be best practice: Since most if not all of these elements will have a visual representation I’m thinking of inheriting their classes from custom wpf/Silverlight controls. So for example, firebreathing is class that represents the firebreathing talent in the game. It inherits from a fire class, which in turn inherits from an abstract “Talent” class, which finally inherits from my custom control class that defines how the firebreathing icon actually looks like

This seems logical to me, and yet it feels wrong. Like I shouldn’t be tying the visual elements of the program so closely into my engine. If I ever wanted to reuse my API for another purpose, I wouldn’t be able to since it inherits directly from a custom Silverlight control, right?

Should I instead pass instances of my custom controls to the API?

I agree that your inheritance is inappropriate. I recommend using composition. A ‘Talent’ is not a ‘Control’ but a ‘Talent’ might have a control, which it can provide later on.

ETA: IIRC, having abstract control classes plays hell with design-time rendering.

Agreed, you should probably use some kind of composition. I don’t know much about silverlight/C#, but you can probably implement an Interface for providing controls you might want to implement that at some level in the hierarchy that makes sense (probably close to the top to the hierarchy). If silverlight has some standard interface to do that, you might want to use that. (And put the actual implementation of the control in some other class).

An additional advantage of implementing interfaces is that you can use them for otherwise totally unrelated classes, so if you’ve got something that isn’t a Talent, it can still implement the same interface if it needs a control.

Interface! That sounds more elegant. Thank you!