Search code examples
androidkotlinandroid-room

Should model data classes and Room entity data classes be seperate?


My application implements retrofit to return a set of data from a Rest Api. This data includes Objects and Arrays.

Api Structure

<OrderInfoResponse
  <OrderInfoResult>
    <deliveryAddresses>
      <OrderingDeliveryAddressStruct>
        <deliveryAddressNo>string</deliveryAddressNo>
        <deliveryAddressName>string</deliveryAddressName>
        <pointsOfService>
          <OrderingPointOfServiceStruct>
            <pointOfServiceNo>string</pointOfServiceNo>
            <pointOfServiceName>string</pointOfServiceName>
            <pointOfServiceDescription>string</pointOfServiceDescription>
            <pointOfServiceOrderingGroupNo>string</pointOfServiceOrderingGroupNo>
            <orders>
              <OrderingOrderStruct>
                <orderType>string</orderType>
                <orderDate>date</orderDate>
                <deliveryDate>date</deliveryDate>
                <orderStatus>int</orderStatus>
                <articles>
                  <OrderingArticleStruct>
                    <articleNo>string</articleNo>
                    <articleDescription>string</articleDescription>
                    <articleSize>string</articleSize>
                    <articleTargetQty>int</articleTargetQty>
                    <articleMinQty>int</articleMinQty>
                    <articleMaxQty>int</articleMaxQty>
                    <articleIntervalQty>int</articleIntervalQty>
                  </OrderingArticleStruct>
                </articles>
              </OrderingOrderStruct>                  
          </OrderingPointOfServiceStruct>              
        </pointsOfService>
      </OrderingDeliveryAddressStruct>
    </deliveryAddresses>
    <orderingGroups>
      <OrderingOrderingGroupStruct>
        <orderingGroupNo>string</orderingGroupNo>
        <orderingGroupDescription>string</orderingGroupDescription>
      </OrderingOrderingGroupStruct>
      <OrderingOrderingGroupStruct>
        <orderingGroupNo>string</orderingGroupNo>
        <orderingGroupDescription>string</orderingGroupDescription>
      </OrderingOrderingGroupStruct>
    </orderingGroups>
  </OrderInfoResult>
</OrderInfoResponse>

I have data classes (Models) that are used for the response of the Api.

Data classes

data class OrderInfo(
    @SerializedName("deliveryAddresses")
    val deliveryAddresses: List<DeliveryAddresses>,
    @SerializedName("orderingGroups")
    val orderingGroups: List<OrderingGroup>
)

data class DeliveryAddresses(
    @PrimaryKey
    val id: String = UUID.randomUUID().toString(),
    @SerializedName("deliveryAddressName")
    val deliveryAddressName: String,
    @SerializedName("deliveryAddressNo")
    val deliveryAddressNo: String,
    @SerializedName("pointsOfService")
     val pointsOfService: List<PointsOfService>
)

data class OrderingGroup(
    @SerializedName("orderingGroupDescription")
    val orderingGroupDescription: String,
    @SerializedName("orderingGroupNo")
    val orderingGroupNo: String
)

data class PointsOfService(
    @SerializedName("orders")
    val orders: List<Order>,
    @SerializedName("pointOfServiceDescription")
    val pointOfServiceDescription: String,
    @SerializedName("pointOfServiceName")
    val pointOfServiceName: String,
    @SerializedName("pointOfServiceNo")
    val pointOfServiceNo: String,
    @SerializedName("pointOfServiceOrderingGroupNo")
    val pointOfServiceOrderingGroupNo: String
)

data class Order(
    @SerializedName("articles")
    val articles: List<Article>,
    @SerializedName("deliveryDate")
    val deliveryDate: String,
    @SerializedName("orderDate")
    val orderDate: String,
    @SerializedName("orderStatus")
    val orderStatus: Int,
    @SerializedName("orderType")
    val orderType: String
)

data class Article(
    @SerializedName("articleDescription")
    val articleDescription: String,
    @SerializedName("articleIntervalQty")
    val articleIntervalQty: Int,
    @SerializedName("articleMaxQty")
    val articleMaxQty: Int,
    @SerializedName("articleMinQty")
    val articleMinQty: Int,
    @SerializedName("articleNo")
    val articleNo: String,
    @SerializedName("articleSize")
    val articleSize: String,
    @SerializedName("articleTargetQty")
    val articleTargetQty: Int
)

I want to be able to store my data to a Room database, do the entity classes have to be seperate from my current data classes that I use for my retrofit service?

Many thanks.


Solution

  • Yes, they have to be in separate packages for better structuring of your code.

    For example, you can create a "remote" package for your Retrofit Service and put all your API implementation in that. Then, create another package called "local" for classes related to Room and DB operations.

    Example structure:

    data:
      > local:
         >model
            > Dao
               ...
      > Remote:
        > Api
           ...