Click here to Skip to main content
15,891,700 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
(Looking for thoughts...)

So - when you create a WCF service with a [ServiceContract()] and [DataContract()] you can't use open generics - which I do understand why.

However - anyone have any ideas about how I could specify a small set of types and have the generics turned into concrete implementations in the service ? (Probably looking at codegen??)

i.e. if I have a function like
VB
Add(Of TParam)(Byval lhs As TParam, ByVal rhs As TParam) As TParam 
I'd like it to generate:-

VB
AddInteger(Byval lhs As Integer, Byval rhs As Integer) As Integer

AddDouble(Byval lhs As Double, Byval rhs As Double) As Double


(etc.)
Posted
Updated 26-Mar-14 7:13am
v3
Comments
Matt T Heffron 26-Mar-14 12:40pm    
Did you mean WCF?
ServiceContract and DataContract have nothing to do with WPF
Duncan Edwards Jones 26-Mar-14 13:09pm    
yeah - WCF it is... corrected.
Matt T Heffron 26-Mar-14 13:13pm    
I fixed the "tag" also...

This is not related to WCF either. You just need to learn how generics work in general and the syntax for their declaration and instantiation, as well as the type inference. Please see:
http://msdn.microsoft.com/en-us/library/ms235246.aspx[^].

—SA
 
Share this answer
 
As it happens I have just explicitly declared the interfaces for the specific types I want to handle and passed them on to the generic code the other side of the WCF service contract e.g.

VB
''' <summary>
 ''' Get the active clients on the system
 ''' </summary>
 ''' <param name="query">
 ''' The query specification for getting the active clients
 ''' </param>
 ''' <returns>
 ''' A set of client records in JSON form
 ''' </returns>
 <operationcontract()>
 <webget(responseformat:>
 Function GetActiveClients(ByVal query As GetActiveClientsQueryDefinition) As IReadOnlyList(Of ClientSummary)


and the implementation of this is:-
VB
Public Function GetActiveClients(query As GetActiveClientsQueryDefinition) As IReadOnlyList(Of ClientSummary) Implements IQueryHandlerService.GetActiveClients
    If (m_queryHandler IsNot Nothing) Then
        Return m_queryHandler.Handle(query)
    Else
        'Return an empty list
        Return New List(Of ClientSummary)()
    End If
End Function


where m_queryHandler.Handle is the generic function :-

VB
''' <summary>
''' Handle the given query definition and return the results
''' </summary>
''' <typeparam name="TResult">
''' The type of the result
''' </typeparam>
''' <param name="query">
''' The query definition to handle
''' </param>
Public Function Handle(Of TResult)(query As Query.IQueryDefinition(Of TResult)) As TResult

    Dim handler = GetHandler(Of TResult)(query)

    If (handler IsNot Nothing) Then
        Return handler.Handle(query)
    Else
        ' Could not find and appropriate handler
        Return Nothing
    End If

End Function
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900