Implementing the domain model

Implementing the Movie domain model using MongoDB annotations will look like the following:

@Document(collection = "movies")
data class Movie(@Id val id : Int,
val title : String,
val year : Int,
val genre : MovieGenre,
val ratings : MutableList<MovieRating>,
val cast : List<Actor>)

The preceding model is written in Kotlin. There are some interesting things about the preceding code:

  • The data keyword: The data keyword in Kotlin can be used to mark classes whose sole purpose is to hold and transfer data. Classes with this keyword will get the equals(), hashCode(), toString(), and copy() functions auto-generated by the compiler.
  • The val keyword: The val keyword is used to define immutable properties right after the class name, which will generate the proper accessors and modifiers accordingly. 

Furthermore, the @Document annotation is used to mark the Movie class as a document in MongoDB and the @Id annotation is used to mark the id property as the identifier of the Movie class. 

Implementing the MovieGenre domain model will look like the following:

data class MovieGenre(val name : String,
val description : String)

Implementing the MovieRating domain model will look like the following:

data class MovieRating(val comment : String,
val rating : Int,
val date : Date)

Implementing the Actor domain model will look like the following:

data class Actor(val name : String,
val inAs : String,
val noOfAwards : Int)

Implementing the User domain model will look like the following:

@Document(collection = "users")
data class User(@Id val id : Int,
val username : String,
val password : String,
val role : String,
val description: String)

All the models will have all argument constructors, which can be used to create instances out of them. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.148.107.254