views:

39

answers:

2

I've just made myself up a problem and am now wondering how to solve it.

To begin with, I'm using some third-party components, including some calendar controls like schedule and timeline. They're used in the project classes more or less like that:

Friend Class TimeBasedDataView
    'some members
End Class

Friend Class ScheduleDataView
    Inherits TimeBasedDataView

    Public Schedule As Controls.Schedule.Schedule
    'and others
End Class

Friend Class TimeLineDataView
    Inherits TimeBasedDataView

    Public TimeLine As Controls.TimeLine.TimeLine
    'and others
End Class

(Hmm, code coloring fail, never mind...) Now, to allow managing the look of data being presented there are some mechanisms including so called Style Managers. A lot of code in them repeats, varying almost only with the control it maintains:

Friend Class TimeLineStyleManager
    Private m_TimeLine As TimeLineDataView

    Private Sub Whatever()
        m_TimeLine.TimeLine.SomeProperty = SomeValue
    End Sub
End Class

Friend Class ScheduleStyleManager
    Private m_Schedule As ScheduleDataView

    Private Sub Whatever()
        m_Schedule.Schedule.SomeProperty = SomeValue
    End Sub
End Class

I was wondering if I could create some base class for those managers, like

Friend Class TimeBasedCtrlStyleManagerBase(Of T As TimeBasedDataView)
    Private m_Control As T
    'and others
End Class

which would unify these two, but I've got lost when it came to maintaining two components that have nothing in common (except their properties' names, etc.). Type reflection maybe? I'll be grateful for any advice ;)

A: 

Looks like you've got a case where you're looking to introduce inheritance, where it's not needed - it would've been better if the third party controls all adhered to a common interface (and then generics may have saved the day), but as they're third party, I'd assume that you have minimal impact on future direction of it.

Rowland Shaw
A: 

You've done exactly the right kind of thing - inheritance and generics is perfect for this situation - but I would make the TimeBasedCtrlStyleManagerBase class MustInherit/abstract and then simply inherit down your two specific managers.

All common management code goes in the abstract base class and any specific code goes in the two specific managers.

Of course you'll need to change Private m_Control As T to Protected m_Control As T for this to work.

Friend Class TimeLineStyleManager
    Inherits TimeBasedCtrlStyleManagerBase(Of TimeLineDataView)
End Class

Friend Class ScheduleStyleManager
    Inherits TimeBasedCtrlStyleManagerBase(Of ScheduleDataView)
End Class
Enigmativity
Yep, I've already figured that out (MustInherit and Protected). My problem is how to make the base class to use the proper inner control (m_Control.TimeLine or m_Control.Schedule) or, for example, enums from the proper namespace in some cases (like Controls.Schedule.TriState).
brovar
I know it sounds messy, but that's the whole point for me, because one manager has, for example, loop: "For Each MyFormat As TimeLine.TimeLineFormatCondition [...]" and the other one: "For Each MyFormat As Schedule.ScheduleFormatCondition [...]" with some variables of such schedule/timeline types inside, but the body itself is identical.
brovar
@brovar - it's not that messy - simply anything that is common is in the base generic class and all of the specific stuff that you mention goes in the derived manager classes. That's just how it works. If you have any further issues maybe post a new question (with a link in a comment here) or add more detail to this question?
Enigmativity