How to structure my GUI agnostic project?

I have a project which loads from database a XML file which defines a form for some user. XML is transformed into a collection of objects whose classes derive from single parent. Something like

Control -> EditControl -> TextBox

Control -> ContainterControl -> Panel

Those classes are responsible for creation of GUI controls for three different enviroments: WinForms, DevExpress XtraReports and WebForms. All three frameworks share mostly the same control tree and have a common single parent (Windows.Forms.Control, XrControl and WebControl).

So, how to do it?

Solution a) Control class has abstract methods

Control CreateWinControl();
XrControl CreateXtraControl();
WebControl CreateWebControl();

This could work but the project has to reference all three frameworks and the classes are going to be fat with methods which would support all three implementations.

Solution b) Each framework implementation is done in separate projects and have the exact class tree like the Core project. All three implementations are connected using a interface to the Core class.

This seems clean but I’m having a hard time wrapping my head around it. Does anyone have a simpler solution or a suggestion how should I approach this task?

If you have a tree of objects (and I do mean tree of objects, not tree of classes) of various types that you want to process in different ways, then the common approach is to use the visitor pattern.

How would that look in your case? Your common project would contain an abstract class ControlTreeVisitor, which would look something like this (simplified by assuming there is no ContainterControl or EditControl):

public abstract class ControlTreeVisitor
{
    protected virtual void VisitControl(Control control)
    {
        // dispatch to the appropriate Visit* method
    }

    protected virtual void VisitPanel(Panel panel)
    {
        foreach (var childControl in panel.Children)
        {
            VisitControl(childControl);
        }
    }

    protected virtual void VisitTextBox(TextBox textBox)
    {
        // do nothing
    }
}

Each GUI-specific project would then contain a concrete class that would inherit from this visitor class and would override all the Visit* methods to create the appropriate control tree for the given GUI library.

There are some possible alternatives in the design of the visitor base class. For example, one variation would be to make it generic, where the type parameter would represent the base class in the specific GUI library (e.g. class WebControlVisitor : ControlTreeVisitor<WebControl>). All Visit* methods would then return this type. This would make creating a tree of WebControls simpler.

1

Solution (b) is the way to go. Define interfaces for the various controls:

public interface EditControl
{
  //...
}

public interface ContainerControl
{
  //...
}

and an interface for a factory:

public interface ControlFactory
{
  EditControl CreateEditControl(...);
  ContainerControl CreateContainerControl(...);
  //...
}

Each of the packages will then implement this set of interfaces; in particular, each package knows how to implement the ControlFactory interface by creating its own flavour of the controls.

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật