darshan kamat
darshan kamat

Reputation: 424

Dependency on multiple classes

I am new to Spring and need to expose web service to save Customer data.My customer object is huge json with multiple repeating elements . I have seperate classes for each of these like Name,Address,Identification,Characteristics etc Now when I define the POJO for Customer I need to declare all the dependencies ,have preferred Constructor based DI for this.

I have read many articles stating that Class should have single responsibility and multiple dependencies on other classes is a sign of bad design Now my question is , Since my object is huge ,how can i reduce these dependencies as all of these are a part of my root Object-Customer. Thanks.

Sample data :
{
   "customer": {
      "partyId": "9000073442",
      "partyRole": [
         {
            "partyRoleId": "CRM000"
         },
         {
            "partyRoleId": "CRM004"
         }
      ],
      "name": [],
      "associationName": [],
      "mobileNumber": "+919387439865",
      "emailId": "[email protected]",
      "status": "CUSA",
      "IndividualIdentification": [
         {
            "id": "POA",
            "type": "FS9200",
            "number": "23456789",
            "issueDate": "2018-01-04",
            "placeOfIssue": "MUMB",
            "issuingAuthority": "RTO"
         },
         {
            "id": "POI",
            "type": "FS9200",
            "number": "23456789",
            "issueDate": "2018-01-04",
            "placeOfIssue": "MUMB",
            "issuingAuthority": "RTO"
         }
      ],
      "PanIdentification": {
         "number": []
      },
      "characteristics": [
         {
            "attributeName": "DOC_REF_NUM",
            "attributeValue": "EM000000155Q"
         },
         {
            "attributeName": "ROUTING_ZONE",
            "attributeValue": "CO007"
         }
      ],
      "segment": {
         "attributeName": "CUSTOMER_CATEGORY",
         "attributeValue": "0007"
      },
      "associatedPartyId": "9000073441",
      "organization": {
         "name": "IPBillingLocation",
         "type": "0007",
         "designation": [],
         "department": []
      },
      "customerUpdationDateTime": "2018-02-08T12:24:31.776Z",
      "addresses": {
         "validFrom": "2018-01-28T18:30:00Z",
         "addressId": "0001980438",
         "subUnitNr": "23456",
         "buildingName": "sadfgh",
         "streetName": "asdfgh",
         "landmark": "sdfgh",
         "locality": "Ghansoli S.O",
         "postcode": "400701",
         "city": "Mumbai",
         "district": "Thane",
         "state": "MH",
         "country": "IN",
         "jioCenterId": "I001",
         "addressType": "PER_ADD"
      },
      "customerCreationDateTime": "2018-01-29T19:10:05",
      "circleId": "MU",
      "aadharIdentification": {
         "number": []
      }
   },
   "response": {
      "interactionStatus": "0"
   },
   "jioroute": "CO007"
}

My Customer POJO :

public class Customer{

  @Autowired
  private IndividualIdentification Indid;

  @Autowired
  private Characteristics chars;

  @Autowired
  private Addresses  addresses;


  // upto n 
}

Does this align with the best practice ? If not what is the alternative to reduce dependency

Upvotes: 0

Views: 430

Answers (2)

davidxxx
davidxxx

Reputation: 131526

Model classes should not be dependencies managed by Spring.
You generally create them in methods or "receive" them from the WS invocation after the request unserialization.
The model instances created from a WS invocation are methods bound and specific to a WS request.
So injecting them in a bean doesn't make sense.
Use rather method parameters to transmit them through the layers.

Model classes should be POJOs (or JPA entities if you use the same class both for the JSON and the persistence layer) but never Spring beans.

Upvotes: 1

fredlo2020
fredlo2020

Reputation: 1

I think there is a bit of misunderstanding regarding what classes are supposed to do.

You only need to register and inject dependencies into Spring context for services, controllers, Data Access Objects (DAO, communicate with databases), Repositories, Configuration.

The class "Customer" should be instantiated and handled by you.

Upvotes: 0

Related Questions