Matthias
Matthias

Reputation: 16209

Developing independently from UI framework

I'm trying to organize my code flexible. So my application makes heavy use of interfaces. In my controller class I want to start the webcam preview on a certain control in the view. Implementations of ClientView and WebcamPreview rely on each other. So there is one specific WebcamPreview for WinForms and one for WPF. I ended with 2 possible ways:

Option 1:

Pros: Generics

Cons: Generic declarations bubble up to the top controller class, and I need to declare at least 3 generics in it. (Maybe I'm doing something wrong?)

interface IClientView<TSelf>
{
    TSelf SelfControl { get; }
}

interface IWebcamPreview<TSelf>
{
    void Start(TSelf selfControl);
}

Option 2:

Pros: Avoids to much generics

Cons: Directives; will compile different assemblies for WPF and WinForms

interface IClientView
{
#if WPF
    ControlBase SelfControl { get; }
#else // WinForms
    PictureBox SelfControl { get; }
#endif
}

interface IWebcamPreview
{
    // analog
}

So how do I organize my code in order to support different UI frameworks?

Upvotes: 1

Views: 128

Answers (2)

Kevin Le - Khnle
Kevin Le - Khnle

Reputation: 10887

Option 1 is the way to go. Option 2 will get very busy with directives very quickly. If one day, you find yourself, in addition to WPF and Winform, have to do for Windows Phone, and Web app, you'll have to touch lots of code to add new #elif directives. Code will not be too pleasant to look at at that point. With option 1 and some good organizations i.e., the WPF goes into 1 assembly, the Winform another, future addition such as for Windows Phone may not require even a recompile of existing code. If it does, maybe very minimal.

Upvotes: 1

Wiktor Zychla
Wiktor Zychla

Reputation: 48314

Option 3:

abstract class AbstractControl { }

class WinFormsControl : AbstractControl { }

class WpfControl : AbstractControl { }

interface IClientView
{
   AbstractControl SelfControl { get; }
}

Upvotes: 2

Related Questions