I have been banging my head against a wall all day and am starting to think this isnt possible so you guys are my last hope!
I have a user control which is used to create messages and then allows the message to be emailed or sent via SMS depending on what aspx page the user control is on. I have a fair amount of logic in the user control and was find I was doing alot of this:
If type="email" Then
'Logic for email
Exit Sub
End If
If type="sms" Then
'Logic for sms
Exit Sub
End If
And alot of the logic was similar so there was alot of duplication. I ended up abstracting out the similar logic for email and SMS into an Interface:
Public Interface IContactMessager(Of T)
Sub Delete(ref As String)
Function GetAll() As List(Of T)
Function GetByRef(ref As String) As T
Function MessageExists(ref As String) As Boolean
ReadOnly Property RefField() As String
Sub Save(ref As String, message As String)
Sub SendMessage(contact As String, message As String)
Sub Update(ref As String, message As String)
End Interface
I now have a sms and email manager classes that inherit from this interface. This should allow me to do something similar to the below in the user control
Dim handler As IContactMessager(Of Email)=if(type="sms",SMSHandler,EmailHandler)
Then instead of ifs I can just do handler.logic
and it will run the correct logic.
Now heres my issue, and its due to the interface being generic. Ideally id like to do the following:
Public ReadOnly Property ContactMessagerHandler() As IContactMessager(Of T)
Get
Return If(MessageType.ToLower() = "email", New EmailContactMessager(), New SMSContactMessager())
End Get
End Property
However I cant have the (Of T)
because obviously it has no idea what type to make this. I need to somehow dynamically set this type but I have run out of ideas.
A few things i have toyed with:
- Trying to see if I can pass an the object from the calling aspx page
- Making the interface a basepage instead then making the calling aspx page inherit from the base page and then trying to get the type from the usercontrols parent, but to no success
- Attempting to make methods to set the type of the interface based on text passed through from the calling aspx page
None of these work. Does anyone else have any other ideas? Hopefully this makes sense, if not let me know if I can improve the question somehow