views:

276

answers:

1

I'm switching code generators for my business objects. I was using SQL Metal, but in moving to the T4 toolbox's generator, serialization seems to have stopped working, and it looks like the two are doing pretty much the same thing.

This is the property generated by SQL Metal (which works):

[Association(Name="FK_FamilyConfiguration_Family", Storage="_FamilyConfigurations", ThisKey="FamilyID", OtherKey="FamilyID", DeleteRule="CASCADE")]
[DataMember(Order=4, EmitDefaultValue=false)]
public EntitySet<FamilyConfiguration> FamilyConfigurations
{
    get
    {
        if ((this.serializing 
                    && (this._FamilyConfigurations.HasLoadedOrAssignedValues == false)))
        {
            return null;
        }
        return this._FamilyConfigurations;
    }
    set
    {
        this._FamilyConfigurations.Assign(value);
    }
}

and this is the property generated by the T4 toolbox (which does not work):

[DataMember(Order = 4, EmitDefaultValue = false)]
[Association(Name = "Family_FamilyConfiguration", Storage = "familyConfigurations", ThisKey = "FamilyID", OtherKey = "FamilyID")]
public EntitySet<FamilyConfiguration> FamilyConfigurations
{
    get 
    {
        if (this.serializing && !this.familyConfigurations.HasLoadedOrAssignedValues)
        {
            return null;
        }

        return this.familyConfigurations; 
    }

    set 
    { 
        this.familyConfigurations.Assign(value); 
    }
}

As far as I can tell, they seem to generate pretty much the same thing. However, with the latter code, the object and all of its references are correctly populated (FamilyConfigurations contains a non-null entry) on the server side of a WCF call, but by the time it gets to the client, FamilyConfigurations is null.

I'm assuming I have a serialization problem of some sort, but I don't see what the difference between the two generated properties is. Perhaps there's something else that needs to be done? The generated class of which FamilyConfigurations is a member has a DataContract tag under both generators.

Update: FamilyConfigurations is null, it is not a collection containing null, as previously state.

+1  A: 

Has FamilyConfiguration changed? I have seen serialization break because of a changed parent-child relationship. Specifically, the child can not have an Association to its parent. That would be my first guess without being able to see the classes themselves.

EDIT: You can write a small console app that serializes and deserializes your objects using the DataContractSerializer explicitly to find out if serialization is the problem.

Brett Bim
Well, in both instances, FmailyConfiguration has an association to Family, and they're pretty similar. Here's the SQL Metal FamilyConfiguration class: http://tinyurl.com/y8msxwv and here's the T4 class: http://tinyurl.com/y8p5kzd. I can post Family, too, if that'll help.
Mike Pateras
Here's the family code, as well. SQL Metal: http://tinyurl.com/y9kh4ta T4: http://tinyurl.com/yat2kyr
Mike Pateras
Do other properties on Family serialize correctly? And is the collection null or is it a collection of N null FamilyConfigurations?
Brett Bim
You might try to write a small Console app that loads up a Family and then try to Serialize it using the DataContractSerializer explicitly. This might give you some insight into where the serialization might be failing.
Brett Bim
Ah, that's interesting. The collection itself is null. I had incorrectly said that it was a collection containing a null entry. Good catch. Does that suggest this isn't a serialization problem? The other properties are null as well.
Mike Pateras
Using a DataContractSerializer I successfully output Xml that has all of the properties off of family, FamilyConfiguration included, and all of their data. So I guess this isn't a serialization problem. I'll open a new question on the T4 toolbox boards. Your comment provided me with the answer. If you re-write it as an answer, I'll mark it as the correct answer. Or would it be appropriate to mark your first response, since the comment is a child of it?
Mike Pateras
I think it may actually be a serialization problem on the client side. Did you try to read the XML back in to objects using the DataContractSerializer? I grabbed your objects and was able to deserialize the SQL Metal objects but not the T4 objects. I got an Object Null Reference exception when it tried to read the set of FamilyConfiguration.
Brett Bim
Well, I can rebuild the object on the server side using the DataContractSerializer (and the FamilyConfigurations and stuff aren't null). However, in trying to do it on the clientside, I kept getting a unicode error. The weird thing is I get this on the server side if I try and deserialize from a file, whereas on the server it works if I use the same stringbuilder that backs the stream for the original deserialization. I'm not entirely sure what that means for the situation, though. Suggestions?
Mike Pateras