简体   繁体   中英

Complex types with Silverlight 4 WCF RIA Domain Service

After extensive Googleing I'm still bashing my head against a wall on this.

I'm trying to get a domain service to return a list of objects to the client side. My objects are

[DataContract]
public class MyComplexType {
    [Key]
    [DataMember]
    public int Id { get; set; }
    [DataMember]
    public string SomeString { get; set; }
    [DataMember]
    public IEnumerable<MySubType> SubTypes { get; set; }
}

[DataContract]
public class MySubType {
    [Key]
    [DataMember]
    public int Id { get; set; }
    [DataMember]
    public string AnotherString { get; set; }
}

The domain service has the following

[Query]
[Invoke]
public IQueryable<MyComplexType> GetMyComplexTypes() {
    /* return stuff */
}

But on the client side the MyComplexType object doesn't have the SubTypes property, I tried adding the property:

[DataMember]
public int ComplexTypeId { get; set; }

...to MySubType, and the attribute:

[Association("SubType_FK", "Id", "ComplexTypeId")]

...to the SubTypes property in MyComplexType.

But still it doesn't push the SubTypes property to the client side. I have another object (same principle, but with real code) where the SubTypes equivalent is on the client side POCO but it is always empty (regardless of what is returned from the server).

I feel I must be missing something here, this surely shouldn't be so difficult? Any help or explanation or directing to a useful resource would be appreciated.

UPDATE : I've found if I add add the following query to the domain service:

    public IQueryable<MySubType> GetMySubTypes() {
        throw new NotImplementedException();
    }

I can now access the MyComplexType.SubType property from the client side, however when I test it with the following code ...

_Context.Load(_Context.GetMyComplexTypesQuery(),
            r => {
                myComplexTypes = r.Entities;
                var noCts = myComplexTypes.Count();
                foreach (var ct in myComplexTypes) {
                    var noSts = ct.SubTypes.Count();
                }
            }, null);

And I debug it, there are the two complextypes I created (so noCts is 2), but for each iteration noSts is 0, despite me adding SubTypes on the server-side.

Any ideas?

You may only need to add the [ComplexType] attribute:

using System.ComponentModel.DataAnnotations;

[ComplexType]
public class MyComplexType {
    public int Id { get; set; }
    public string SomeString { get; set; }
    public IEnumerable<MySubType> SubTypes { get; set; }
}

[ComplexType]
public class MySubType {
    public int Id { get; set; }
    public string AnotherString { get; set; }
}

The [DataContract] and [DataMember] attributes are optional. They are automatically inferred by WCF RIA.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM