WCF return DataContract composits

Posts   
 
    
zee
User
Posts: 35
Joined: 24-Jul-2008
# Posted on: 30-Jul-2008 00:11:54   

I hope the subject defined what i am trying to do.

I am trying to do a one call to the WCF to get a few configuration/drop down lists..

so.. in order to do that i have

<OperationContract()> _ Function GetConfigData() As MyCompositType

<DataContract()> _ Public Class MyCompositType

Private aEntity As myEntity

<DataMember()> _
Public Property aEntityCollection() As myEntity
    Get
        Return Me.aEntity 
    End Get
    Set(ByVal value As myEntity)
        Me.aEntity = value
    End Set
End Property

End Class

and then., for the implementation i have

Public Function GetConfigData() As MyCompositType Implements IService1.GetConfigData Dim TheCollection As New MyCompositType

    Dim adapter As New DataAccessAdapter()
    Dim allEntities As New EntityCollection(New myEntity())
    adapter.FetchEntityCollection(allEntities, Nothing)

    '******************line below will not work......
    TheCollection.aEntity  = CType(allEntities, myEntity)

End Function

So pretty much what i was trying to do was have GetConfigData return the class MyCompositType

The class MyCompositType would contain the collections.....

is this the right way to do so? like i said., im trying to avoid 4 - 5 calls to get 4-5 drop down lists... i want 1 call to return 1 class that has the collections.... i have done this before when i was returning stright xml datasets in a web services project by adding the dataset to the return xml., that is not what i am trying to do here., i would like class/collections/entities simple_smile ...

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 30-Jul-2008 11:26:27   

If you want to use DataContract then you should use DTOs and not custom objects/classess.

Otherwise you should use an interface to define your passed in/out objects and share this between the service and the client.

Please check the WCF example posted on our web site download page.

zee
User
Posts: 35
Joined: 24-Jul-2008
# Posted on: 30-Jul-2008 19:35:23   

i am looking at the WCF example,.

i see the use of IEntity2 and IEntityCollection2

for example /// <summary> /// Gets the customer entity with the id specified /// </summary> /// <param name="customerId">The customer id.</param> /// <returns>customer entity with the id specified.</returns> [OperationContract] IEntity2 GetCustomer(string customerId);

is there a reason why the return type isnt CustomersEntity since its returning the customer entity?

zee
User
Posts: 35
Joined: 24-Jul-2008
# Posted on: 30-Jul-2008 19:43:11   

i think the bulb over my head just blinked and turned on....

IEntity2,IEntityCollection2 = DTO ? customerEntity = ... entity?

so If you want to have a fixed DataContract instead, you shouldn't use Entity classes but you should send Data Transfer Objects (DTO)'

would mean that in my code... instead of trying to return the custom class., i should be returning it as a IEntity2?

zee
User
Posts: 35
Joined: 24-Jul-2008
# Posted on: 30-Jul-2008 20:22:22   

the implementation of a service contract Public Function GetMediaSelectionConfig() As IEntity2 Implements IService1.GetMediaSelectionConfig Dim TheCollection As New MediaSelectCollectionType

    Dim adapter As New DataAccessAdapter()
    Dim allDistributors As New EntityCollection(New DistributorEntityFactory())
    adapter.FetchEntityCollection(allDistributors, Nothing)

    TheCollection.DistributorCollection = allDistributors

    Return TheCollection

End Function

and the <DataContract()> _ Public Class MediaSelectCollectionType

Private Distributors As IEntityCollection2
Private stringValueField As String

<DataMember()> _
Public Property DistributorCollection() As IEntityCollection2
    Get
        Return Me.Distributors
    End Get
    Set(ByVal value As IEntityCollection2)
        Me.Distributors = value
    End Set
End Property

now Return TheCollection will throw an error because im returning my class when its expecting., ientity2

i am sure i need to send this back using serilization/dto...

i am not sure of the code on how to do so.. i guess the problem really is... if i am returning a DTO., it will lose the class properties?

meaning., i wont really be able to do something like the following on the client //psudocode

mycollection = myservice.get_one_time_call_for_collectionobjects() adistributorcollection = mycollection.distributors alicensingcollection = mycollection.licenses

and so on

maybe i have to implement something like this?

Batching WCF calls http://davybrion.com/blog/2008/06/batching-wcf-calls/

zee
User
Posts: 35
Joined: 24-Jul-2008
# Posted on: 30-Jul-2008 23:48:18   

here is a similar wcf return...

Public Function GetMediaPending() As IEntityCollection2 Implements IService1.GetMediaPending Dim adapter As New DataAccessAdapter() Dim invoices As New MediaPendingTypedView

    adapter.FetchTypedView(invoices.GetFieldsInfo, invoices)

    Return invoices

End Function

i think what i need to figure out and understand is simple_smile returning invoces... as a collection... is there a method that allows me to return it as a DTO ?

zee
User
Posts: 35
Joined: 24-Jul-2008
# Posted on: 31-Jul-2008 02:50:22   

maybe im learning... i had to change my view to a spx because i needed to pass in some parameters...

Public Function GetMediaPending(ByVal MediaTypeId As Integer) As DataSet Implements IService1.GetMediaPending

    Dim resultSet As DataTable = RetrievalProcedures.GetMediaPending(MediaTypeId)
    Dim AReturnDataset As New DataSet
    AReturnDataset.Tables.Add(resultSet)
    Return AReturnDataset


End Function

the one above,. works., but the return is a dataset.. is that the same as a DTO ? is there a difference between ientitycollection2 and a dataset return?

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 31-Jul-2008 09:39:48   

Using LLBLGen objects or their interfaces are not considered as DTOs. But rather Business Objects.

And the difference is that DTOs do not have any behaviour except for storage and retrieval of its own data.

Anyway if you exactly follow the steps of the WCF example you will get what you want.

Hints. Use interface in your methods declaration, and use the service known type attribute to declare the actual returned types.

e.g.

[ServiceKnownType(typeof(CustomerEntity))]