Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataContractResolver / KnownType issue when custom class contains another custom class

I'm trying to output a list of objects to a json format using the DataContractJsonSerializer class however I keep running into the following error.

Type 'Castle.Proxies.JokeCategoryProxy' with data contract name 
'JokeCategoryProxy:http://schemas.datacontract.org/2004/07/Castle.Proxies' 
is not expected. Consider using a DataContractResolver or add any types not 
known statically to the list of known types - for example, by using the
KnownTypeAttribute attribute or by adding them to the list of known 
types passed to DataContractSerializer.

I know this has been answered before but it only seems to happen when in my object a have a property which is another custom object.

[DataContract]
[KnownType(typeof(ModelBase<int>))]
public class Joke : ModelBase<int>
{
    [DataMember]
    public virtual string JokeText { get; set; }

    [DataMember]
    public virtual JokeCategory JokeCategory { get; set; }
}

[DataContract]
[KnownType(typeof(ModelBase<int>))]
public class JokeCategory : ModelBase<int>
{
    [DataMember]
    public virtual string Name { get; set; }
}

As you can see the Joke model contains a Joke Category object, if I remove the Joke Category and simply have an int (JokeCategoryId) the error disappears, although a solution, not an ideal one as I would like to have the Category available without having to query again.

Below is the code I'm using to generate the json

    public static ContentResult JsonResponse<TReturnType>(this Controller controller, TReturnType data)
    {
        using (var oStream = new System.IO.MemoryStream())
        {
            new DataContractJsonSerializer(typeof(TReturnType)).WriteObject(oStream, data);

            return new ContentResult
            {
                ContentType = "application/json",
                Content = Encoding.UTF8.GetString(oStream.ToArray()),
                ContentEncoding = Encoding.UTF8
            };
        }
    }

What confuses me the most is that the error references Castle.Proxies.JokeCategoryProxy (where did this come from?!)

Any suggestions?

like image 631
JConstantine Avatar asked Jul 08 '11 09:07

JConstantine


1 Answers

nHibernate assumes that all your properties, unless specified otherwise, are lazy-loaded.
What that means is that, in your case, the JokeCategory isn't pulled from the DB whenever you pull your Joke object; instead, a 'proxy' is generated dynamically.
The first time you access that property nHibernate knows to pull it from the DB. (this is how nHib's lazy-loading works)

So basically what's going on here is that you expect your JokeCategory to be of type JokeCategory, but since it isn't really initialized- it's of type Proxy....

(this is just a brief explanation; google some more on nHib and how it works to find out more. you can also check out summer of nhibernate for a great introduction to this ORM)

And, to your question: you have a couple of options here:

  1. configure you Category property as non-lazy, which will force nHibernate to initialize it with the proper object type

  2. (much more preferable, in my opinion) do not serialize your Model entities; instead- build some sort of DTO that will hold whatever information your presentation layer needs.
    This way your presentation needn't be affected by changes to the domain model and vice versa.
    also, you can hold all the necessary information in your DTO, even if it's related to more than one Model entity.

For example:

  public class JokeDTO
    {
       public int JokeId;
       /*more Joke properties*/
       public int JokeCategoryId;
       public string JokeCategoryName;
       /*etc, etc..*/
    }
like image 132
J. Ed Avatar answered Sep 27 '22 15:09

J. Ed