PacktPublishing / Hands-On-Domain-Driven-Design-with-.NET-Core

Hands-On Domain-Driven Design with .NET Core, published by Packt
MIT License
622 stars 204 forks source link

Marketplace.Domain.ClassifiedAdTitle. A class should either have a default constructor.... #6

Closed paulspencerwilliams closed 4 years ago

paulspencerwilliams commented 4 years ago

When testing the developed application, at the end of Chapter 6, whilst running the server in between API posts, all seems to work fine, most likely due to Raven's document cache. However, if I restart the app in between API posts, I'm presuming the lack of Raven client cache means it's client has to pull documents from the database, and deserialisation fails with:

      Connection id "0HLQQ0FOTFTT7", Request id "0HLQQ0FOTFTT7:00000001": An unhandled exception was thrown by the application.
System.InvalidOperationException: Could not convert document ClassifiedAd/a287c0ad-898f-4160-9fa1-e005c778759a to entity of type Marketplace.Domain.ClassifiedAd
 ---> Newtonsoft.Json.JsonSerializationException: Unable to find a constructor to use for type Marketplace.Domain.ClassifiedAdTitle. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'Title.Value'.

I can see that persistence is generally working however, when viewing the document store, amount 321 has been updated several times...

{
    "Id": {
        "Value": "a287c0ad-898f-4160-9fa1-e005c778759a"
    },
    "State": "PendingReview",
    "OwnerId": {
        "Value": "a287c0ad-898f-4160-9fa1-e005c778759a"
    },
    "Title": {
        "Value": "the title"
    },
    "Text": {
        "Value": "the text"
    },
    "Price": {
        "Amount": 321,
        "Currency": {
            "CurrencyCode": "EUR",
            "InUse": false,
            "DecimalPlaces": 0
        }
    },
    "ApprovedBy": null,
    "@metadata": {
        "@collection": "ClassifiedAds",
        "Raven-Clr-Type": "Marketplace.Domain.ClassifiedAd, Marketplace.Domain"
    }
}

I'd imagine this doesn't really matter based on the context of the book, but has certainly brought doubt in my mind as to my following of the code.

alexeyzimarev commented 4 years ago

The proper RavenDb implementation is in Chapter 8. Sorry, it caused some confusion. If you look at the ClassifiedAdTitle in that chapter, you'd find the default constructor that I had to add exactly to solve the issue you reported:

https://github.com/PacktPublishing/Hands-On-Domain-Driven-Design-with-.NET-Core/blob/master/Chapter08/ravendb/Marketplace.Domain/ClassifiedAdTitle.cs#L44